网站建设设计公司类网站织梦模板 带手机端门户网站
👂 回到夏天(我多想回到那个夏天) - 傲七爷/小田音乐社 - 单曲 - 网易云音乐
截图自 劈里啪啦 -- 黑马Mysql,仅学习使用
👇原地址
47. 基础-多表查询-表子查询_哔哩哔哩_bilibili
目录
🦂函数
🌳字符串函数
🌳数值函数
🌳日期函数
🌳流程函数
🌳小结
🦂约束
🍈概述
🍈演示
🍈外键约束
🍈外键删除更新行为
🍈小结
🦂多表查询
🐱多表关系
🐱概述
🐱内连接
🐱外连接
🐱自连接
🐱联合查询union
🐱子查询
🐱标量子查询
🐱列子查询
🐱行子查询
🐱表子查询
🐱练习1
🐱练习2
🐱小结
🦂事务
🐟简介
🐟操作演示
🐟四大特性ACID
🐟并发事务问题
🐟并发事务演示及隔离级别
🐟小结
🌼总结
🦂函数
🌳字符串函数
-- ------------------函数演示-----------------------
-- concat
select concat('Hello', ' MySQL');-- lower
select lower('HEllo');-- upper
select upper('HEllo');-- lpad
select lpad('hello', 3, 'as');
select lpad('hello', 10, 'as');-- rpad
select rpad('haha', 2, 'UVA');
select rpad('haha', 11, 'UVA');-- trim
select trim(' haha ');
select trim(' Hello Mysql hahaha ');-- substring
-- 索引从1开始
select substring('Hello, Man, MySQL haha', 1, 9);-- 1.工号统一为5位数,不足5位前补0
update emp set workno = lpad(workno, 5, '0');
👆
🌳数值函数
-- 数值函数
-- ceil
select ceil(1.5);
select ceil(1.1);-- floor
select floor(1.1);
select floor(1.9);-- mod
select mod(3, 4);
select mod(5, 4);-- rand
select rand();-- round
select round(2.345, 2);
select round(2.3495, 3);
select round(2.3495, 1);-- 案例:通过数据库函数,生成一个六位随机验证码
-- 有bug,可能生成5位数,需要补0
select round(rand()*1000000, 0); #保留0位小数
-- rand()0~1随机数, round()四舍五入保留0位小数, lpad左填充
select lpad(round(rand()*1000000, 0), 6, '0');
🌳日期函数
-- 日期函数
-- curdate()
select curdate();-- curtime()
select curtime();-- now()
select now();-- year, month, day
select year(now());
select month(now());
select day(now());-- date_add()
select date_add(now(), interval 51 day);
select date_add(now(), interval 51 month);
select date_add(now(), interval 51 year);-- datediff 前 - 后
select datediff('2023-9-1', '2023-7-11');-- 案例:查询所有员工入职天数,并根据入职天数倒序排序
select name, datediff(curdate(), entrydate) from emp;
select name, datediff(curdate(), entrydate) as 'entrydays' from emp order by entrydays desc;
🌳流程函数
-- 流程控制函数
-- if
select if(true, 'ok', 'error');
select if(false, 'ok', 'error');-- ifnull
select ifnull('ok', 'default');
select ifnull('', 'default');
select ifnull(null, 'default');-- case when then else end
-- 查询emp表的员工姓名和工作地址
-- (如果地址是上海/北京 --> 一线城市,否则 --> 二线
selectname,(case workaddress when '北京' then '一线城市' when '上海' then '一线城市' else '二线城市' end)
from emp;-- 案例:统计各个学员的成绩
-- >= 85,优秀
-- >= 60,及格
-- 否则,不及格
create table score(id int comment 'ID',name varchar(20) comment '姓名',math int comment '数学',english int comment '英语',chinese int comment '语文'
) comment '学员成绩表';insert into score(id,name,math,english,chinese) VALUES (1,'Tom',67,88,95),(2,'Rose',23,66,90),(3,'Jack',56,98,76);selectid,name,(case when math >= 85 then '优秀' when math >= 60 then '及格' else '不及格' end) '数学',(case when english >= 85 then '优秀' when english >= 60 then '及格' else '不及格' end) '英语',(case when chinese >= 85 then '优秀' when chinese >= 60 then '及格' else '不及格' end) '语文'
from score;
🌳小结
🦂约束
🍈概述
🍈演示
-- 约束演示
create table user(id int primary key auto_increment comment '主键',name varchar(10) not null unique comment '姓名',age int check (age > 0 && age <= 120) comment '年龄',status char(1) default '1' comment '状态',gender char(1) comment '性别'
) comment '用户表';
-- 插入数据
insert into user(name, age, status, gender)values('Tom1',19,'1','男'),('Tom2',25,'0','男');
insert into user(name, age, status, gender)values('Tom3',19,'1','男')insert into user(name, age, status, gender)values(null,19,'1','男'); #错误,非空
insert into user(name, age, status, gender)values('Tom3',19,'1','男'); #错误,重复insert into user(name, age, status, gender)values('Tom4',80,'1','男');
insert into user(name, age, status, gender)values('Tom4',-1,'1','男'); #错误,无效值年龄
insert into user(name, age, status, gender)values('Tom4',121,'1','男'); #错误,无效值年龄insert into user(name, age, gender)values('Tom5',120,'男'); #status采取默认值
🍈外键约束
use itheima;select database();create table dept(id int auto_increment comment 'ID' primary key,name varchar(50) not null comment '部门名称'
)comment '部门表';
insert into dept (id, name) values (1, '研发部'),(2, '市场部'),(3, '财务部'),(4, '销售部'),(5, '总经办');rename table emp to emp2;create table emp(id int auto_increment comment 'ID' primary key,name varchar(50) not null comment '姓名',age int comment '年龄',job varchar(20) comment '职位',salary int comment '薪水',entrydate date comment '入职时间',managerid int comment '直属领导ID',dept_id int comment '部门ID'
)comment '员工表';insert into emp(id, name, age, job, salary, entrydate, managerid, dept_id) values(1,'金庸',66,'总裁',20000,'2000-01-01',null,5),(2,'张无忌',20,'项目经理',12500,'2005-12-05',1,1),(3,'杨逍',33,'开发',8400,'2000-11-03',2,1),(4,'韦一笑',48,'开发',11000,'2002-02-05',2,1),(5,'常遇春',43,'开发',10500,'2004-09-07',3,1),(6,'小昭',19,'程序员鼓励师',6600,'2004-10-12',2,1);-- 添加外键
alter table emp add constraint fk_emp_dept_id foreign key (dept_id) references dept(id);-- 删除外键
alter table emp drop foreign key fk_emp_dept_id;
🍈外键删除更新行为
create table dept(id int auto_increment comment 'ID' primary key,name varchar(50) not null comment '部门名称'
)comment '部门表';insert into dept (id, name) values (1, '研发部'),(2, '市场部'),(3, '财务部'),(4, '销售部'),(5, '总经办');create table emp(id int auto_increment comment 'ID' primary key,name varchar(50) not null comment '姓名',age int comment '年龄',job varchar(20) comment '职位',salary int comment '薪水',entrydate date comment '入职时间',managerid int comment '直属领导ID',dept_id int comment '部门ID'
)comment '员工表';insert into emp(id, name, age, job, salary, entrydate, managerid, dept_id) values(1,'金庸',66,'总裁',20000,'2000-01-01',null,5),(2,'张无忌',20,'项目经理',12500,'2005-12-05',1,1),(3,'杨逍',33,'开发',8400,'2000-11-03',2,1),(4,'韦一笑',48,'开发',11000,'2002-02-05',2,1),(5,'常遇春',43,'开发',10500,'2004-09-07',3,1),(6,'小昭',19,'程序员鼓励师',6600,'2004-10-12',2,1);-- 外键的删除和更新行为
alter table emp add constraint fk_emp_dept_id foreign key (dept_id) references dept(id) on update cascade on delete cascade;alter table emp add constraint fk_emp_dept_id foreign key (dept_id) references dept(id) on update set null on delete set null;
🍈小结
🦂多表查询
🐱多表关系
-- -----------多表关系 演示----------------use itheima;-- 多对多
create table student(id int auto_increment primary key comment '主键ID',name varchar(10) comment '姓名',no varchar(10) comment '学号'
) comment '学生表';
insert into student values (null, '黛绮丝', '2000100101'),(null, '谢逊', '2000100102'),(null, '殷天正', '2000100103'),(null,'韦一笑','2000100104');create table course(id int auto_increment primary key comment '主键ID',name varchar(10) comment '课程名称'
) comment '课程表';
insert into course values (null, 'Java'),(null,'PHP'),(null,'MySQL'),(null,'Hadoop');-- 中间表 维护多对多关系
create table student_course(id int auto_increment primary key comment '主键',studentid int not null comment '学生ID',courseid int not null comment '课程ID',-- constraint 外键名 foreign key (外键字段) references 主表名(主表字段)constraint fk_courseid foreign key (courseid) references course (id),constraint fk_studentid foreign key (studentid) references student (id)
) comment '学生课程中间表';insert into student_course values (null,1,1),(null,1,2),(null,1,3),(null,2,2),(null,2,3),(null,3,4);
右键中间表 -- 底部diagrams -- show visualization -- 查看多表关系的可视化界面👇
-- 一对一create table tb_user(id int auto_increment primary key comment '主键ID',name varchar(10) comment '姓名',age int comment '年龄',gender char(1) comment '1: 男 , 2: 女',phone char(11) comment '手机号'
) comment '用户基本信息表';create table tb_user_edu(id int auto_increment primary key comment '主键ID',degree varchar(20) comment '学历',major varchar(50) comment '专业',primaryschool varchar(50) comment '小学',middleschool varchar(50) comment '中学',university varchar(50) comment '大学', # unique保证一对一userid int unique comment '用户ID', # userid是外键,关联tb_user的主键-- constraint 外键名 foreign key (外键字段) references 主表名(主表字段)constraint fk_userid foreign key (userid) references tb_user(id)
) comment '用户教育信息表';insert into tb_user(id,name,age,gender,phone) values(null,'黄渤',45,'1','18800001111'),(null,'冰冰',35,'2','18800002222'),(null,'码云',55,'1','18800008888'),(null,'李彦宏',55,'1','18800009999');insert into tb_user_edu(id,degree,major,primaryschool,middleschool,university,userid) values(null,'本科','舞蹈','静安区第一小学','静安区第一中学','北京舞蹈学院',1),(null,'硕士','表演','朝阳区第一小学','朝阳区第一中学','北京电影学院',2),(null,'本科','英语','杭州市第一小学','杭州市第一中学','杭州师范大学',3),(null,'本科','应用数学','阳泉第一小学','阳泉第一中学','清华大学',4);
🐱概述
-- ------------------------------------> 多表查询 <--------------------------------------------
-- 准备数据
create table dept(id int auto_increment comment 'ID' primary key,name varchar(50) not null comment '部门名称'
)comment '部门表';create table emp(id int auto_increment comment 'ID' primary key,name varchar(50) not null comment '姓名',age int comment '年龄',job varchar(20) comment '职位',salary int comment '薪资',entrydate date comment '入职时间',managerid int comment '直属领导ID',dept_id int comment '部门ID'
)comment '员工表';-- 添加外键
-- constraint 外键名 foreign key (外键字段) references 主表名(主表字段)
alter table emp add constraint fk_emp_dept_id foreign key (dept_id) references dept(id);INSERT INTO dept (id, name) VALUES (1, '研发部'), (2, '市场部'),(3, '财务部'), (4, '销售部'), (5, '总经办'), (6, '人事部');
INSERT INTO emp (id, name, age, job,salary, entrydate, managerid, dept_id) VALUES(1, '金庸', 66, '总裁',20000, '2000-01-01', null,5),(2, '张无忌', 20, '项目经理',12500, '2005-12-05', 1,1),(3, '杨逍', 33, '开发', 8400,'2000-11-03', 2,1),(4, '韦一笑', 48, '开发',11000, '2002-02-05', 2,1),(5, '常遇春', 43, '开发',10500, '2004-09-07', 3,1),(6, '小昭', 19, '程序员鼓励师',6600, '2004-10-12', 2,1),(7, '灭绝', 60, '财务总监',8500, '2002-09-12', 1,3),(8, '周芷若', 19, '会计',48000, '2006-06-02', 7,3),(9, '丁敏君', 23, '出纳',5250, '2009-05-13', 7,3),(10, '赵敏', 20, '市场部总监',12500, '2004-10-12', 1,2),(11, '鹿杖客', 56, '职员',3750, '2006-10-03', 10,2),(12, '鹤笔翁', 19, '职员',3750, '2007-05-09', 10,2),(13, '方东白', 19, '职员',5500, '2009-02-12', 10,2),(14, '张三丰', 88, '销售总监',14000, '2004-10-12', 1,4),(15, '俞莲舟', 38, '销售',4600, '2004-10-12', 14,4),(16, '宋远桥', 40, '销售',4600, '2004-10-12', 14,4),(17, '陈友谅', 42, null,2000, '2011-10-12', 1,null);-- 多表查询
select * from emp; #单表查询
select * from emp, dept; # 6 * 17 = 102 笛卡尔积
-- 消除笛卡尔积 无效字段
select * from emp, dept where emp.dept_id = dept.id;
🐱内连接
use itheima;
-- 内连接演示
-- 1.查询每一个员工姓名,及关联的部门名称(隐式内连接)
-- 表结构:emp, dept
-- 连接条件:emp.dept_id = dept.id(外键)
select * from emp, dept where emp.dept_id = dept.id;
select emp.name, dept.name from emp, dept where emp.dept_id = dept.id;
#起别名后,就不能通过表名来操作
select e.name, d.name from emp e, dept d where e.dept_id = d.id; #起别名-- 2.查询每一个员工姓名,及关联的部门名称(显示内连接)--- INNER JOIN...ON...
select e.name, d.name from emp e inner join dept d on e.dept_id = d.id;
select e.name, d.name from emp e join dept d on e.dept_id = d.id;
🐱外连接
-- 外连接演示
-- 1.查询emp表的所有数据,和对应的部门信息(左外连接)
-- 表结构:emp, dept
-- 连接条件:emp.dept_id = dept.id
select * from emp e left outer join dept d on e.dept_id = d.id;
select e.*, d.name from emp e left outer join dept d on e.dept_id = d.id;
select e.*, d.name from emp e left join dept d on e.dept_id = d.id;-- 2.查询dept表的所有数据,和对应的员工信息(右外连接)
#右外连接,会完全包含右表的数据
select d.*, e.* from emp e right join dept d on e.dept_id = d.id;
#用左外连接实现相同需求 ↓
select d.*, e.* from dept d left join emp e on e.dept_id = d.id;
🐱自连接
-- 自连接
-- 1.查询员工 及其 所属领导的名字
-- 表结构:emp
#将同一张表,看成,2张表
#自连接必须给表起别名
select * from emp a, emp b where a.managerid = b.id;
#将a的外键managerid,指向,b的主键id
select a.name, b.name from emp a, emp b where a.managerid = b.id;-- 2.查询所有员工 emp 及其领导的名字 emp,如果员工没有领导,也需要查询出来
# 需完全包含左表 所以用 外连接
-- 表结构:emp a, emp b
select * from emp a left join emp b on a.managerid = b.id;
select a.name '员工', b.name '领导' from emp a left join emp b on a.managerid = b.id;
🐱联合查询union
将结果集合并
-- union all ,union
-- 1.薪资低于5000的员工,和年龄大于50岁的,全部查询出来
select * from emp e where salary < 5000
union all
select * from emp e where age > 50;-- 合并后去重 去掉all(鹿杖客)
select * from emp e where salary < 5000
union
select * from emp e where age > 50;-- 联合查询的 列数 需要一致,select后查询内容的列数要一样
select name, age, salary from emp e where salary < 5000
union
select name, age, salary from emp e where age > 50;
🐱子查询
🐱标量子查询
-- ------------------------ 子查询 --------------------------
-- 标量子查询-- 1.查询“销售部”所有员工信息
-- a.查询“销售部“部门ID
select id from dept where name = '销售部';-- b.根据销售部ID,查询员工信息
select * from emp where dept_id = 4;
-- 合二为一查询
select * from emp where dept_id = (select id from dept where name = '销售部');-- 2.查询“方东白”之后入职的员工信息
select * from emp where entrydate > (select entrydate from emp where name = '方东白');
🐱列子查询
-- 列子查询-- 1.查询”销售部”和“市场部”所有员工信息
-- a.销售部 和 市场部 部门ID
select id from dept where name = '销售部' or name = '市场部';
-- b.根据部门ID查询信息
select * from emp where dept_id in (select id from dept where name = '销售部' or name = '市场部');-- 2.查询比财务部人工资都高的员工信息
-- a.所有财务部的人员工资
select id from dept where name = '财务部';
select salary from emp where dept_id = (select id from dept where name = '财务部');
-- b.比 财务部 所有人员工资高的员工信息
select * from emp where salary > all (select salary from emp where dept_id = (select id from dept where name = '财务部'));-- 3.查询比研发部其中任一人工资高的员工信息
select * from emp where salary > any (select salary from emp where dept_id = (select id from dept where name = '研发部'));
🐱行子查询
-- 行子查询
-- 1.查询与“张无忌”的薪资及直属领导相同的员工信息
-- a.查询“张无忌”的薪资及直属领导
select salary, managerid from emp where name = '张无忌';-- b.查询与“张无忌”薪资及直属领导相同的员工信息
select * from emp where salary = 12500 and managerid = 1;
select * from emp where (salary, managerid) = (12500, 1);
select * from emp where (salary, managerid) = (select salary, managerid from emp where name = '张无忌');
🐱表子查询
表 子查询,返回多行多列的数据
-- 表 子查询-- 1.查询与“鹿杖客”,“宋远桥”的职位和薪资相同的员工信息
-- a.查询“鹿杖客” “宋远桥”的职位和薪资
select job, salary from emp where name = '鹿杖客' or name = '宋远桥';
-- b.查询他俩的职位和员工信息
select * from emp where (job, salary) in (select job, salary from emp where name = '鹿杖客' or name = '宋远桥');-- 2.查询入职日期是“2006-01-01”后的员工信息和部门信息
-- a.入职日期 ... 之后的员工信息
select * from emp where entrydate > '2006-01-01';
-- b.查询这部分员工,对应的部门信息
# a中子查询的结果作为临时表存在 from(...)
select * from (select * from emp where entrydate > '2006-01-01') e left join dept d on e.dept_id = d.id;
select e.*, d.* from (select * from emp where entrydate > '2006-01-01') e left join dept d on e.dept_id = d.id;
🐱练习1
重点是第5个需求👇
use itheima;
-- ------------------> 多表查询案例 <-----------------------
create table salgrade(grade int,losal int, #low salary 下限hisal int #hith salsary 上限
) comment '薪资等级表';insert into salgrade values (1,0,3000); #等级1 0-3000
insert into salgrade values (2,3001,5000);
insert into salgrade values (3,5001,8000);
insert into salgrade values (4,8001,10000);
insert into salgrade values (5,10001,15000);
insert into salgrade values (6,15001,20000);
insert into salgrade values (7,20001,25000);
insert into salgrade values (8,25001,30000);-- 1.查询员工姓名,年龄,职位,部门信息(隐式内连接)
-- emp, dept
-- 连接条件:emp.dept_id = dept.id
select * from emp e, dept d where e.dept_id = d.id;
select e.name, e.age, e.job, d.name from emp e, dept d where e.dept_id = d.id;-- 2.查询年龄小于30岁的员工姓名,年龄,职位和部门信息(显式内连接)
select e.name, e.age, e.job, d.name from emp e join dept d on e.dept_id = d.id where e.age < 30;-- 3.查询拥有员工的部门ID,部门名称
-- 哪些部门下有员工 -> 2个表的交集 -> 内连接
select d.id, d.name from emp e, dept d where e.dept_id = d.id;
# distinct 结果去重
select distinct d.id, d.name from emp e, dept d where e.dept_id = d.id;-- 4.查询所有年龄大于40岁的员工,及其归属的部门名称;若未分配部门,也需要显示
# '陈友谅'没有部门, 也需要展示, 使用外连接
-- 表:emp, dept
-- 连接条件:emp.dept_id = dept.id
select e.*, d.name from emp e left join dept d on e.dept_id = d.id where e.age > 40;-- 5.查询所有员工的工资等级
-- 表:emp, salgrade
-- 连接条件:emp.salary >= salgrade.losal and emp.salary <= salgrade.hisal
select e.name, e.salary, s.grade, s.losal, s.hisal from emp e, salgrade s where e.salary >= s.losal and e.salary <= s.hisal;
select e.name, e.salary, s.grade, s.losal, s.hisal from emp e, salgrade s where e.salary between s.losal and s.hisal;
🐱练习2
小tips
当表越来越多,sql语句越来越长,为了方便查看,我们借助Datagrip
右键选中的语句 Reformat code,格式化语句👇
需求12,学生与课程,多对多的关系
-- 6.查询 “研发部” 所有员工的 信息 及 工资等级
-- 表:emp, salgrade, dppt
# 联查 n 张 表,至少有 n - 1个连接条件
-- 连接条件1:e.salary between s.losal and s.hisal
-- 连接条件2:e.dept_id = d.id
-- 查询条件:d.name = '研发部'
select e.*, s.grade
from emp e,dept d,salgrade s
where e.dept_id = d.idand (e.salary between s.losal and s.hisal)and d.name = '研发部';-- 7.查询 “研发部” 员工 平均工资
-- 表:emp, dept
-- 连接条件:e.dept_id = d.id
# 平均用聚合函数 avg()
select avg(e.salary)
from emp e,dept d
where e.dept_id = d.idand d.name = '研发部';-- 8.查询工资比 “灭绝” 高的员工信息
-- a.查询 ‘灭绝’ 的薪资
select salary
from emp
where name = '灭绝';
-- b.查询比 她 工资 高的员工数据
select *
from emp
where salary > 8500;
# 标量子查询
select *
from emp
where salary > (select salary from emp where name = '灭绝');-- 9.查询比平均薪资高的员工信息
-- a.查询员工 平均薪资
select avg(salary)
from emp;
-- b.查询比 平均工资 高的员工信息
select *
from emp
where salary > (select avg(salary) from emp);-- 10.查询低于本部门 平均工资 的员工信息
-- a.查询指定部门 平均工资
select avg(e1.salary)
from emp e1
where e1.dept_id = 1;
select avg(e1.salary)
from emp e1
where e1.dept_id = 2;
-- b.查询 低于 本部门 平均工资 的员工信息
# e2.dept_id 表示当前部门, e1
select *,(select avg(e1.salary)from emp e1where e1.dept_id = e2.dept_id)
from emp e2
where e2.salary < (select avg(e1.salary) from emp e1 where e1.dept_id = e2.dept_id);-- 11.查询所有部门信息,并统计部门的员工人数
# select后的子查询
select d.id, d.name, (select count(*) from emp e where e.dept_id = d.id) '人数'
from dept d;
select count(*)
from emp
where dept_id = 1;-- 12.查询所有学生的选课情况,展示出学生名称,学号,课程名称
-- 表:student, course, student_course
# 学生与课程 多对多 中间表至少包含2个外键 分别关联两方主键
-- 连接条件:student.id = student_course.studentid, course.id = student_course.courseid
select s.name, s.no, c.name
from student s,student_course sc,course c
where s.id = sc.studentid #连接条件 消除无效笛卡尔积and sc.courseid = c.id;
🐱小结
🦂事务
🐟简介
🐟操作演示
方式一
方式二
use itcast;
-- -------------------- 事务操作 ----------------------
-- 数据准备
create table account(id int auto_increment primary key comment '主键ID',name varchar(10) comment '姓名',money int comment '余额'
) comment '账户表';
insert into account(id, name, money) values (null,'张三',2000),(null,'李四',2000);-- 恢复数据
update account set money = 2000 where name = '张三' or name = '李四';select @@autocommit; # 默认自动提交,返回1
set @@autocommit = 1; # 设置为0手动1自动提交-- 转账操作
-- 1.查询张三账户余额
select * from account where name = '张三';-- 2.将张三账户余额 - 1000
update account set money = money - 1000 where name = '张三';程序执行出错...
-- 3.李四账户余额 + 1000
update account set money = money + 1000 where name = '李四';-- 提交事务
commit;-- 回滚事务
rollback;-- 方式二 ------------------------
start transaction; #开启事务, 手动控制select * from account where name = '张三';
update account set money = money - 1000 where name = '张三';
程序执行出错...
update account set money = money + 1000 where name = '李四';-- 提交事务
commit;
-- 回滚事务
rollback;
🐟四大特性ACID
原子,一致
隔离
持久
🐟并发事务问题
脏读
不可重复读
幻读
🐟并发事务演示及隔离级别
(事务隔离级别 UP↑) (效率 DOWN↓)
用2个cmd模拟客户端,2个并发事务的操作(一个cmd代表一个独立的事务)
提示:Windows -- cmd可以按上下键,快速切换历史指令
Read uncommitted -- 脏读 √
-- cmd一号
mysql -u root -p
123456
use itcast;
set session transaction isolation level read uncommitted; #设置事务隔离级别
select * from account;
start transaction; #开启事务
select * from account;-- cmd二号
mysql -u root -p
123456
use itcast;
start transaction; #开启事务
update account set money = money - 1000 where name = '张三'; #修改字段-- cmd一号
select * from account;-- cmd二号
commit;-- cmd一号
select * from account;
commit;
Read committed 脏读 -- ×
-- cmd 1
mysql -u root -p
******
use itcast;
set session transaction isolation level read committed; #设置隔离级别
start transaction; #开启事务
select * from account;-- cmd 2
start transaction;
update account set money = money - 1000 where name = '张三';-- cmd 1
select * from account; #未改变-- cmd 2
commit; #提交后才能在cmd 1中查到变化-- cmd 1
select * from account; #已改变
commit;
...... --> 55. 基础-事务-并发事务演示及隔离级别_哔哩哔哩_bilibili
幻读
mysql> insert into account(id, name, money) values (3,'大刀王五',2000);
ERROR 1062 (23000): Duplicate entry '3' for key 'account.PRIMARY' #插入报错重复已有
mysql> select * from account where id = 3;
Empty set (0.00 sec) #查询又说没有
🐟小结
🌼总结
黑马Mysql基础篇告一段落,后续要学习进阶和运维的内容,进阶里包含了索引,SQL优化,InnoDB的内容。
考虑到字节青训营的项目,应该是学完索引优化就行了,还不需要接触InnoDB和运维
下步关于Mysql的学习,决定,将《Mysql必知必会》1~21章(视图之前)速刷,对应黑马Mysql基础篇的内容
值得一提的是,《Mysql必知必会》还讲到了正则表达式,这点是黑马Mysql所欠缺
总之,查漏补缺,互相印证,放18,19,20年,弄懂《Mysql必知必会》,再跟一遍Mysql黑马这样的一套视频,Mysql,有个增删改查的项目,Mysql这块就是不是问题了,可22,23的形势来看,你还得会InnoDB和运维的内容
今年是过去10年最差的一年,但也是未来10年最好的一年,奥力给!