mysql命令使用3
算术运算函数
sum()求和
mysql> select sum(price) from books;
+------------+
| sum(price) |
+------------+
| 10048 |
+------------+
1 row in set (0.00 sec)
avg()平均值
求书籍id小于3的所有书籍的平均价格
mysql> select avg(price) from books where bid<=3;
+------------+
| avg(price) |
+------------+
| 39.3333 |
+------------+
1 row in set (0.00 sec)
max()最大值
min()最小值
count()统计记录数
统计价格大于40的所有书籍的数量
mysql> select count(*) from books where price>40;
+----------+
| count(*) |
+----------+
| 43 |
+----------+
1 row in set (0.00 sec)
mysql> select count(distinct price) from books where price>40;
+-----------------------+
| count(distinct price) |
+-----------------------+
| 26 |
+-----------------------+
1 row in set (0.00 sec)
算数运算 +- * /
给所有价格小于40元的书籍涨价5元
mysql> update books set price=price+5 where price <40;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
给所有价格高于70元的书籍打8折
mysql> update books set price=price*0.8 where price>70;
Query OK, 10 rows affected (0.00 sec)
Rows matched: 10 Changed: 10 Warnings: 0
字段修饰符(约束)null 和not null
not null的字段是不能插入null的,只能插入空值或其他内容,空值不占用空间,而null占用额外空间。
not null 比null的效率更高,因为在索引中如果含义null,索引不会存储null的空间,所以索引中的null效率会变慢。
mysql> select * from test;
+------+------+
| col1 | col2 |
+------+------+
| | NULL |
| 1 | 2 |
| | 2 |
| | 2 |
| adb | NULL |
+------+------+
5 rows in set (0.00 sec)
mysql> select * from test where col1 is not null; #not null也包含空值
+------+------+
| col1 | col2 |
+------+------+
| | NULL |
| 1 | 2 |
| | 2 |
| | 2 |
| adb | NULL |
+------+------+
5 rows in set (0.00 sec)
mysql> select * from test where col1 <> ''; #其中“<>”代表不等于
+------+------+
| col1 | col2 |
+------+------+
| 1 | 2 |
| adb | NULL |
+------+------+
2 rows in set (0.00 sec)
DEfault 默认字段
mysql> create table test2(name varchar(8) not null,dept varchar(25) default 'sister');
Query OK, 0 rows affected (0.03 sec)
mysql> desc test2;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name | varchar(8) | NO | | NULL | |
| dept | varchar(25) | YES | | sister | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> insert into test2(name) values('abc');
Query OK, 1 row affected (0.01 sec)
mysql> select * from test2;
+------+--------+
| name | dept |
+------+--------+
| abc | sister | #default默认字段自动加上,无需手写
+------+--------+
1 row in set (0.00 sec)
AUTO_INCREMENT 字段约束 自增长
mysql> create table test3(id int not null auto_increment primary key ,label varchar(20) not null);
mysql> desc test3;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO| PRI | NULL | auto_increment |
| label | varchar(20) | NO | | NULL | |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
mysql> insert into test3(label) values('abc');
mysql> insert into test3(label) values('abcd');
mysql> insert into test3 values(9,'abc');
mysql> select * from test3;
+----+-------+
| id | label |
+----+-------+
| 1 | abc |
| 2 | abcd |
| 9 | abc |
+----+-------+
3 rows in set (0.00 sec)
mysql> insert into test3(label) values('abcff');
Query OK, 1 row affected (0.05 sec)
mysql> select * from test3;
+----+-------+
| id | label |
+----+-------+
| 1 | abc |
| 2 | abcd |
| 9 | abc |
| 10 | abcff |
+----+-------+
4 rows in set (0.00 sec)
mysql> delete from test3;
Query OK, 3 rows affected (0.01 sec)
mysql> select * from test3;
Empty set (0.00 sec)
mysql> insert into test3(label) values('dosg');
Query OK, 1 row affected (0.01 sec)
mysql> select * from test3;
+----+-------+
| id | label |
+----+-------+
| | dosg | #默认从上次的值往后计算
+----+-------+
1 row in set (0.01 sec)
mysql> delete from test where col1='1';
Query OK, 0 rows affected (0.00 sec)
mysql> truncate test3; #截断表,默认表内容全丢失,下次插入新字段从1开始计算
Query OK, 0 rows affected (0.01 sec)
mysql> insert into test3(label) values('xuesheng');
Query OK, 1 row affected (0.01 sec)
mysql> select * from test3;
+----+----------+
| id | label |
+----+----------+
| | xuesheng |
+----+----------+
1 row in set (0.01 sec)
索引是一种特殊的文件(InnoDB数据表中的索引是表空间的一个组成部分),包含对数据表里所有记录的引用指针。相当于一本书里面的目录,加快数据库查询速度。
缺点:索引是以文件存储的,索引过多,占用磁盘空间较大。影响insert,update,delete的执行时间。
索引数据必须与数据表数据同步,如果索引过多,当数据表数据更新时,索引也要同步更新,这就降低了效率。
索引类型
普通索引: 不具备唯一性,只是为了提高查询速度
mysql> create table test6(id int(4),name varchar(20),pwd varchar(20),index(pwd)); #index(pwd)中index为索引,pwd为需要索引的列;还可以写成key(pwd)
Query OK, 0 rows affected (0.02 sec)
mysql> desc test6;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(4) | YES | | NULL | |
| name | varchar(20)| YES | | NULL | |
| pwd | varchar(20) | YES | MUL | NULL | | #Key为MUL表示为普通索引
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)
mysql> create table test7(id int(4),name varchar(20),pwd varchar(20),index Chen (pwd)); #指定pwd列的索引名称为Chen
mysql> show create table test7;
| Table | Create Table |
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| test7 | CREATE TABLE `test7` (
`id` int(4) DEFAULT NULL,
`name` varchar(20) DEFAULT NULL,
`pwd` varchar(20) DEFAULT NULL,
KEY `Chen` (`pwd`) #指定pwd的索引名称为Chen,默认不指定为列名
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)
主键索引 primary
mysql> create table demo(id int(4) not null auto_increment,name varchar(20) default null,primary key(id)); #主键设置key(id)
Query OK, 0 rows affected (0.01 sec)
mysql> desc demo;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(4) | NO | PRI | NULL | auto_increment | #key 为PRI主键模式
| name | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
mysql> create table test12(id int(4) not null ,name varchar(20)default null,key chen(name));
Query OK, 0 rows affected (0.07 sec)
mysql> show create table test12;
+--------+-----------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+--------+-----------------------------------------------------------------------------------------------------------------------------------------------+
| test12 | CREATE TABLE `test12` (
`id` int(4) NOT NULL,
`name` varchar(20) DEFAULT NULL,
KEY `chen` (`name`) #创建索引,并设置索引名称为chen
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+--------+-----------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> alter table test12 drop key name; #删除索引字段,无法删除
ERROR 1091 (42000): Can't DROP 'name'; check that column/key exists
mysql> alter table test12 drop key chen; #删除索引名称,可以删除
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table test12;
+--------+------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+--------+------------------------------------------------------------------------------------------------------------------------+
| test12 | CREATE TABLE `test12` (
`id` int(4) NOT NULL,
`name` varchar(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+--------+------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> alter table test12 add key wang(id); #增加索引,给id字段增加索引名称为wang
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table test12;
+--------+---------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+--------+---------------------------------------------------------------------------------------------------------------------------------------------+
| test12 | CREATE TABLE `test12` (
`id` int(4) NOT NULL,
`name` varchar(20) DEFAULT NULL,
KEY `wang` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+--------+---------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
唯一性索引 unique
mysql> create table test13(id int(4) auto_increment primary key,uname varchar(20),up varchar(20),unique index(uname));
Query OK, 0 rows affected (0.04 sec) #对uname字段做唯一性索引
mysql> desc test13;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(4) | NO | PRI | NULL | auto_increment |
| uname | varchar(20) | YES | UNI | NULL | | #唯一性索引,字段内容不能重复,具有唯一性
| up | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
mysql> alter table test13 drop key uname; #删除索引
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table test13 add unique chen(uname); #增加唯一性索引,字段为uname,索引名称为chen
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table test13;
+--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| test13 | CREATE TABLE `test13` (
`id` int(4) NOT NULL AUTO_INCREMENT,
`uname` varchar(20) DEFAULT NULL,
`up` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `chen` (`uname`) #修改唯一性索引后的名称
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> alter table test13 change id id int(4) not null; #删除auto_increment类型,使用change
Query OK, 0 rows affected (0.10 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc test13;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(4) | NO | PRI | NULL | |
| uname | varchar(20) | YES | UNI | NULL | |
| up | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> alter table test13 drop primary key; #删除主键类型
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc test13;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(4) | NO | | NULL | | #主键类型为空,已删除
| uname | varchar(20) | YES | UNI | NULL | |
| up | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
复合索引
mysql> create table firewall(host varchar(15) not null, port smallint(4) not null, access enum('deny','allow') not null,primary key(host,port));
Query OK, 0 rows affected (0.01 sec) #主键有2个,分别为host和port字段
mysql> desc firewall;
+--------+----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+----------------------+------+-----+---------+-------+
| host | varchar(15) | NO | PRI | NULL | |
| port | smallint(4) | NO | PRI | NULL | |
| access | enum('deny','allow') | NO | | NULL | |
+--------+----------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> insert into firewall values('192.168.2.1',22,'allow');
Query OK, 1 row affected (0.01 sec)
mysql> insert into firewall values('192.168.2.1',23,'deny'); #这里host和port作为一个整体,只有这个整体信息一样,才被认为是内容相同不能插入
Query OK, 1 row affected (0.00 sec)
外键约束 设定表与表之间的约束关系on delete cascade on update cascade 表示级联更新和级联删除
例如有2张表,一张用户表,一张订单表,用户表里的用户删除了,订单表里的用户就成了无头数据。所以需要使用外键约束来定义2张表之间的关联关系,保障第一张表改变的同时第二张表也跟着改变。
mysql> create table `user`(id int(11) not null auto_increment,name varchar(50) not null default '',sex int(1) not null default '0',primary key(id))ENGINE=innodb;
Query OK, 0 rows affected (0.01 sec)
mysql> create table `order`(o_id int(11) auto_increment,u_id int(11) default '0',username varchar(50),money int(11),primary key(o_id),index(u_id),foreign key order_f_key(u_id) references user(id) on delete cascade on update cascade)ENGINE=innodb;
Query OK, 0 rows affected (0.01 sec)
mysql> insert into `order`(u_id,username,money) values(1,'HA',234),(2,'LB',146),(3,'HPC',256);
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from `order`;
+------+------+----------+-------+
| o_id | u_id | username | money |
+------+------+----------+-------+
| 1 | 1 | HA | 234 |
| 2 | 2 | LB | 146 |
| 3 | 3 | HPC | 256 |
+------+------+----------+-------+
3 rows in set (0.00 sec)
mysql> delete from user where id=1; #删除user表中id=1的行
Query OK, 1 row affected (0.06 sec)
mysql> select * from `order`; #删除了user表后,order表自动进行了更新
+------+------+----------+-------+
| o_id | u_id | username | money |
+------+------+----------+-------+
| 2 | 2 | LB | 146 |
| 3 | 3 | HPC | 256 |
+------+------+----------+-------+
2 rows in set (0.01 sec)
视图 view 经常需要查看几个固定表时,可以使用视图关联这几个表,以后只查看该视图就能够找到这几个表中的内容,不用单独去查看每一张表。视图是存在于数据库中的虚拟表
table1 table2 table3 >> select * from view1;book
mysql> create view bc as select b.bName,b.price,c.bTypeName from books as b left join category as c on b.bTypeId=c.bTypeId;
Query OK, 0 rows affected (0.01 sec) b和c为别名,b表示books,c表示category 其中left join表示左连接
mysql> select * from bc; #通过视图查看表信息
+---------------------------------------------------------+-------+---------------+
| bName | price | bTypeName |
+---------------------------------------------------------+-------+---------------+
| 网站制作直通车 | 39 | 网站 |
| 黑客与网络安全 | 41 | 黑客 |
| 网络程序与设计-asp | 43 | 网站 |
| pagemaker 7.0短期培训教程 | 43 | 平面
+---------------------------------------------------------+-------+---------------+
4 rows in set (0.07 sec)
mysql> select * from bc where price<40;
+-----------------------+-------+-----------+
| bName | price | bTypeName |
+-----------------------+-------+-----------+
| 网站制作直通车 | 39 | 网站 |
+-----------------------+-------+-----------+
1 row in set (0.00 sec)
mysql> alter view bc as select b.bName,b.publishing,c.bTypeId from books as b left join category as c on b.bTypeId=c.bTypeId;
Query OK, 0 rows affected (0.01 sec) #修改视图信息
mysql> update bc set bName='HA' where price=39; #不能更改原因,修改了视图列信息,只有bName,b,publishing,而没有
ERROR 1288 (HY000): The target table bc of the UPDATE is not updatable
mysql> drop view bc; #删除视图
Query OK, 0 rows affected (0.00 sec)
mysql命令使用3的更多相关文章
- Mysql命令大全
格式: mysql -h主机地址 -u用户名 -p用户密码 1.连接到本机上的MYSQL.首先打开DOS窗口,然后进入目录mysql\bin,再键入命令mysql -u root -p,回车后提示你输 ...
- ****Linux MySQL命令运用个人总结
1.xampp在linux下如何使用mysql命令 A: 解决方法(推荐第二个方法): 1.xampp中的命令工具在/opt/lampp/bin/目录中,所以可以这样运行mysql命令 /opt/la ...
- MySQL命令行登录的例子
环境:MySQL Sever 5.1 + MySQL命令行工具 问题:MySQL命令行登录 解决: 命令 行登录语法: mysql –u用户名 [–h主机名或者IP地址] –p密码 说明:用户名是你登 ...
- MySQL命令行下执行.sql脚本详解
本文主要介绍一个在MySQL命令行下执行脚本文件的例子,通过这个例子让我们来了解一下在命令行下MySQL是怎样执行脚本的吧.现在我们开始介绍这一过程. 1.首先编写sql脚本,保存为的:book.sq ...
- Mysql 命令大全
1.连接Mysql 格式: mysql -h主机地址 -u用户名 -p用户密码1.连接到本机上的MYSQL.首先打开DOS窗口,然后进入目录mysql\bin,再键入命令mysql -u root - ...
- mysql 在windows下,使用 net start mysql 命令发生错误 服务名无效 或 1067
mysql 在windows下,使用 net start mysql 命令发生错误 :服务名无效 或 1067 先使用mysqld -install安装一下 删除data目录下的日志等文件(因为之前 ...
- MySQL命令大全:MySQL常用命令手册、MySQL命令行大全、查询工具
1.连接Mysql 格式: mysql -h主机地址 -u用户名 -p用户密码 1.连接到本机上的MYSQL.首先打开DOS窗口,然后进入目录mysql\bin,再键入命令mysql -u root ...
- MYSQL命令行使用指南
一.连接MYSQL. 格式: mysql -h主机地址 -u用户名 -p用户密码 1.例1:连接到本机上的MYSQL. 首先在打开DOS窗口,然后进入目录 mysqlbin,再键入命令mysql -u ...
- mysql命令行以及mysql workbence查询结果中文乱码的解决方法
最近正在学习mysql,安装环境是windows server 2003 32位操作系统+mysql 5.1.47同时也安装了mysql命令行以及mysql workbench这里是test数据库cr ...
- MySQL命令行导出数据库
MySQL命令行导出数据库:1,进入MySQL目录下的bin文件夹:cd MySQL中到bin文件夹的目录如我输入的命令行:cd C:\Program Files\MySQL\MySQL Server ...
随机推荐
- 14.AutoMapper 之依赖注入(Dependency Injection)
https://www.jianshu.com/p/f66447282780 依赖注入(Dependency Injection) AutoMapper支持使用静态服务定位构建自定义值解析器和自定 ...
- html元素标签时间格式化
<fmt:formatDate value="${user.loginTime}" pattern="yyyy-MM-dd HH:mm:ss"/>
- this 到底指向谁
this 的指向,是在调用函数时根据执行上下文所动态确定的. 在函数体中,简单调用该函数时(非显式/隐式绑定下),严格模式下 this 绑定到 undefined,否则绑定到全局对象 window/g ...
- MySQL下载~安装教程~这里示例 MySQL 8.0 Command Line Client
打开 https://www.mysql.com 也可以选择我分享的百度网盘文件 MySQL8.0.16.0 安装包 https://pan.baidu.com/s/1U8DkyJVp9Zvx7Zok ...
- springboot学习1
gradle环境配置 https://www.w3cschool.cn/gradle/ctgm1htw.html Spring profile 多环境配置管理 参考:https://www.cnblo ...
- 关于ARM PC值
PC值(Program Counter). ARM采用流水线来提高CPU的利用效率, 对于三级流水线, 一条汇编指令的执行包括 取值, 译码, 执行三个阶段. 当MOV指令的取指动作完毕后, 进入M ...
- linux新建用户并分配sudo权限
新建用户 useradd [username] 给用户设置密码 passwd [username] 设置sudo权限 首先将sudoers权限设置可写入 chmod u+w /etc/sudoers ...
- Ubuntu安装DroidCamX网络摄像头
1.安装依赖项 sudo apt-get install gcc make linux-headers-`uname -r` 2.安装 cd /tmp/ bits=`getconf LONG_BIT` ...
- LB_GETCURSEL和LB_GETTEXT的使用
case IDC_LIST1: { switch (HIWORD(wParam)) { case LBN_DBLCLK: { HWND hwndList = GetDlgItem(hDlg, IDC_ ...
- /etc/fstab 下的配置参数
第一列:分区的label或者UUID 若要查看/dev/sdb1设备的label或者UUID[root@localhost ~]# dumpe2fs -h /dev/sdb1dumpe2fs 1.42 ...