## 创建库(注意:库名多是大写字母) create database 库名; ## 查看有哪些库 show databases; ## 删除某个库 drop database 库名; ## 创建库时制定编码格式 create database 库名 character set utf8; ## 进入某一个库下 use 库名;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
## 创建一个数据库表 create table person{ name varchar(10), sex varchar(2), age int }; ## 查看该仓库下的所有表 show tables; ## 查看某个表的所有列及列的数据类型 desc 表名; ## 查看创建表的详细语句,数据库引擎及字符编码 show create table ## 表重命名 alter table 旧名 rename 新名 ## 删除表 drop table 表名;
增删改查
增
1 2 3 4 5 6
## 插入所有列 insert into person values("zhangsan","男",18); ## 插入部分列 insert into person(name,age) values("zhangsan",18); ## 插入查询出来的数据 insert into user(username) select name from account;
查
1 2 3 4 5 6 7 8 9 10
## 查看所有数据 select * from person; ## 查看符合条件的数据 select * from person where age = 18; ## 查看通过符合多个条件的数据 select * from person where age = 18 and sex = '男'; ## 查看部分列数据 select name,sex from person where age = 18; ## 别名的使用,注:as可以省略 select name as "姓名" ,sex as "性别" from person where age = 18;
改
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
## 更新所有数据 update person set name = 'lisi'; ## 更新符合条件的数据 update person set name = 'lisi' where age = 18; ## 修改字段数据类型 alter table 表名 modify 字段名 字段类型 ## 修改字段名 alter table 表名 change 旧字段名 新字段名 新字段类型 ## 添加字段 alter table 表名 add 字段名 字段类型 ## 删除字段 alter table 表名 drop 字段名 字段类型 ## 删除表的外键约束 alter table 表名 drop foreign key 外键约束名 ## 删除没有关联的表 drop table if exists 表名 (多个逗号隔开)
删
1 2 3 4
## 删除表中的所有数据(如果不加where表示删除所有数据) delete from person where ..... ## 删除表中所有数据 truncate table 表名;
createtable person( id int(11) primary key auto_increment, name varchar(10) default'', sex varchar(2) default'男', age intdefault0 );
表达式
like
1 2 3 4 5
select*from person where name like'张三%'; select*from person where name like'%张三'; select*from person where name like'%张三%'; select*from person where name like'%张%三'%; select*from person where name like'张三_';
排序
1 2 3 4
## 默认为升序(asc),降序使用desc select*from person orderby age (asc|desc) ## 排序可以同时指定两个关键词 select*from person orderby age desc,name asc
分页
1 2 3 4
## 1的意思是从1开始,但是不包括1,2的意思是取2条 select*from person limit 1,2 ## 分页与排序同时使用时,必须先排序,后分页 select*from person orderby age limit 1,2
聚合函数
1 2 3 4 5 6 7 8
## 统计数量是最好用count(1) selectsum(age) from person; selectsum(age) from person where id >2; selectcount(*) from person; selectcount(1) from person; selectcount('a') from person; selectcount(age) from person; selectcount(*) from person where id >2;
1 2 3
selectmax(age) from person; selectmin(age) from person; selectavg(age) from person;
分组
1 2 3
select age from person groupby age; select name,age from person groupby name,age; select age,count(1) from person groupby age;
分组统计
1
selectcount(1) from (select age from person groupby age) temp
分组获取最大值
1 2
## 统计各年龄的最大id selectmax(id) ,age from person groupby age;
having的使用
1 2 3 4
## where不能和groupby一起使用,所以此时出现了having select age from person groupby age having age >2; selectcount(1),age from person groupby age havingcount(1) >1; select age from person groupby age havingcount(1) >1;
## student表 createtable student( id int(11) primary key auto_increment; name varchar(100) default'', age int(11) default0 );
## score表 createtable score( id int(11) primary key auto_increment, student_id int(11), type varchar(100) default'', score int(11) default0 );
交叉查询
1 2 3 4 5
## 笛卡尔积运算 select*from student crossjoin score select*from student st crossjoin score sc where st.id = sc.student_id; ## 开发中最常用的语句 select st.name st,age,sc.type,sc.score from student st,score sc where st.id = sc.student_id;
内连接查询
1 2
## 这种方式和交叉连接得到的结果相同,所以这种方式很少使用 select*from student st innerjoin score sc on st.id = sc.student_id;
外连接查询
1 2 3 4
## 左外连接(会获取到左边的所有数据) select*from student st leftouterjoin score sc on st.id = sc.student_id; ## 右外连接 select*from student st rightouterjoin score sc on st.id = sc.student_id;
嵌套查询
1
select*from score where student_id in (select id from student where naem ='zhangsan');
组合查询
1 2 3 4 5 6 7 8
## union中,所有查询的列数和列顺序必须相同 select cust_name, cust_contact, cust_email from customers where cust_state in ('a,'b','c') union select cust_name, cust_contact, cust_email from customers where cust_name = 'test';
索引操作
1 2 3 4 5 6
## 创建索引 create index user_index onuser(id); ## 创建唯一索引 createunique index user_index onuser(id); ## 删除索引 altertableuserdrop index user_index;
## 开始事务 start transaction; ## 插入操作 A insertinto'user'values(xxx,xxx); ## 创建保留点 savepoint updateA; ## 插入操作 B insertinto'user'values(yyy,yyy); ## 回滚到保留点updateA rollbackto updateA; ## 提交事务,只有操作 A 生效 commit;
权限控制
GRANT 和 REVOKE 可在几个层次上控制访问权限:
整个服务器,使用GRANT ALL 和 REVOKE ALL
整个数据库,使用 ON database.*;
特定的表,使用ON database.table;
特定的列
特定的存储过程
新创建的账户没有任何权限
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
## 创建账户 createuser myuser identified by'mypassword'; ## 修改账户 updateusersetuser='newuser'whereuser='myuser'; flush privileges; ## 删除账户 dropuser myuser; ## 查看权限 show grants for myuser; ## 授予权限 grantselect,inserton*.*to myuser; ## 删除权限 revokeselect ,inserton*.*from myuser; ## 更改密码 set passowrd for myuser ='mypass';
存储过程(todo 待完善)
触发器(todo 待完善)
SQL优化
创建索引
执行计划分析
查看某个sql语句的执行计划只需要在sql语句前面添加EXPLAN
1
EXPLAIN select*from person where age =16and id >0;
type:表示执行sql使用了哪种类型。类型从好到差依次是system,const, eq_reg, ref, range, index, ALL
rows:数据遍历的行数,数值越小越好
key_len:使用索引的长度,该值越小越好
key:实际用到的索引,null就是没有
id:相同,执行顺序由上而下;不相同,子查询id序号递增,id值越大优先级越高,越先被执行
select_type:
Ref:显示索引的哪一列被使用了,如果可能的话,是一个常数。
sql优化具体分析
尽量给字段设置默认值,避免字段值为null(如果字段是int类型的age,首先给age创建索引时null不会参与创建索引。where age=0 比where age is null效率高)
在where语句中少使用or,使用or将不使用索引 如select name from person where age = 16 or age = 20; 可以写成: select name from person where age = 16 union all select name from person where age = 20;