1. 程式人生 > 實用技巧 >mysql8學習筆記7--事務使用的控制

mysql8學習筆記7--事務使用的控制

預設情況下,MySQL在啟用自動提交模式的情況下執行。這意味著,當不在事務內時,每個語句都是原子的,就像它被START TRANSACTION和包圍一樣COMMIT。不能ROLLBACK用來撤消效果。但是,如果在語句執行期間發生錯誤,則會回滾該語句。

根據mysql參考手冊:

13.3.1 START TRANSACTION,COMMIT和ROLLBACK語句

START TRANSACTION
    [transaction_characteristic [, transaction_characteristic] ...]

transaction_characteristic: {
    WITH CONSISTENT SNAPSHOT
  
| READ WRITE | READ ONLY } BEGIN [WORK] COMMIT [WORK] [AND [NO] CHAIN] [[NO] RELEASE] ROLLBACK [WORK] [AND [NO] CHAIN] [[NO] RELEASE] SET autocommit = {0 | 1}

中括號裡的修飾符號,後面再看

這些語句提供對事務使用的控制

  • START TRANSACTIONBEGIN開始新交易。

  • COMMIT提交當前事務,使其更改永久生效。

  • ROLLBACK回滾當前事務,取消其更改。

  • SET autocommit禁用或啟用當前會話的預設自動提交模式。

要隱式禁用單個語句系列的自動提交模式,請使用以下START TRANSACTION語句:

START TRANSACTION;
SELECT @A:=SUM(salary) FROM table1 WHERE type=1;
UPDATE table2 SET summary=@A WHERE type=1;
COMMIT;

使用時START TRANSACTION,自動提交保持禁用狀態,直到您使用COMMIT結束事務ROLLBACK然後,自動提交模式將恢復為之前的狀態。

