SavePoint是数据库事务中的一个概念, 可以将整个事务切割为不同的小事务, 可以选择将状态回滚到某个小事务发生时的样子。
语法:
--创建保存点 SAVEPOINT 保存点的名称; --回滚到某个保存点 ROLLBACK [WORK] TO 保存点的名称; --删除保存点 RELEASE SAVEPOINT 保存点名称;
操作演示:
// 开始查询空表 mysql> select * from t_x; Empty set // 开启事务 mysql> begin; Query OK, 0 rows affected (0.01 sec) // 插入第一条数据 mysql> insert into t_x value(1, '1'); Query OK, 1 row affected (0.01 sec) // 设置保存点 mysql> savepoint a1; Query OK, 0 rows affected (0.01 sec) // 此时查询一条数据 mysql> select * from t_x; +----+------+ | id | name | +----+------+ | 1 | 1 | +----+------+ 1 row in set (0.01 sec) // 插入第二条数据 mysql> insert into t_x value(2, '2'); Query OK, 1 row affected (0.01 sec) // 设置保存点2 mysql> savepoint a2; Query OK, 0 rows affected (0.01 sec) // 查询两条数据 mysql> select * from t_x; +----+------+ | id | name | +----+------+ | 1 | 1 | | 2 | 2 | +----+------+ 2 rows in set (0.01 sec) // 插入第三条数据 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) // 回滚到第一个保存点 mysql> rollback to a1; Query OK, 0 rows affected (0.01 sec) // 此时查询,只有第一个保存点时的一条数据 mysql> select * from t_x; +----+------+ | id | name | +----+------+ | 1 | 1 | +----+------+ 1 row in set (0.00 sec) // 尝试回滚到第二个保存点, 出错 mysql> rollback to a2; 1305 - SAVEPOINT a2 does not exist mysql>
标签:事务,affected,SavePoint,0.01,数据库,sec,mysql,Query,+----+------+ From: https://www.cnblogs.com/zjfjava/p/17238446.html