當把多個操作當做乙個事務時,這些操作具有一下特點:
mysql> select @@autocommit; # 檢視autocommit
+--------------+
| @@autocommit |
+--------------+
| 1 |
+--------------+
1 row in set (0.00 sec)
mysql> set @@autocommit=0;# 將autocommit設定為0,防止自動提交
query ok, 0 rows affected (0.00 sec)
mysql> select @@autocommit;
+--------------+
| @@autocommit |
+--------------+
| 0 |
+--------------+
1 row in set (0.00 sec)
mysql> start transaction;# 開始事務
query ok, 0 rows affected (0.00 sec)
mysql> update account set balance=balance-1000 where name='ada';
query ok, 1 row affected (0.00 sec)
rows matched: 1 changed: 1 warnings: 0
mysql> update account set balance=balance+1000 where name='bob';
query ok, 1 row affected (0.00 sec)
rows matched: 1 changed: 1 warnings: 0
mysql> select * from account;# 此時該資料表和其他地方的表不一樣了,因為修改還沒有持久化到磁碟資料庫
+----+------+---------+
| id | name | balance |
+----+------+---------+
| 1 | ada | -1000 |
| 2 | bob | 6000 |
+----+------+---------+
2 rows in set (0.00 sec)
mysql> commit;# 提交事務,持久化到磁碟
query ok, 0 rows affected (0.00 sec)
mysql> select * from account;
+----+------+---------+
| id | name | balance |
+----+------+---------+
| 1 | ada | 1000 |
| 2 | bob | 4000 |
+----+------+---------+
2 rows in set (0.00 sec)
mysql> start transaction;
query ok, 0 rows affected (0.00 sec)
mysql> update account set balance=balance-1000 where name='bob';
query ok, 1 row affected (0.00 sec)
rows matched: 1 changed: 1 warnings: 0
mysql> update account set balance=balance+1000 where name='ada';
query ok, 1 row affected (0.00 sec)
rows matched: 1 changed: 1 warnings: 0
mysql> select * from account;
+----+------+---------+
| id | name | balance |
+----+------+---------+
| 1 | ada | 2000 |
| 2 | bob | 3000 |
+----+------+---------+
2 rows in set (0.00 sec)
mysql> rollback;# 事務回滾,撤銷開始事務之後的更改
query ok, 0 rows affected (0.00 sec)
mysql> select * from account;
+----+------+---------+
| id | name | balance |
+----+------+---------+
| 1 | ada | 1000 |
| 2 | bob | 4000 |
+----+------+---------+
2 rows in set (0.00 sec)
開啟兩個mysql客戶端命令列
命令列一
mysql> start transaction;
query ok, 0 rows affected (0.00 sec)
mysql> select * from account;
+----+------+---------+
| id | name | balance |
+----+------+---------+
| 1 | ada | 2000 |
| 2 | bob | 3000 |
+----+------+---------+
2 rows in set (0.00 sec)
mysql> update account set balance=balance-1000 where name='bob';
query ok, 1 row affected (0.00 sec)
rows matched: 1 changed: 1 warnings: 0
mysql> update account set balance=balance+1000 where name='ada';
query ok, 1 row affected (0.00 sec)
rows matched: 1 changed: 1 warnings: 0
一切正常執行,還未進行提交
命令列二
mysql> start transaction;
query ok, 0 rows affected (0.00 sec)
mysql> update account set balance=balance-1000 where name='bob';# 執行更改操作時會陷入等待,這是為了同步兩個程序,防止最後提交上的事務是其中乙個的提交結果,把另乙個提交結果覆蓋
error 1205 (hy000): lock wait timeout exceeded; try restarting transaction
mysql>
當命令列一 執行commit;
後命令列二才能正常執行事務操作 mysql事務操作 mysql的事務操作
倒著思考。杜絕純粹的知識填鴨教育 少廢話,是上 update table1 set money 100 where id 1 a賬戶減少100元 update table2 set money 100 where id 2 b 賬戶增加100元 問題 這是乙個簡單的銀行轉賬案例sql,由於伺服器等未...
MySQL事務操作
在 mysql 命令列的預設設定下,事務都是自動提交的,即執行 sql 語句後就會馬上執行 commit 操作。因此要顯式地開啟乙個事務務須使用命令 begin 或 start transaction,或者執行命令 set autocommit 0,用來禁止使用當前會話的自動提交。菜鳥教程 1 用 ...
MySQL事務操作
在 mysql 命令列的預設設定下,事務都是自動提交的,即執行 sql 語句後就會馬上執行 commit 操作。因此要顯式地開啟乙個事務務須使用命令 begin 或 start transaction,或者執行命令 set autocommit 0,用來禁止使用當前會話的自動提交。菜鳥教程 1 用 ...