MySQL事務(wù)的SavePoint簡介及操作
什么是SavePoint
SavePoint是數(shù)據(jù)庫事務(wù)中的一個(gè)概念, 可以將整個(gè)事務(wù)切割為不同的小事務(wù), 可以選擇將狀態(tài)回滾到某個(gè)小事務(wù)發(fā)生時(shí)的樣子,
語法
定義保存點(diǎn)的語法如下:
SAVEPOINT 保存點(diǎn)的名稱;
當(dāng)我們想回滾到某個(gè)保存點(diǎn)時(shí),可以使用下邊的語句。
ROLLBACK [WORK] TO 保存點(diǎn)的名稱
刪除保存點(diǎn)
RELEASE SAVEPOINT 保存點(diǎn)名稱;
重要操作
開啟事務(wù): begin ....DML語句.... 設(shè)置保存點(diǎn): savepoint 保存點(diǎn)名字 ....DML語句.... 設(shè)置保存點(diǎn): savepoint 保存點(diǎn)名字2 ....DML語句.... 回滾保存點(diǎn): rollback to 保存點(diǎn)名字2 (此時(shí)保存點(diǎn)2后面操作的狀態(tài)都將回滾直保存點(diǎn)2時(shí)樣子)
注意,回滾點(diǎn)無法隨意跳轉(zhuǎn),例如上面,如果跳轉(zhuǎn)到第一個(gè)保存點(diǎn)名字,就無法再到保存點(diǎn)名字2了,當(dāng)事務(wù)整體提交、回滾時(shí),保存點(diǎn)也隨之釋放。(一個(gè)事務(wù)中可以設(shè)置多少個(gè)保存點(diǎn)并無限制)
演示: 接下來用mysql演示一下,思路是向mysql insert三條數(shù)據(jù), 每次insert設(shè)置一個(gè)保存點(diǎn),在第三次insert后回滾到第一個(gè)保存點(diǎn)看下效果:
// 開始查詢空表 mysql> select * from t_x; Empty set // 開啟事務(wù) mysql> begin; Query OK, 0 rows affected (0.01 sec) // 插入第一條數(shù)據(jù) mysql> insert into t_x value(1, '1'); Query OK, 1 row affected (0.01 sec) // 設(shè)置保存點(diǎn) mysql> savepoint a1; Query OK, 0 rows affected (0.01 sec) // 此時(shí)查詢一條數(shù)據(jù) mysql> select * from t_x; +----+------+ | id | name | +----+------+ | 1 | 1 | +----+------+ 1 row in set (0.01 sec) // 插入第二條數(shù)據(jù) mysql> insert into t_x value(2, '2'); Query OK, 1 row affected (0.01 sec) // 設(shè)置保存點(diǎn)2 mysql> savepoint a2; Query OK, 0 rows affected (0.01 sec) // 查詢兩條數(shù)據(jù) mysql> select * from t_x; +----+------+ | id | name | +----+------+ | 1 | 1 | | 2 | 2 | +----+------+ 2 rows in set (0.01 sec) // 插入第三條數(shù)據(jù) mysql> insert into t_x value(3, '3'); Query OK, 1 row affected (0.01 sec) // 共三條 mysql> select * from t_x; +----+------+ | id | name | +----+------+ | 1 | 1 | | 2 | 2 | | 3 | 3 | +----+------+ 3 rows in set (0.01 sec) // 回滾到第一個(gè)保存點(diǎn) mysql> rollback to a1; Query OK, 0 rows affected (0.01 sec) // 此時(shí)查詢,只有第一個(gè)保存點(diǎn)時(shí)的一條數(shù)據(jù) mysql> select * from t_x; +----+------+ | id | name | +----+------+ | 1 | 1 | +----+------+ 1 row in set (0.00 sec) // 嘗試回滾到第二個(gè)保存點(diǎn), 出錯(cuò) mysql> rollback to a2; 1305 - SAVEPOINT a2 does not exist mysql>
到此這篇關(guān)于MySQL事務(wù)的SavePoint簡介及操作的文章就介紹到這了,更多相關(guān)MySQL事務(wù)的SavePoint內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Mysql如何實(shí)現(xiàn)不存在則插入,存在則更新
這篇文章主要介紹了Mysql如何實(shí)現(xiàn)不存在則插入,存在則更新,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03DELETE、TRUNCATE 和 DROP 在MySQL中的區(qū)別及功能使用示例
在MySQL數(shù)據(jù)庫中,DELETE、TRUNCATE TABLE 和 DROP 這三個(gè)命令分別適用于不同的數(shù)據(jù)刪除需求,它們?cè)诠ぷ髟怼?yīng)用場(chǎng)景以及特性上有所區(qū)別,這篇文章主要介紹了DELETE、TRUNCATE 和 DROP 在MySQL中的區(qū)別及功能使用示例,需要的朋友可以參考下2024-03-03mysql server is running with the --skip-grant-tables option
今天在mysql中新建數(shù)據(jù)庫提示The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement,原來是數(shù)據(jù)中配置的--skip-grant-tables,這樣安全就降低了,這個(gè)一般當(dāng)忘記root密碼的時(shí)候需要這樣操作2017-07-07MySQL中基本的用戶和權(quán)限管理方法小結(jié)
這篇文章主要介紹了MySQL中基本的用戶和權(quán)限管理方法小結(jié),是MySQL入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-08-08MySQL8.0.28安裝教程詳細(xì)圖解(windows?64位)
如果電腦上已經(jīng)有MySQL數(shù)據(jù)庫再進(jìn)行重做往往會(huì)遇到問題,下面這篇文章主要給大家介紹了關(guān)于windows?64位系統(tǒng)下MySQL8.0.28安裝教程的詳細(xì)教程,文章通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04