回滾點
設(shè)置回滾點語法:savepoint 回滾點名字;
回到回滾點語法:rollback to 回滾點名字;
自動事務(wù)處理
show variables like 'autocommit';
關(guān)閉自動提交:set autocommit=off/0;
事務(wù)的四大特性:ACID
A:Atomic,原子性
C:Consistency,一致性
I:Isolation,隔離性
D:Durability,持久性
鎖機制
-- 創(chuàng)建一個賬戶表
create table my_account(
id int primary key auto_increment,
? ? number char(16)not null unique comment '賬戶',
? ? name varchar(20)not null ,
? ? money decimal(10,2)default 0.0 comment '賬戶余額'
)charset utf8;
-- 插入數(shù)據(jù)
insert into my_accountvalues (null,'0000000000000001','張三',1000),(null,'0000000000000002','李四',1000);
-- 張三轉(zhuǎn)賬1000元給李四
update my_accountset money=money-1000 where id=1;
-- 事務(wù)安全
-- 開啟事務(wù)
start transaction ;
-- 事務(wù)的操作: 李四賬戶減少
update my_accountset money=money-1000 where id=2;
-- 事務(wù)的操作:張三賬戶增加
update my_accountset money=money+1000 where id=1;
-- 提交事務(wù)
commit ;
-- 回滾點操作
-- 開啟事務(wù)
start transaction ;
-- 事務(wù)處理:張三發(fā)工資,加錢
update my_accountset money=money+10000 where id=1;
-- 設(shè)置回滾點
? ? savepoint sq1;
-- 銀行扣稅
update my_accountset money=money-10000*0.05 where id=2;
-- 錯誤
-- 回滾到回滾點
rollback to sq1;
-- 繼續(xù)操作
update my_accountset money=money-10000*0.05 where id=1;
-- 查看結(jié)果
select * from my_account;
-- 提交結(jié)果
commit ;
-- 顯示系統(tǒng)變量autocommit(模糊查詢)
show variables like 'autocommit';
-- 關(guān)閉事務(wù)自動提交
set autocommit=0;
update my_accountset money=money+11000 where id=2;
commit ;-- 開啟事務(wù)自動提交
set autocommit =1;
update my_accountset money=money-10000*0.05 where id=2;
-- 開啟事務(wù): 隔離性
start transaction ;
-- 給張三返稅,反500
update my_accountset money=money+500 where id=1;
start transaction ;
? ? update my_accountset money=money-500 where id=2;
? ? select * from my_account;
select * from my_account;
rollback ;
select * from my_account;
-- 鎖機制
start transaction ;
update my_accountset money=money+500 where name='張三';
? ? update my_accountset money=money+500 where id=2;
rollback ;