create table account(
-> id int primary key auto_increment,
-> name varchar(40),
-> money float
-> );
4、往表中插入数据
insert into account(name,money) values
-> (‘a’,1000),
-> (‘b’,1000);
6、开启一个事务然后通过update语句将a账户的100元钱转给b用户,最后提交事务。
start transaction;
update account set money=money-100 where name = ‘a’;
update account set money=money+100 where name = ‘b’;
commit;
7、查询表中余额
select * from account;
8、开启一个事务然后通过update语句将b账户的100元钱转给a用户
start transaction;
update account set money=money+100 where name = ‘a’;
update account set money=money-100 where name = ‘b’;
9、再查询表中余额
select * from account;
10、先退出MySQL数据库,再登录MySQL数据库,然后再使用chapter06数据库,在查询余额
11、开启一个事务然后通过update语句将b账户的100元钱转给a用户,然后使用commit语句来提交事务。
start transaction;
update account set money = money +100 where name =‘a’;
update account set money = money -100 where name =‘b’;
commit;
13、开启一个事务,通过update语句将a账户的100元钱转给b账户。
start transaction;
update account set money = money -100 where name =‘a’;
update account set money = money +100 where name =‘b’;
14、然后查询A、B账户的金额;
select * from account;
15、从查询结果可以看出,a账户的100元钱转给b账户了,但是事务还没有提交,我们还可以执行事务回滚,操作如下:
rollback;
17、将b账户的隔离级别改为read uncommitted;
set session transaction isolation level read uncommitted;
select @@transaction_isolation;
19、演示脏读:
start transaction;
select * from account;
20、在a账户中开启一个事务,并在当前窗口中执行转账功能,但不提交事务。
start transaction;
update account set money = money - 100 where name =‘a’;
update account set money = money + 100 where name =‘b’;
21、然后再查询余额信息
select * from account;
22、将上一步开启的事务进行回滚
rollback;
select * from account;
23、设置b账户中事务的隔离级别
set session transaction isolation level read committed;
24、验证是否出现脏读:
start transaction;
select * from account;
25、在a账户中开启一个事务,并在该事务中实现转账功能
start transaction;
update account set money = money -100 where name = ‘a’;
update account set money = money +100 where name = ‘b’;
26、再次查询各账户的余额信息。
select * from account;
27、演示不可重复读
start transaction;
select * from account;
28、在a账户中不用开启事务,直接使用update语句执行更新操作即可
update account set money = money -100 where name = ‘a’;
29、查询a账户的余额
select * from account;
30、当a账户的更新语句完成后,查询b账户的余额
select * from account;
31、设置b账户中事务的隔离级别
set session transaction isolation level repeatable read;
32、验证是否出现不可重复读
start transaction;
select * from account;
33、幻读
set session transaction isolation level read committed;
34、演示幻读
start transaction;
select * from account;
insert into account(name,money) values(‘c’,1000);
set session transaction isolation level repeatable read;
35、验证是否出现幻读
start transaction;
select * from account;
select * from account;
36、可串行化
start transaction;
select * from account;