SQL事務的用法示例
阿新 • • 發佈:2018-12-18
開啟事務的三種方式:
1.在此執行緒內有效,上一事務結束後,立刻自動開啟新的事物
mysql> set autocommit = 0;
2.臨時開啟
mysql> begin;
3.臨時開啟
mysql> start transaction;
提交事務:
mysql> commit;
回滾事務:
mysql> rollback;
插入儲存點:
mysql> savepoint a;
插入後,再回滾到a時,意味著a之後的語句全部失效
mysql> begin; Query OK, 0 rows affected (0.00 sec) mysql> insert into jdbc_test values(null,"li","jkhsdfjdgh"); Query OK, 1 row affected (0.02 sec) mysql> savepoint a; Query OK, 0 rows affected (0.02 sec) mysql> insert into jdbc_test values(null,"wang","45456456"); Query OK, 1 row affected (0.00 sec) mysql> savepoint b; Query OK, 0 rows affected (0.00 sec) mysql> select * from jdbc_test; +---------+-----------+---------------------+ | jdbc_id | jdbc_name | jdbc_desc | +---------+-----------+---------------------+ | 1 | kobe | twotimesmvp | | 2 | levin | arookie | | 3 | jodan | mvp of the history | | 4 | kobe | two times mvp | | 5 | levin | a rookie | | 6 | jodan | mvp of the history | | 7 | mcgrady | two times defenwang | | 8 | shaq | rebound | | 10 | lebron | mvp | | 11 | bird | mop | | 12 | li | jkhsdfjdgh | | 13 | wang | 45456456 | +---------+-----------+---------------------+ 12 rows in set (0.00 sec) mysql> rollback to a; Query OK, 0 rows affected (0.00 sec) mysql> select * from jdbc_test; +---------+-----------+---------------------+ | jdbc_id | jdbc_name | jdbc_desc | +---------+-----------+---------------------+ | 1 | kobe | twotimesmvp | | 2 | levin | arookie | | 3 | jodan | mvp of the history | | 4 | kobe | two times mvp | | 5 | levin | a rookie | | 6 | jodan | mvp of the history | | 7 | mcgrady | two times defenwang | | 8 | shaq | rebound | | 10 | lebron | mvp | | 11 | bird | mop | | 12 | li | jkhsdfjdgh | +---------+-----------+---------------------+ 11 rows in set (0.00 sec)