您好,登錄后才能下訂單哦!
本文主要給大家簡單講講Mysql數據庫事務的特性及運用,相關專業術語大家可以上網查查或者找一些相關書籍補充一下,這里就不涉獵了,我們就直奔主題吧,希望Mysql數據庫事務的特性及運用這篇文章可以給大家帶來一些實際幫助。
2.1.多事務同時執行:
彼此之間互不影響的方式進行并行;事務之間交互,通過數據集。
START TRANSACTION: 啟動事務命令 數據庫只有啟動了事務才允許回滾撤銷等操作。
且數據的engine引擎必須是innodb,才支持事務
ROLLBACK : 回滾事務,mysql只要沒有提交開啟了事務都可以進行回滾操作。
COMMIT: 事務提交,事務提交后無法進行回滾操作。
若未明確啟動事務:autocommit 能實現自動提交,每一個操作都直接提交;
強烈建議:明確使用事務,否則所有操作都被當成一個事務來處理,并關閉自動提交。
否則浪費mysql很多IO操作,每寫一條語句都執行提交至持久性存儲,很浪費資源
2.2.事務的特性:
2.2.1.Atomicity: 原子性
事務所引起的數據庫操作,要不都完成,要么都不執行;
2.2.2.Consistency: 一致性
2.2.3.Isolation: 隔離性
事務調度: 事務之間影響最小
MVCC: 多版本并發控制
2.2.4.Durability: 持久性
一旦事務成功完成,系統必須保證任何故障都不會引起事務表示出不一致性;
1、事務提交之前就已經寫出數據至持久性存儲;
2、結合事務日志完成;
事務日志:順序IO
數據文件:隨機IO
2.3.事務的狀態:
活動的: active
部分提交的: 最后一條語句執行后
失敗的:
終止的:
提交的:
狀態間的轉換過程
2.4.事務并發執行的優勢:
1、提高吞吐量和資源利用率 2、減少等待時間
2.5.事務調度: 1、可恢復調度 2、無極聯調度
3.1. 設置自動提交開關
mysql> SELECT @@AUTOCOMMIT; #自動提交的狀態1為開啟,0為關閉;
+--------------+
| @@AUTOCOMMIT |
+--------------+
| 1 |
+--------------+
1 row in set (0.00 sec)
mysql> SET AUTOCOMMIT=0; #設定自動提交關閉
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT @@AUTOCOMMIT; #查詢自動提交的狀態1為開啟,0為關閉;
+--------------+
| @@AUTOCOMMIT |
+--------------+
| 0 |
+--------------+
1 row in set (0.00 sec)
3.2. 回滾事務
mysql> SET AUTOCOMMIT=0; #設定自動提交關閉
Query OK, 0 rows affected (0.00 sec)
mysql> commit #將之前的事務全部提交
mysql> DELETE FROM student WHERE Name LIKE 'Li%'; #刪除Name字段包含Li的行
Query OK, 1 row affected (0.03 sec)
mysql> SELECT * FROM student; #Li哪行已經被刪除
+-----+------------+-----+-----+
| SID | Name | Age | CID |
+-----+------------+-----+-----+
| 2 | Cheng Long | 0 | 2 |
| 3 | Yang Guo | 0 | 3 |
| 4 | Guo Jing | 0 | 4 |
+-----+------------+-----+-----+
3 rows in set (0.00 sec)
mysql> ROLLBACK; #回滾事務,自動提交關閉后,默認就開啟了事務,可以實現回滾等操作
Query OK, 0 rows affected (0.01 sec)
mysql> SELECT * FROM student; #刪除的行恢復了
+-----+------------+-----+-----+
| SID | Name | Age | CID |
+-----+------------+-----+-----+
| 1 | Li Lianjie | 0 | 1 |
| 2 | Cheng Long | 0 | 2 |
| 3 | Yang Guo | 0 | 3 |
| 4 | Guo Jing | 0 | 4 |
+-----+------------+-----+-----+
4 rows in set (0.00 sec)
3.2. 保存點:恢復到所定義的那個保存點SAVEPOINT,保存點名稱不能為純數字。
保存點:SAVEPOINT savepoint_name; 保存以上操作為該保存點名稱
回滾保存點:ROLLBACK TO savepoint_name; 回滾到該保存點之前的狀態
3.2. 1.保存點實驗一
mysql> START TRANSACTION; #啟動事務
Query OK, 0 rows affected (0.00 sec)
mysql> SAVEPOINT a; #該保存點student表數據都存在
Query OK, 0 rows affected (0.00 sec)
mysql> select * FROM student;
+-----+------------+-----+-----+
| SID | Name | Age | CID |
+-----+------------+-----+-----+
| 1 | Li Lianjie | 0 | 1 |
| 2 | Cheng Long | 0 | 2 |
| 3 | Yang Guo | 26 | 3 |
| 4 | Guo Jing | 53 | 4 |
+-----+------------+-----+-----+
4 rows in set (0.00 sec)
mysql> DELETE FROM student WHERE SID=4; #刪除student表的SID為4的行
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM student;
+-----+------------+-----+-----+
| SID | Name | Age | CID |
+-----+------------+-----+-----+
| 1 | Li Lianjie | 0 | 1 |
| 2 | Cheng Long | 0 | 2 |
| 3 | Yang Guo | 26 | 3 |
+-----+------------+-----+-----+
3 rows in set (0.00 sec)
mysql> SAVEPOINT b; #該保存點student表數據的SID為4的行不存在了
Query OK, 0 rows affected (0.00 sec)
mysql> DELETE FROM student WHERE SID=3;
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM student;
+-----+------------+-----+-----+
| SID | Name | Age | CID |
+-----+------------+-----+-----+
| 1 | Li Lianjie | 0 | 1 |
| 2 | Cheng Long | 0 | 2 |
+-----+------------+-----+-----+
2 rows in set (0.00 sec)
mysql> SAVEPOINT c; #該保存點student表的SID為3和4的行都不存在
Query OK, 0 rows affected (0.00 sec)
mysql> ROLLBACK TO b; #回滾至保存點b,即student表,SID為4的行不存在的行
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT * FROM student;
+-----+------------+-----+-----+
| SID | Name | Age | CID |
+-----+------------+-----+-----+
| 1 | Li Lianjie | 0 | 1 |
| 2 | Cheng Long | 0 | 2 |
| 3 | Yang Guo | 26 | 3 |
+-----+------------+-----+-----+
3 rows in set (0.00 sec)
mysql> ROLLBACK TO a; #即所有數據都存在的那個點
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT * FROM student;
+-----+------------+-----+-----+
| SID | Name | Age | CID |
+-----+------------+-----+-----+
| 1 | Li Lianjie | 0 | 1 |
| 2 | Cheng Long | 0 | 2 |
| 3 | Yang Guo | 26 | 3 |
| 4 | Guo Jing | 53 | 4 |
+-----+------------+-----+-----+
4 rows in set (0.00 sec)
3.2.2.保存點實驗二
3.2.2.1.設置a2、a4、a6、a8四個保存點
mysql> select * from tutors;
+-----+--------------+--------+------+
| TID | Tname | Gender | Age |
+-----+--------------+--------+------+
| 1 | HongQigong | M | 93 |
| 2 | HuangYaoshi | M | 63 |
| 3 | Miejueshitai | F | 72 |
| 4 | OuYangfeng | M | 76 |
| 5 | YiDeng | M | 90 |
| 6 | YuCanghai | M | 56 |
| 7 | Jinlunfawang | M | 67 |
| 8 | HuYidao | M | 42 |
| 9 | NingZhongze | F | 49 |
+-----+--------------+--------+------+
9 rows in set (0.00 sec)
mysql> delete from tutors where TID = 2 ;
Query OK, 1 row affected (0.00 sec)
mysql> savepoint a2 ;
Query OK, 0 rows affected (0.00 sec)
mysql> delete from tutors where TID = 4 ;
Query OK, 1 row affected (0.00 sec)
mysql> savepoint a4;
Query OK, 0 rows affected (0.00 sec)
mysql> delete from tutors where TID = 6 ;
Query OK, 1 row affected (0.00 sec)
mysql> savepoint a6;
Query OK, 0 rows affected (0.00 sec)
mysql> delete from tutors where TID = 8 ;
Query OK, 1 row affected (0.00 sec)
mysql> savepoint a8;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from tutors;
+-----+--------------+--------+------+
| TID | Tname | Gender | Age |
+-----+--------------+--------+------+
| 1 | HongQigong | M | 93 |
| 3 | Miejueshitai | F | 72 |
| 5 | YiDeng | M | 90 |
| 7 | Jinlunfawang | M | 67 |
| 9 | NingZhongze | F | 49 |
+-----+--------------+--------+------+
5 rows in set (0.00 sec)
3.2.2.回滾到a4保存點OK
mysql> rollback to a4;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from tutors;
+-----+--------------+--------+------+
| TID | Tname | Gender | Age |
+-----+--------------+--------+------+
| 1 | HongQigong | M | 93 |
| 3 | Miejueshitai | F | 72 |
| 5 | YiDeng | M | 90 |
| 6 | YuCanghai | M | 56 |
| 7 | Jinlunfawang | M | 67 |
| 8 | HuYidao | M | 42 |
| 9 | NingZhongze | F | 49 |
+-----+--------------+--------+------+
7 rows in set (0.00 sec)
3.2.3.再回滾到a6保存點失敗,該保存點已不存在
mysql> rollback to a6;
ERROR 1305 (42000): SAVEPOINT a6 does not exist
3.2.4.但再回滾到a2保存點OK
mysql> rollback to a2;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from tutors;
+-----+--------------+--------+------+
| TID | Tname | Gender | Age |
+-----+--------------+--------+------+
| 1 | HongQigong | M | 93 |
| 3 | Miejueshitai | F | 72 |
| 4 | OuYangfeng | M | 76 |
| 5 | YiDeng | M | 90 |
| 6 | YuCanghai | M | 56 |
| 7 | Jinlunfawang | M | 67 |
| 8 | HuYidao | M | 42 |
| 9 | NingZhongze | F | 49 |
+-----+--------------+--------+------+
8 rows in set (0.00 sec)
3.2.5.但再回滾到最原始保存點OK
mysql> rollback;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from tutors;
+-----+--------------+--------+------+
| TID | Tname | Gender | Age |
+-----+--------------+--------+------+
| 1 | HongQigong | M | 93 |
| 2 | HuangYaoshi | M | 63 |
| 3 | Miejueshitai | F | 72 |
| 4 | OuYangfeng | M | 76 |
| 5 | YiDeng | M | 90 |
| 6 | YuCanghai | M | 56 |
| 7 | Jinlunfawang | M | 67 |
| 8 | HuYidao | M | 42 |
| 9 | NingZhongze | F | 49 |
+-----+--------------+--------+------+
9 rows in set (0.00 sec)
3.2.6.回滾到最原始保存點后,但再回滾a8、a2R失敗
mysql> rollback to a8;
ERROR 1305 (42000): SAVEPOINT a8 does not exist
mysql> rollback to a2;
ERROR 1305 (42000): SAVEPOINT a2 does not exist
Mysql數據庫事務的特性及運用就先給大家講到這里,對于其它相關問題大家想要了解的可以持續關注我們的行業資訊。我們的板塊內容每天都會捕捉一些行業新聞及專業知識分享給大家的。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。