【MySQL 第十二天 事务的介绍|InnoDB使用事务】

时间:2022-11-02 08:55:08

【MySQL 第十二天 事务的介绍|InnoDB使用事务】

生命不息,学习不止


【1】mysql事物的介绍

【MySQL 第十二天 事务的介绍|InnoDB使用事务】

atomicity 原子性:所有事情是统一的整体,必须一起执行,或者都不执行
consistency:一致性 -》数据需要保证一致性
isolation :隔离性 -》对其他不显示
durability:持久性 

【MySQL 第十二天 事务的介绍|InnoDB使用事务】


【2】mysql事务处理的方法

【1】 rollback:n. 回降;削减;还原;回退
v. 还原;回退
【2】 commit:v. 犯;干;使(人;组织)作出保证;使承担义务;约束;交托;托付;托运
【3】identifier: n. 鉴定人;识别人;鉴定物;识别物;认同者;支持者;标识符
【4】savepoint : 保存点
【5】rollback TO identifier: 回滚到标识符

== 以下几句要深入理解==
【MySQL 第十二天 事务的介绍|InnoDB使用事务】

【1】start transaction : 开始交易
【2】autocommit : 自动提交

【MySQL 第十二天 事务的介绍|InnoDB使用事务】


举例1

-- 创建表 插入数据
create table mytest(
	id int primary key,
	name varchar(30)
);

-- 开始事务
begin;
insert into mytest values(1,'test01');
insert into mytest values(2,'test02');
commit; -- 提交事务

-- 查看
select *from mytest;

-- 插入数据
begin;
insert into mytest values(3,'test03');
rollback;

begin;
insert into mytest values(4,'test04');
savepoint s1; -- 保存点1
insert into mytest values(5,'test05');
savepoint s2;-- 保存点2
insert into mytest values(6,'test06');
rollback to s2; -- 回滚s2  即代码只执行s2 所以test06无法插入
commit;

【MySQL 第十二天 事务的介绍|InnoDB使用事务】


举例2 从不同的平台查看事务 体现事务的隔离性



-- 禁止事务的自动提交
set autocommit = 0;
insert into mytest values(7,'test07');
rollback; -- 事务回滚


set autocommit = 0;
insert into mytest values(8,'test08');


-- 查看
select *from mytest;

【MySQL 第十二天 事务的介绍|InnoDB使用事务】
【MySQL 第十二天 事务的介绍|InnoDB使用事务】

== 解决上面例子,添加提交事务,使得其他平台可见==

-- 禁止事务的自动提交
set autocommit = 0;
insert into mytest values(7,'test07');
rollback; -- 事务回滚


set autocommit = 0;
insert into mytest values(8,'test08');
commit; -- 提交事务

-- 查看
select *from mytest;

【MySQL 第十二天 事务的介绍|InnoDB使用事务】
【MySQL 第十二天 事务的介绍|InnoDB使用事务】


【3】mysql是u用InnoDB使用事务

【MySQL 第十二天 事务的介绍|InnoDB使用事务】
【MySQL 第十二天 事务的介绍|InnoDB使用事务】

改变前

图书信息表
【MySQL 第十二天 事务的介绍|InnoDB使用事务】
== 读者信息表==
【MySQL 第十二天 事务的介绍|InnoDB使用事务】

== 借阅信息表==
【MySQL 第十二天 事务的介绍|InnoDB使用事务】

改变后

delimiter //
create procedure borrowproc(in cid char(18),in bid int)
begin 
declare store_1 int;
declare money float(7,3);
select store into store_1 from  bookinfo where book_id = bid; -- 查询图书信息表价格并赋值给store_1
select balance into money from readerinfo where card_id = cid; -- 查询读者信息表余额赋值给money
set autocommit = 0;	-- 禁止自动提交 必须认为手动
insert into borrowinfo values(bid,cid,curdate(),date_add(curdate(),interval 1 month),'否'); -- 插入借阅信息表book_id card_id 当前日期 还书日期 一个月 否 
update bookinfo set store = store-1 where book_id = bid; -- 图书信息表 图书库存减一 更新值
update readerinfo set balance =  balance - (select price from bookinfo where book_id = bid)*(0.05) where  card_id = cid; -- 读者信息表余额插入更新

if store_1 = 0 or money <= 200 then
	rollback; -- 回滚 以上操作全部取消
else 
	commit;	-- 提交事务 执行成功
end if;
end//
delimiter ;

-- 查看相关表
select *from bookinfo;
select *from borrowinfo;
select *from readerinfo;

-- 条用储存过程
call borrowproc('52242855555',1);


【MySQL 第十二天 事务的介绍|InnoDB使用事务】


图书信息表
【MySQL 第十二天 事务的介绍|InnoDB使用事务】

== 读者信息表==
【MySQL 第十二天 事务的介绍|InnoDB使用事务】

== 借阅信息表==
【MySQL 第十二天 事务的介绍|InnoDB使用事务】


坚持,最后还剩两节了