前言
本文講述MySQL中的事務(wù),以賬戶轉(zhuǎn)賬為例,體會事務(wù)的概念,并講解事務(wù)相關(guān)的一個關(guān)鍵字用法:savepoint
示例
數(shù)據(jù)準(zhǔn)備
drop table if exists account;
create table account(
id int primary key AUTO_INCREMENT comment 'ID',
name varchar(10) comment '姓名',
money double(10,2) comment '余額'
) comment '賬戶表';
insert into account(name, money) VALUES ('張三',2000), ('李四',2000);
未控制事務(wù) | 轉(zhuǎn)賬正常
-- 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 = '李四';
所有SQL正常執(zhí)行,沒有出錯。結(jié)果就是:張三賬戶余額-1000;李四賬戶余額+1000
未控制事務(wù) | 異常情況
-- 1. 查詢張三余額
select * from account where name = '張三';
-- 2. 張三的余額減少1000
update account set money = money - 1000 where name = '張三';
-- 手動寫一行錯誤的SQL語句
select xxx xxx;
-- 3. 李四的余額增加1000
update account set money = money + 1000 where name = '李四';
只有前兩個SQL執(zhí)行了,第三個SQL沒有執(zhí)行,出現(xiàn)數(shù)據(jù)不一致了:張三的錢減少了,但是李四的錢沒有增加
控制事務(wù) | 示例
mysql> START TRANSACTION; # 開啟事務(wù)
Query OK, 0 rows affected (0.00 sec)
mysql> select * from account where name = '張三'; # 展示張三賬戶金額
+----+--------+---------+
| id | name | money |
+----+--------+---------+
| 1 | 張三 | 2000.00 |
+----+--------+---------+
1 row in set (0.00 sec)
mysql> update account set money = money - 1000 where name = '張三'; # 將張三賬戶金額減少1000
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update account set money = money + 1000 where name = '李四'; # 將李四賬戶金額增加1000
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from account where name = '張三'; # 查詢張三賬戶金額(在事務(wù)里看,金額確實少了。但是數(shù)據(jù)庫里面賬戶值還沒變)
+----+--------+---------+
| id | name | money |
+----+--------+---------+
| 1 | 張三 | 1000.00 |
+----+--------+---------+
1 row in set (0.00 sec)
mysql> COMMIT; # 上述操作均成功,提交事務(wù),修改生效。若某一操作失敗,需要回滾,COMMIT改為ROLLBACK
Query OK, 0 rows affected (0.00 sec)
mysql> select * from account where name = '張三';
+----+--------+---------+
| id | name | money |
+----+--------+---------+
| 1 | 張三 | 1000.00 |
+----+--------+---------+
1 row in set (0.00 sec)
上述操作過程大致三個步驟:開啟事務(wù)、執(zhí)行一組SQL語句、提交或回滾事務(wù)。
事務(wù)開啟方式
針對開啟事務(wù)這一步,有如下幾種方式:
# 方式1:關(guān)閉隱式事務(wù)
SELECT @@autocommit;
SET @@autocommit = 0;
# 方式2:手動開啟事務(wù)
START TRANSACTION;
# 方式3:手動開啟事務(wù)-另一種
BEGIN;
針對提交或回滾事務(wù)這一步,是看上數(shù)SQL操作是否都成功。若都成功,則提交;若有失敗,則回滾,使數(shù)據(jù)恢復(fù)到修改前的狀態(tài):
COMMIT; # 若SQL語句執(zhí)行正常,提交事務(wù)
ROLLBACK; # 若SQL語句有執(zhí)行異常,回滾事務(wù)
若是要用一個sql腳本來處理一組事務(wù),則應(yīng)該在提交或者回滾這一步加上判斷條件:
# 開始事務(wù)
BEGIN;
#執(zhí)行一組SQL
xxxxxx
# 提交或回滾
if(所有SQL執(zhí)行成功) THEN
COMMIT;
ELSE
ROLLBACK;
END IF;
復(fù)雜的事務(wù)處理過程,通??梢越柚谌焦ぞ?,例如腳本語言:Python、PHP等。
事務(wù)相關(guān)關(guān)鍵字 | savepoint
savepoint關(guān)鍵字可以使得回滾事務(wù)的時候只混滾部分代碼處理的結(jié)果。文章來源:http://www.zghlxwxcb.cn/news/detail-850107.html
如下,是關(guān)于savepoint的大概作用流程:文章來源地址http://www.zghlxwxcb.cn/news/detail-850107.html
# 開啟事務(wù)
xxx
# 一組SQL
xxx修改操作1
xxx修改操作2
SAVEPOINT change1;
xxx查詢操作1
xxx查詢操作2,出問題報錯了
# 因為修改操作都完成了,在查詢結(jié)果是否正確的時候出了錯誤,就可以通過這樣,只回滾change1到ROLLBACK TO change1之間的代碼
ROLLBACK TO change1;
# ROLLBACK如果不加TO,就會回滾到初始位置,不管是否有savepoint
# 若是不想要某個savepoint點,可以刪除
RELEASE SAVEPOINT change1;
到了這里,關(guān)于【示例】MySQL-事務(wù)控制示例:賬戶轉(zhuǎn)賬-savepoint關(guān)鍵字的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!