-- start transaction 开启事务 -- Rollback 回滚事务,即撤销指定的sql语句(只能回退insert delete update语句),回滚到上一次commit的位置 -- Commit 提交事务,提交未存储的事务 -- -- savepoint 保留点 ,事务处理中设置的临时占位符 你可以对它发布回退(与整个事务回退不同)
新建一张表
mysql> create table account(id int,name varchar(20),balance double); Query OK, 0 rows affected (0.07 sec) mysql> insert into account values(1,'cunzhang',8000); Query OK, 1 row affected (0.01 sec) mysql> insert into account values(2,'zhengwen',8000); Query OK, 1 row affected (0.01 sec) mysql> select * from account; +------+----------+---------+ | id | name | balance | +------+----------+---------+ | 1 | cunzhang | 8000 | | 2 | zhengwen | 8000 | +------+----------+---------+
开始事务
mysql> start transaction; Query OK, 0 rows affected (0.00 sec)
-- id=1 balance-5000
mysql> update account set balance = balance-5000 where id =1; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from account; +------+----------+---------+ | id | name | balance | +------+----------+---------+ | 1 | cunzhang | 3000 | | 2 | zhengwen | 8000 | +------+----------+---------+ 2 rows in set (0.00 sec)
回滚事务
mysql> rollback; Query OK, 0 rows affected (0.01 sec) mysql> select * from account; +------+----------+---------+ | id | name | balance | +------+----------+---------+ | 1 | cunzhang | 8000 | | 2 | zhengwen | 8000 | +------+----------+---------+
-- id=2 balance+5000
mysql> update account set balance = balance+5000 where id =2; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from account; +------+----------+---------+ | id | name | balance | +------+----------+---------+ | 1 | cunzhang | 3000 | | 2 | zhengwen | 13000 | +------+----------+---------+ 2 rows in set (0.00 sec)
结束事务
mysql> commit; Query OK, 0 rows affected (0.02 sec) mysql> select * from account; +------+----------+---------+ | id | name | balance | +------+----------+---------+ | 1 | cunzhang | 3000 | | 2 | zhengwen | 13000 | +------+----------+---------+