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表的员工姓名和工作地址
-- (如果地址是上海/北京 --> 一线城市,否则 --> 二线
select
    name,
    (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);

select
    id,
    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.id
  and (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.id
  and 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 e1
        where 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年最好的一年,奥力给!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/43202.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

使用Plist编辑器——简单入门指南

本指南将介绍如何使用Plist编辑器。您将学习如何打开、编辑和保存plist文件&#xff0c;并了解plist文件的基本结构和用途。跟随这个简单的入门指南&#xff0c;您将掌握如何使用Plist编辑器轻松管理您的plist文件。 plist文件是一种常见的配置文件格式&#xff0c;用于存储应…

docker - prometheus+grafana监控与集成到spring boot 服务

一、Prometheus 介绍 1.数据收集器&#xff0c;它以配置的时间间隔定期通过HTTP提取指标数据。 2.一个时间序列数据库&#xff0c;用于存储所有指标数据。 3.一个简单的用户界面&#xff0c;您可以在其中可视化&#xff0c;查询和监视所有指标。二、Grafana 介绍 Grafana 是一…

Kubernetes对象深入学习之四:对象属性编码实战

欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码)&#xff1a;https://github.com/zq2599/blog_demos 本篇概览 本文是《Kubernetes对象深入学习》系列的第四篇&#xff0c;前面咱们读源码和文档&#xff0c;从理论上学习了kubernetes的对象相关的知识&#xff…

spring复习:(50)@Configuration注解配置的singleton的bean是什么时候被创建出来并缓存到容器的?

一、主类&#xff1a; 二、配置类&#xff1a; 三、singleton bean的创建流程 运行到context.refresh(); 进入refresh方法&#xff1a; 向下运行到红线位置时&#xff1a; 会实例化所有的singleton bean.进入finisheBeanFactoryInitialization方法&#xff1a; 向下拖动代…

(双指针) 剑指 Offer 57 - II. 和为s的连续正数序列 ——【Leetcode每日一题】

❓ 剑指 Offer 57 - II. 和为s的连续正数序列 难度&#xff1a;简单 输入一个正整数 target &#xff0c;输出所有和为 target 的连续正整数序列&#xff08;至少含有两个数&#xff09;。 序列内的数字由小到大排列&#xff0c;不同序列按照首个数字从小到大排列。 示例 1…

【CMU15-445 FALL 2022】Project #1 - Buffer Pool

About 实验官网 Project #1 - Buffer Pool在线评测网站 gradescope Lab Task #1 - Extendible Hash Table 详见——【CMU15-445 FALL 2022】Project #1 - Extendable Hashing 如果链接失效&#xff0c;请查看当前平台我之前发布的文章。 Task #2 - LRU-K Replacement Polic…

Hadoop——DataGrip连接MySQL|Hive

1、下载 DataGrip下载&#xff1a;DataGrip: The Cross-Platform IDE for Databases & SQL by JetBrains 2、破解 破解链接&#xff1a;https://www.cnblogs.com/xiaohuhu/p/17218430.html 3、启动环境 启动Hadoop&#xff1a;到Hadoop的sbin目录下右键管理员身份运行…

计算机服务器被devos勒索病毒攻击怎么解决,数据库解密恢复方式

科学技术的发展为企业的生产运行提供了极大的便利性&#xff0c;但随之而来的网络安全也应该引起人们的重视。近期&#xff0c;我们收到很多企业的求助&#xff0c;企业的计算机服务器内的数据库被devos后缀勒索病毒攻击&#xff0c;导致企业许多工作无法正常运行。Devos后缀勒…

每天五分钟计算机视觉:单卷积层的前向传播过程

什么是单卷积层? 一张图片(输入)经过多个卷积核卷积就会得到一个输出,而这多个卷积核的组合就是一个单卷积层。 这些卷积核可能大小是不一样的,但是他们接收同样大小是输入,他们的输出必须是一般大小,所以不同的卷积核需要具备不同的步长和填充值。 单层卷积网络前向传…

大数据分析案例-基于LightGBM算法构建乳腺癌分类预测模型

&#x1f935;‍♂️ 个人主页&#xff1a;艾派森的个人主页 ✍&#x1f3fb;作者简介&#xff1a;Python学习者 &#x1f40b; 希望大家多多支持&#xff0c;我们一起进步&#xff01;&#x1f604; 如果文章对你有帮助的话&#xff0c; 欢迎评论 &#x1f4ac;点赞&#x1f4…

微服务如何治理

微服务远程调用可能有如下问题&#xff1a; 注册中心宕机&#xff1b; 服务提供者B有节点宕机&#xff1b; 服务消费者A和注册中心之间的网络不通&#xff1b; 服务提供者B和注册中心之间的网络不通&#xff1b; 服务消费者A和服务提供者B之间的网络不通&#xff1b; 服务提供者…

安装redis,适配阿里云服务器,Liunx安装redis

下载redis以及编译安装 下载redis文件 wget http://download.redis.io/releases/redis-6.0.8.tar.gz #下载redis压缩文件 tar xzf redis-6.0.8.tar.gz #解压缩 cd redis-6.0.8 make 查看是否安装了gcc编译输入gcc --version如果没有…

数据仓库表设计理论

数据仓库表设计理论 数仓顾名思义是数据仓库&#xff0c;其数据来源大多来自于业务数据(例如:关系型数据库)&#xff0c;当设计数仓中表类型时(拉链表、增量表、全量表、流水表、切片表)时&#xff0c;应先观察业务数据的特点再设计数仓表结构 首先业务数据是会不断增长的-即…

STM32外设系列—TB6612FNG

本文涉及到定时器和串口的知识&#xff0c;详细内容可见博主STM32速成笔记专栏。 文章目录 一、TB6612简介二、TB6612使用方法2.1 TB6612引脚连接2.2 控制逻辑2.3 电机调速 三、实战项目3.1 项目简介3.2 初始化GPIO3.3 PWM初始化3.3 电机控制程序3.4 串口接收处理函数 一、TB66…

基于SPDK-vhost的云原生Kubevirt虚拟化存储IO的优化方案

摘要 本文主要介绍针对云原生kubernetes虚拟化IO的应用场景&#xff0c;在Kubevirt中引入SPDK-vhost的支持&#xff0c;来加速虚机中IO存储性能。同时基于Intel开源的Workload Service Framework[1]平台集成部署一套端到端虚拟化IO的应用场景做基本的性能对比测试。 云原生Kube…

Hadoop——Windows系统下Hadoop单机环境搭建

为了便于开发&#xff0c;我在本地Windows系统进行Hadoop搭建。 我使用的版本&#xff1a;hadoop-2.7.0。其他版本也可&#xff0c;搭建流程基本一样&#xff0c;所以参考这个教程一般不会有错。 1、下载安装包和插件 安装包hadoop-2.7.0.tar.gz 必要插件winutils-master 2、…

RocketMQ教程-安装和配置

Linux系统安装配置 64位操作系统&#xff0c;推荐 Linux/Unix/macOS 64位 JDK 1.8 Maven3.0 yum 安装jdk8 yum 安装maven 1.下载安装Apache RocketMQ RocketMQ 的安装包分为两种&#xff0c;二进制包和源码包。 点击这里 下载 Apache RocketMQ 5.1.3的源码包。你也可以从这…

详细介绍Matlab中线性规划算法的使用

Matlab中提供了用于线性规划的优化工具箱&#xff0c;其中包含了多种算法&#xff0c;如单纯形法、内点法等。线性规划是一种优化问题&#xff0c;旨在找到一组变量的最佳值&#xff0c;以最大化或最小化线性目标函数&#xff0c;同时满足一组线性约束条件。 下面将详细介绍Ma…

【Spring Boot】事务的隔离级别与事务的传播特性详解:如何在 Spring 中使用事务?不同隔离级别的区别?

文章目录 1 事务1.1 事务简介与 mysql 中的事务使用1.2 Spring 编程式事务&#xff08;手动操作&#xff09;1.3 Spring 声明式事务&#xff08;自动操作&#xff09;1.4 Transactional 的工作原理 2 事务的隔离级别2.1 事务的四大特性及事务的隔离级别回顾2.2 Spring 事务的隔…

python web开发之WSGI/uwsgi/uWSGI详解

1. 三者的定义 WSGI是一种通信协议。uwsgi是一种传输协议。uWSGI是实现了uwsgi和WSGI两种协议的Web服务器。 2.三者的使用场景 WSGI&#xff0c;全称 Web Server Gateway Interface&#xff0c;是为 Python 语言定义的 Web 服务器和 Web 应用程序或框架之间的一种简单而通用的接…
最新文章