复习
1:客户端和数据库操作
1. 登录 mysql -uroot -p
> 2. 查看当前数据库的版本 select version();
> 3. 显示所有数据库 show databases;
> 4. 查看创建库的语句 show create database 库名;
> 5. 创建数据库 create database [if not exists];
数据库名 character set 字符集编码 collate 排序规则;
> 6. 选择数据库 use 数据库名
> 7. 查看当前选择的数据库 select database();
> 8. 修改数据库 alter database数据库名 character set 字符编码及 collate 排序规则;
> 9. 删除数据库 drop database [if exists] 数据库;
> 10. 查看警告信息 show warnings;
> 11. 退出 quit/exit
2:表操作
> 1.查看当前数据库中有哪些表 show tables;
> 2.创建一张新表 create table [if not exists] 表名 (列/字段名 数据类型[,列/字段名 数据类型]);
> 3.查看表结构 desc 表名;
> 4.查看创表语句 show create table 表名;
> 5.修改表 alter table 表名 {add | modify| dorp |rename} 列;
> 6.删除表 dorp table [if exists] 表名;
3: CRUD 增删改查
> 1.新增-插入 insert into 表名 [列名[,列名]] values (值[,值]);
> 指定了多少列名,就需要指定多少值,值与列名一一对应,不指定列名,值的顺序与个数和表中定义列的顺序一一对应
2.查询操作
>a.全列查询 select * from 表名;//不加限制的查询会把表中所有的数据都查出来.
>b.指定列查询 select 列[,列...]from 表名;//推荐使用指定列查询
>c.列为表达式查询 列名/表达式 from 表名;
>d.去重查询 select distinct 列名[,列名] from 表名 //如果查询列表中有多个列,每个列的值都相等才会判定为重复
>e.别名查询 select 列名/表达式 [as ] '别名' from 表名 //别名中如果包含空格,需要用单引号引用
> f.排序 select 列名[,列名...] from 表名 order by 列名 asc | desc[,列名 asc |desc]; asc升序,desc降序
> g.条件查询select * from 表名 where 列名/表达式 比较/逻辑运算符;//多个条件可以用and和or 连接
> h.区间查询 select * from 表名 where 列名 between 开始条件 and 结束条件;//开始条件<=列的值<=结束条件
> i.模糊查询 select * from 表名 where 列名 like '%值_';//%匹配任意字符,_匹配单个字符
> j.分页查询 select *from 表名 where 条件 order by 列名 asc|desc limit num;//查询前num 条记录
> select *from 表名 where 条件 order by 列名 asc|desc limit start,num;//从strat 条开始向后查num条记录
> select *from 表名 where 条件 order by 列名 asc|desc limit num offset start;//
> 从strat 条开始向后查num条记录
3.更新
update 表名 set 列=值 where 条件 order by 子句 limit num;
//如果不指定条件和limit的数据就会更新整张表的数据
4.删除
delect from 表名 where 条件 order by 子句 limit num;//如果不指定条件和limit的数据就会删除整表数据
5.截断表
truncate 表名;//把表的状态重置为初始状态,表中的数据也会被清空
4:数据库约束
1.非空约束: NOT NULL 标记一个字段是否可以为空,指定这个约束,字段的值就不能为空
2.唯一约: UNIQUE 标记一个字段的内容在当前表中唯一,不能重复,但是可以为NULL
3.主键约束: PRIMARY KEY 在校验的方式上相当于NOT NULL 与UNIQUE 的组合,建议为每张表中定义自增主键
4.外键约束:FOREIGN KEY 一个表中的字段与另一个表中的主键或者唯一键建立关联关系,当对表中的数据进行增删改查的时候数据库会帮助我们进行检查
5.默认约束:DEFAULT 当一个字没有制定时,就会用默认值去填充该列,当手动指定列的值位NULL时,默认就不会生效
6.检查约束:CHECK 对当前列的值合法性的进行检查,在8.0中才生效,5.7版本允许定义但是不生效
5:表的设计
三大范式
关系模型
6:聚合函数
1.COUNT(列*)统计结果条数
2.SUM(列)求和
3.AVG(列)求平均数
4.MAX(列)求最大值
5.MIN(列)求最小值
7:GROUP BY分组查询和HAVING子句
8:联合查询(表连接查询)
1.内连接
select * from table 1,table 2...where table 1.xxx=table 2.xxx;
select * from table 1 join table 2 on table 1.xxx=table 2.xxx where 条件;
select * from table 1 inner join table 2 on table 1.xxx=table 2.xxx where 条件;
2.外连接,分为左连接和右连接
select * from table1 left join table2 on table1.xxx=table2.xxx;
select * from table1 right join table2 on table1.xxx=table2.xxx;
3.自连接
select * from table t1,table t2 where t1.xxx=t2.xxx;
把行转换为列,在查询的时候可以使用where 条件进行过滤,实行了行与行之间的比较
4.子查询
单行子查询: select *from table1 where id=(select id from table 2 wherer clo=xxx);
多行子查询: select * from table1 where id [NOT] IN(select id from table2 where clo=xxx);
多列查询:select * from table1 where (列1,列2,列3...) IN (select 列1,列2,列3 from table2 where clo=xxx);
5.合并查询
select * from table1 union select * from table2//过滤掉重复的行
select * from table1 union all select * from table2;//不会过滤重复行,显示所有的查询结果作用把多个查询结果合并成一个返回给客户端
6.一条SQL语句中各部分执行顺序
自己构造一条SQL,不一定执行成功,但是包含相关的关键字
select distinct id,name,avg(age) from student join class on student.class_id where class_id=1 group by student.id having avg(age)>0 order by student.id asc limit 100;
顺序:from __ join on __ where __ group by __ having __ select __ distinct __ order by __ limit