一、 数据查询语言(DQL)(重中之重)
残缺语法格局:
- select 表达式1|字段,....
- [from 表名 where 条件]
- [group by 列名]
- [having 条件]
- [order by 列名 [asc|desc]]
- [limit 地位,数量]
<1> 一般查问
- select 查问表达式; // 最简略的sql语句,是一个函数
- select database();
- select version();
- select now();
<2> 条件查问
- where 条件表达式, 反对运算符和函数
MySQL反对的运算符:
- =、 !=、 >、 >=、 <、 <=、 <>
- and、 or、 not
- is null、 is not null
- between...and... (区间查问,多少到多少之间)
- in(set);
like 通配符和占位符: % _ (含糊查问)
- %: 示意0个或者多个字符
- _: 示意占位一个
-- 查问所有的老师信息 select * from teacher;-- 查问id 大于2的老师信息 select * from teacher where id>2; -- 查问姓名为空的老师信息 在数据库中null永远都不等于null,那么怎么去判断null值?通过 is null / is not null-- select * from teacher where name=null; # 谬误 select * from teacher where name is not null;-- 查问id为1 并且 姓名是 "xiaosi"的老师信息 select * from teacher where id=1 and name='xiaosi';-- 查问id为1 并且 姓名是 "xiaosi"的老师信息 select * from teacher where id=1 or name='xiaosi';-- 查问薪水在2000到10000之间的老师信息 select * from teacher where sal >=2000 and sal <=10000; select * from teacher where sal between 2000 and 10000; # 这种形式等同于下面-- 查问姓名中有 ‘尘’ 字的老师信息 select * from teacher where name like '%尘%';-- 查问姓名是三个字的 select * from teacher where name like '___';-- 查问姓 '小' 的老师信息 select * from teacher where name like '小%';-- 查问名字中含有下划线的老师信息 '\' 本义-- select * from teacher where name like '%_%'; # 谬误 select * from teacher where name like '%\_%';
<3> 分组查问
语法格局:
- [group by 列名] [haveing 条件]
个别状况分组查问联合聚合函数一起应用
- max()
- min()
- sum()
- avg()
- count()
-- 查问每个部门的平居薪资 # select * from teacher GROUP BY dname # 记住:分组的正确应用形式,group by 前面没有呈现的列名不能呈现在select 和from 的两头, # 尽管不报错,然而不是分组的正确应用形式 # 聚合函数中呈现的列名group by前面没有无所谓 select dname from teacher GROUP BY dname; select dname, avg(sal) from teacher GROUP BY dname;
<4> 排序查问
语法格局:
- order by 列名 asc|desc 默认升序(asc)
-- 查问老师信息,依据薪资进行排序,要求从大到小进行排序select * from teacher order by sal desc; # 依据sal进行降序排序select * from teacher order by sal asc; # 依据sal进行升序排序select * from teacher order by sal; # 依据sal进行升序排序, 利用默认排序
<5> 限度后果集数量的查问(分页)
编号 商品名称 商品价格 操作
1 玩具娃娃 100.0 删除 批改
2 玩具汽车 200.0 删除 批改
3 玩具飞机 300.0 删除 批改
................................
首页 上一页 1 2 3 4 5 下一页 尾页
语法格局
- limit n条数;-------从第一条开始取n条数据(理解)
- limit start开始下标索引,count条数; ---- 从起始地位start取count条数据(起始地位是从0开始的) (举荐应用)
分页(每页显示两条数据)第一页:select * from teacher limit 0,2;第二页:select * from teacher limit 2,2;第三页:select * from teacher limit 4,2;第四页:select * from teacher limit 6,2;第五页:select * from teacher limit 8,2;
分页公式:
- 开始下标索引(起始地位) = (当前页-1)*每页显示条数;
-- 每页显示3条-- 显示第二页 select * from teacher limit 3,3;
<6> 扩大
- 别名
select * from teacher; # 查问表中所有字段记录select name, sal, dname from teacher; # 查问表中指定字段记录-- 给查问的字段设置别名 同时也能够给表设置别名 通过as 关键字实现别名 select name as '姓名', sal '薪资', dname '部门名称' from teacher
二、 事务管制语言(TCL)
MySQL事务默认主动开启的
- 在MySQL数据库中只有应用了Innodb数据库引擎的数据表或库才会反对事务
- 通过事务来治理 insert、update、delete语句
事务必须满足4个条件(ACID):
- 原子性(不可分割性): 要么全副实现,要么全副不实现,不会完结在两头的某个环节。在执行的过程中一旦呈现谬误/异样,会被回滚(Rollback)到事务开始前的状态,就像这个事务素来没有执行过一样。
- 一致性: 事务处理前后数据保持一致
- 隔离性: 事务处理必须是独立的彼此隔离
- 持久性: 事务对数据的批改永恒保留
<1> 为什么应用事务
- 银行转账
- 事务宽泛应用:订单零碎,银行零碎等....
<2> MySQL事务管制
- commit(提交)
- rollback(回滚)
- savepoint(事务节点)
<3> 实战操作
create table student( id int, name varchar(32), age int, money double);insert into student values(1, '老王', 18, 60000);select * from student;rollback;
手动敞开事务提交语法:
- set autocommit = false|true; // 设置事务的提交形式
- rollback; // 事务回滚
- commit; // 事务提交
- savepoint 节点名称; // 设置回滚的节点
- rollback to 节点名称; // 回滚到具体的某个节点
set autocommit = false; # 设置事务手动提交select * from student;delete from student where id=1; # 删除id为1 的信息rollback; # 事务回滚commit; # 事务提交update student set money = money-30000 where id=1;savepoint t1; # 设置事务节点update student set money = money-20000 where id=1;rollback to t1; # 回滚到t1节点地位commit; # 事务提交