mysql> show variables like '%autocommit%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit    | ON    |
+---------------+-------+
1
row in set (0.00 sec) mysql> select * from customers4; +---------+-----------+-----------------------+-----------+------------+----------+--------------+--------------+------------+ | cust_id | cust_name | cust_address | cust_city | cust_state | cust_zip | cust_country | cust_contact | cust_email | +---------+-----------+-----------------------+-----------+------------+----------+--------------+--------------+------------+ | 1 | big | 中國深圳南山區 | 深圳 | NULL | NULL | NULL | NULL | NULL | | 2 | big | 中國深圳南山區 | 深圳 | NULL | NULL | NULL | NULL | NULL | +---------+-----------+-----------------------+-----------+------------+----------+--------------+--------------+------------+ 2 rows in set (0.00 sec) mysql> update customers4 set cust_address='中國深圳龍崗區',cust_city='深圳' where cust_id=1; Query OK, 1 row affected (0.37 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from customers4; +---------+-----------+-----------------------+-----------+------------+----------+--------------+--------------+------------+ | cust_id | cust_name | cust_address | cust_city | cust_state | cust_zip | cust_country | cust_contact | cust_email | +---------+-----------+-----------------------+-----------+------------+----------+--------------+--------------+------------+ | 1 | big | 中國深圳龍崗區 | 深圳 | NULL | NULL | NULL | NULL | NULL | | 2 | big | 中國深圳南山區 | 深圳 | NULL | NULL | NULL | NULL | NULL | +---------+-----------+-----------------------+-----------+------------+----------+--------------+--------------+------------+ 2 rows in set (0.00 sec) mysql> rollback; Query OK, 0 rows affected (0.00 sec) mysql> select * from customers4; +---------+-----------+-----------------------+-----------+------------+----------+--------------+--------------+------------+ | cust_id | cust_name | cust_address | cust_city | cust_state | cust_zip | cust_country | cust_contact | cust_email | +---------+-----------+-----------------------+-----------+------------+----------+--------------+--------------+------------+ | 1 | big | 中國深圳龍崗區 | 深圳 | NULL | NULL | NULL | NULL | NULL | | 2 | big | 中國深圳南山區 | 深圳 | NULL | NULL | NULL | NULL | NULL | +---------+-----------+-----------------------+-----------+------------+----------+--------------+--------------+------------+ 2 rows in set (0.00 sec) mysql> start transaction; Query OK, 0 rows affected (0.00 sec) mysql> update customers4 set cust_name='small' where cust_id=1; Query OK, 1 row affected (0.02 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from customers4; +---------+-----------+-----------------------+-----------+------------+----------+--------------+--------------+------------+ | cust_id | cust_name | cust_address | cust_city | cust_state | cust_zip | cust_country | cust_contact | cust_email | +---------+-----------+-----------------------+-----------+------------+----------+--------------+--------------+------------+ | 1 | small | 中國深圳龍崗區 | 深圳 | NULL | NULL | NULL | NULL | NULL | | 2 | big | 中國深圳南山區 | 深圳 | NULL | NULL | NULL | NULL | NULL | +---------+-----------+-----------------------+-----------+------------+----------+--------------+--------------+------------+ 2 rows in set (0.00 sec) mysql> rollback; Query OK, 0 rows affected (0.04 sec) mysql> select * from customers4; +---------+-----------+-----------------------+-----------+------------+----------+--------------+--------------+------------+ | cust_id | cust_name | cust_address | cust_city | cust_state | cust_zip | cust_country | cust_contact | cust_email | +---------+-----------+-----------------------+-----------+------------+----------+--------------+--------------+------------+ | 1 | big | 中國深圳龍崗區 | 深圳 | NULL | NULL | NULL | NULL | NULL | | 2 | big | 中國深圳南山區 | 深圳 | NULL | NULL | NULL | NULL | NULL | +---------+-----------+-----------------------+-----------+------------+----------+--------------+--------------+------------+ 2 rows in set (0.00 sec) mysql> start transaction; Query OK, 0 rows affected (0.00 sec) mysql> update customers4 set cust_name='small' where cust_id=1; Query OK, 1 row affected (0.04 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from customers4; +---------+-----------+-----------------------+-----------+------------+----------+--------------+--------------+------------+ | cust_id | cust_name | cust_address | cust_city | cust_state | cust_zip | cust_country | cust_contact | cust_email | +---------+-----------+-----------------------+-----------+------------+----------+--------------+--------------+------------+ | 1 | small | 中國深圳龍崗區 | 深圳 | NULL | NULL | NULL | NULL | NULL | | 2 | big | 中國深圳南山區 | 深圳 | NULL | NULL | NULL | NULL | NULL | +---------+-----------+-----------------------+-----------+------------+----------+--------------+--------------+------------+ 2 rows in set (0.00 sec) mysql> commit; Query OK, 0 rows affected (0.01 sec) mysql> select * from customers4; +---------+-----------+-----------------------+-----------+------------+----------+--------------+--------------+------------+ | cust_id | cust_name | cust_address | cust_city | cust_state | cust_zip | cust_country | cust_contact | cust_email | +---------+-----------+-----------------------+-----------+------------+----------+--------------+--------------+------------+ | 1 | small | 中國深圳龍崗區 | 深圳 | NULL | NULL | NULL | NULL | NULL | | 2 | big | 中國深圳南山區 | 深圳 | NULL | NULL | NULL | NULL | NULL | +---------+-----------+-----------------------+-----------+------------+----------+--------------+--------------+------------+ 2 rows in set (0.00 sec) mysql> rollback; Query OK, 0 rows affected (0.00 sec) mysql> select * from customers4; +---------+-----------+-----------------------+-----------+------------+----------+--------------+--------------+------------+ | cust_id | cust_name | cust_address | cust_city | cust_state | cust_zip | cust_country | cust_contact | cust_email | +---------+-----------+-----------------------+-----------+------------+----------+--------------+--------------+------------+ | 1 | small | 中國深圳龍崗區 | 深圳 | NULL | NULL | NULL | NULL | NULL | | 2 | big | 中國深圳南山區 | 深圳 | NULL | NULL | NULL | NULL | NULL | +---------+-----------+-----------------------+-----------+------------+----------+--------------+--------------+------------+ 2 rows in set (0.00 sec) mysql>

要顯式禁用自動提交模式,請使用以下語句:

SET autocommit=0;
mysql> SET autocommit=0;
Query OK, 0 rows affected (0.04 sec)

mysql> show variables like '%autocommit%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit    | OFF   |
+---------------+-------+
1 row in set (0.00 sec)

mysql> select * from customers4;
+---------+-----------+-----------------------+-----------+------------+----------+--------------+--------------+------------+
| cust_id | cust_name | cust_address          | cust_city | cust_state | cust_zip | cust_country | cust_contact | cust_email |
+---------+-----------+-----------------------+-----------+------------+----------+--------------+--------------+------------+
|       1 | small     | 中國深圳龍崗區        | 深圳      | NULL       | NULL     | NULL         | NULL         | NULL       |
|       2 | big       | 中國深圳南山區        | 深圳      | NULL       | NULL     | NULL         | NULL         | NULL       |
+---------+-----------+-----------------------+-----------+------------+----------+--------------+--------------+------------+
2 rows in set (0.00 sec)

mysql> update customers4  set cust_name='middle' where cust_id=1;     
Query OK, 1 row affected (0.02 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from customers4;
+---------+-----------+-----------------------+-----------+------------+----------+--------------+--------------+------------+
| cust_id | cust_name | cust_address          | cust_city | cust_state | cust_zip | cust_country | cust_contact | cust_email |
+---------+-----------+-----------------------+-----------+------------+----------+--------------+--------------+------------+
|       1 | middle    | 中國深圳龍崗區        | 深圳      | NULL       | NULL     | NULL         | NULL         | NULL       |
|       2 | big       | 中國深圳南山區        | 深圳      | NULL       | NULL     | NULL         | NULL         | NULL       |
+---------+-----------+-----------------------+-----------+------------+----------+--------------+--------------+------------+
2 rows in set (0.00 sec)

mysql> rollback;
Query OK, 0 rows affected (0.12 sec)

mysql> select * from customers4;
+---------+-----------+-----------------------+-----------+------------+----------+--------------+--------------+------------+
| cust_id | cust_name | cust_address          | cust_city | cust_state | cust_zip | cust_country | cust_contact | cust_email |
+---------+-----------+-----------------------+-----------+------------+----------+--------------+--------------+------------+
|       1 | small     | 中國深圳龍崗區        | 深圳      | NULL       | NULL     | NULL         | NULL         | NULL       |
|       2 | big       | 中國深圳南山區        | 深圳      | NULL       | NULL     | NULL         | NULL         | NULL       |
+---------+-----------+-----------------------+-----------+------------+----------+--------------+--------------+------------+
2 rows in set (0.00 sec)

mysql> update customers4  set cust_name='middle' where cust_id=1;
Query OK, 1 row affected (0.03 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from customers4;
+---------+-----------+-----------------------+-----------+------------+----------+--------------+--------------+------------+
| cust_id | cust_name | cust_address          | cust_city | cust_state | cust_zip | cust_country | cust_contact | cust_email |
+---------+-----------+-----------------------+-----------+------------+----------+--------------+--------------+------------+
|       1 | middle    | 中國深圳龍崗區        | 深圳      | NULL       | NULL     | NULL         | NULL         | NULL       |
|       2 | big       | 中國深圳南山區        | 深圳      | NULL       | NULL     | NULL         | NULL         | NULL       |
+---------+-----------+-----------------------+-----------+------------+----------+--------------+--------------+------------+
2 rows in set (0.00 sec)

mysql> commit;
Query OK, 0 rows affected (0.09 sec)

mysql> select * from customers4;
+---------+-----------+-----------------------+-----------+------------+----------+--------------+--------------+------------+
| cust_id | cust_name | cust_address          | cust_city | cust_state | cust_zip | cust_country | cust_contact | cust_email |
+---------+-----------+-----------------------+-----------+------------+----------+--------------+--------------+------------+
|       1 | middle    | 中國深圳龍崗區        | 深圳      | NULL       | NULL     | NULL         | NULL         | NULL       |
|       2 | big       | 中國深圳南山區        | 深圳      | NULL       | NULL     | NULL         | NULL         | NULL       |
+---------+-----------+-----------------------+-----------+------------+----------+--------------+--------------+------------+
2 rows in set (0.00 sec)

mysql> rollback;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from customers4;
+---------+-----------+-----------------------+-----------+------------+----------+--------------+--------------+------------+
| cust_id | cust_name | cust_address          | cust_city | cust_state | cust_zip | cust_country | cust_contact | cust_email |
+---------+-----------+-----------------------+-----------+------------+----------+--------------+--------------+------------+
|       1 | middle    | 中國深圳龍崗區        | 深圳      | NULL       | NULL     | NULL         | NULL         | NULL       |
|       2 | big       | 中國深圳南山區        | 深圳      | NULL       | NULL     | NULL         | NULL         | NULL       |
+---------+-----------+-----------------------+-----------+------------+----------+--------------+--------------+------------+
2 rows in set (0.00 sec)

mysql>