Mysql基础(下)之函数,约束,多表查询,事务
👂 回到夏天(我多想回到那个夏天) - 傲七爷/小田音乐社 - 单曲 - 网易云音乐
截图自 劈里啪啦 -- 黑马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年最好的一年,奥力给!
相关文章:

Mysql基础(下)之函数,约束,多表查询,事务
👂 回到夏天(我多想回到那个夏天) - 傲七爷/小田音乐社 - 单曲 - 网易云音乐 截图自 劈里啪啦 -- 黑马Mysql,仅学习使用 👇原地址 47. 基础-多表查询-表子查询_哔哩哔哩_bilibili 目录 🦂函数 dz…...

Android 屏幕适配各种宽高比的手机
由于android 手机的屏幕宽高比样式太多了,在设计UI时,很多时候,会因为宽高比,分辨率不同会有展示上的差异。 我是这样解决的 在activity的onCreate方法前,调用: fun screenFit(context: Context) {val me…...

云计算——云计算与虚拟化的关系
作者简介:一名云计算网络运维人员、每天分享网络与运维的技术与干货。 座右铭:低头赶路,敬事如仪 个人主页:网络豆的主页 目录 前言 一.虚拟化 1.什么是虚拟化 2.虚拟化技术作用 二.云计算与虚拟化的关系 三.虚…...

手机变局2023:一场瞄准产品和技术的“思维革命”
以折叠屏冲高端,已成为中国手机厂商们的共识。 在这个苹果未涉足的领域,国产手机厂商们加快脚步迭代推新,积极抢占机遇。但平心而论,虽然国产折叠屏机型众多,但市场上始终缺乏一款突破性的产品作为标杆,为…...

【Linux】自动化构建工具-make/Makefile详解
前言 大家好吖,欢迎来到 YY 滴 Linux系列 ,热烈欢迎!本章主要内容面向接触过Linux的老铁,主要内容含 欢迎订阅 YY 滴Linux专栏!更多干货持续更新!以下是传送门! 订阅专栏阅读:YY的《…...

1 js嵌入html使用
1.1 直接在html内部使用js代码 使用script标签,在前后标签内部写的代码即为js代码。 <body><p id"p1">初始段落</p> <!--id是为了定位需要更改内容的标签--><button type"button" onclick"showNum()">…...

总结RoctetMQ
RoctetMQ 定义优缺点场景使用方式消息顺序问题死信幂等性可视化面板 定义 优缺点 场景 使用方式 消息顺序问题 死信 幂等性 可视化面板...

命名约定~
1.变量的命名约定 JavaScript 变量名称是区分大小写的,大写和小写字母是不同的。比如: let DogName Scooby-Doo; let dogName Droopy; let DOGNAME Odie; console.log(DogName); // "Scooby-Doo" console.log(dogName); // "Dro…...

Python基础-列表(list)和元组(tuple)
Python包含6种内建的序列:列表,元组,字符串,Unicode字符串,buffer对象,xrange对象,本文讨论列表和元组。 1.列表可以修改,元组则不能修改。 2.几乎在所有的情况下,列表…...

Dubbo介绍及使用
🍓 简介:java系列技术分享(👉持续更新中…🔥) 🍓 初衷:一起学习、一起进步、坚持不懈 🍓 如果文章内容有误与您的想法不一致,欢迎大家在评论区指正🙏 🍓 希望这篇文章对你有所帮助,欢…...

初阶C语言-分支和循环语句(下)
“花会沿途盛开,以后的路也是。” 今天我们一起来继续学完分支语句和循环语句。 分支和循环 3.循环语句3.4 do...while()循环3.4.1 do语句的用法 3.5关于循环的一些练习3.6 goto语句 3.循环语句 3.4 do…while()循环 3.4.1 do语句的用法 do循环语句;//当循环语句…...

pytorch工具——pytorch中的autograd
目录 关于torch.tensor关于tensor的操作关于梯度gradients 关于torch.tensor 关于tensor的操作 x1torch.ones(3,3) xtorch.ones(2,2,requires_gradTrue) print(x1,\n,x)yx2 print(y) print(x.grad_fn) print(y.grad_fn)zy*y*3 outz.mean() print(z,out)注意 atorch.randn(2,…...

Linux--进程池
1.一个父进程生成五个子进程且分别建立与子进程管道 ①用for循环,结束条件为<5 ②father父进程每次都要离开for循环,生成下一个子进程和管道 2.#include <cassert>和#include <assert.h>的区别 assert.h 是 C 标准库的头文件ÿ…...

SpringCloudAlibaba微服务实战系列(四)Sentinel熔断降级、异常fallback、block细致处理
SpringCloudAlibaba Sentinel降级和熔断 接着上篇文章的内容,在Sentinel中如何进行降级和熔断呢? 熔断降级规则 降级规则 在Sentinel中降级主要有三个策略:RT、异常比例、异常数,也是针对某个资源的设置。而在1.8.0版本后RT改为…...

WebDAV之π-Disk派盘+ WinSCP
WinSCP是一个免费的开源文件传输应用程序,它使用文件传输协议,安全外壳文件传输协议和安全复制协议来进行纯文件或安全文件传输。该应用程序旨在与Windows一起使用,并支持常见的Windows桌面功能,例如拖放文件,跳转列表…...

Python案例分析|使用Python图像处理库Pillow处理图像文件
本案例通过使用Python图像处理库Pillow,帮助大家进一步了解Python的基本概念:模块、对象、方法和函数的使用 使用Python语言解决实际问题时,往往需要使用由第三方开发的开源Python软件库。 本案例使用图像处理库Pillow中的模块、对象来处理…...

音视频——压缩原理
H264视频压缩算法现在无疑是所有视频压缩技术中使用最广泛, 最流行的。随着 x264/openh264以及ffmpeg等开源库的推出,大多数使用者无需再对H264的细节做过多的研究,这大降低了人们使用H264的成本。 但为了用好H264,我们还是要对…...

微服务 云原生:搭建 K8S 集群
为节约时间和成本,仅供学习使用,直接在两台虚拟机上模拟 K8S 集群搭建 踩坑之旅 系统环境:CentOS-7-x86_64-Minimal-2009 镜像,为方便起见,直接在 root 账户下操作,现实情况最好不要这样做。 基础准备 关…...

C++中的数学问题---进制转换
二进制转十六进制 string binToHex(string bin){string hex"";if(bin.size()%4!0){for(int i0;i<(4-bin.size()%4);i){bin"0"bin;}}for(int i0;i<bin.size();i4){string tmpbin.substr(i,4);bitset<4>b(tmp);hexb.to_ulong()<10?char(b.t…...

开发一个RISC-V上的操作系统(三)—— 串口驱动程序(UART)
目录 文章传送门 一、什么是串口 二、本项目串口的FPGA实现 三、串口驱动程序的编写 四、上板测试 文章传送门 开发一个RISC-V上的操作系统(一)—— 环境搭建_riscv开发环境_Patarw_Li的博客-CSDN博客 开发一个RISC-V上的操作系统(二&…...

nuxt项目部署,npm run build 和npm run generate的区别
每日鸡汤:每个你想要学习的瞬间都是未来的你向自己求救 非服务端渲染的项目,比如普通的vite vue项目,我们在部署生产环境的时候,只需要两步 运行 npm run build 然后得到了一个 dist 文件夹将这个dist文件夹部署到一个静态服务器…...

数据仓库设计理论
数据仓库设计理论 一、数据仓库基本概念 1.1、数据仓库介绍 数据仓库是一个用于集成、存储和分析大量结构化和非结构化数据的中心化数据存储系统。它旨在支持企业的决策制定和业务分析活动。 1.2、基本特征 主题导向:数据仓库围绕特定的主题或业务领域进行建模…...

数据接口有哪些?(数据接口有哪几种)
数据接口是指不同应用程序或系统之间交换数据的通信界面。在现代信息化社会中,数据接口扮演着极为重要的角色,它们使得不同平台之间能够相互连接和交流,从而实现数据共享和应用集成。 数据接口的种类繁多,常见的有以下几种&#…...

华为云CodeArts产品体验的心得体会及想法
文章目录 前言CodeArts 的产品优势一站式软件开发生产线研发安全Built-In华为多年研发实践能力及规范外溢高质高效敏捷交付 功能特性说明体验感受问题描述完结 前言 华为云作为一家全球领先的云计算服务提供商,致力于为企业和个人用户提供高效、安全、可靠的云服务。…...

下载安装:SQLite+SQLiteStudio+VS
目录 1、SQLite 1.1、下载SQLite 1.2、配置SQLite的环境变量 2、SQLite Studio 2.1、下载SQLite Studio 2.2、安装SQLite Studio 3、Visual Studio 3.1、下载Visual Studio 3.2、安装Visual Studio 1、SQLite 1.1、下载SQLite SQLite官网:SQLite Downl…...

nginx路由
一般我们经常在访问网站时,通常会遇到输入某个页面的网址时,出现路由的转发,重定向等。可能访问的是一个网址,出来的时候就显示的是另外的地址。这是由于使用了nginx的缘故,保护了网址的安全性 (1…...

MobPush Android SDK 厂商推送限制
概述 厂商推送限制 每个厂商通道都有对应的厂商配额和 QPS 限制,当请求超过限制且已配置厂商回执时,MobPush会采取以下措施: 当开发者推送请求超过厂商配额时,MobPush将通过自有通道进行消息下发。当开发者推送请求超过厂商 QP…...

计算机网络 day7 扫描IP脚本 - 路由器 - ping某网址的过程
目录 network 和 NetworkManager关系: 实验:编写一个扫描脚本,知道本局域网里哪些ip在使用,哪些没有使用? 使用的ip对应的mac地址都要显示出来 计算机程序执行的两种不同方式: shell语言编写扫描脚本 …...

gitee 配置ssh 公钥(私钥)
步骤1:添加/生成SSH公钥,码云提供了基于SSH协议的Git服务,在使用SSH协议访问项目仓库之前,需要先配置好账户/项目的SSH公钥。 绑定账户邮箱: git config --global user.name "Your Name" git config --glob…...

JAVA面试总结-Redis篇章(七)——数据淘汰策略
JAVA 面试总结-数据淘汰策略...