事务回滚

最近更新时间: 2024-10-17 17:10:00

事务回滚用于撤消尚未保存到数据库的命令。

命令语法如下:

ROLLBACK; 或者 ROLLBACK to savepoint_name;

例1:

BEGIN;
delete from tbase where id in (3,4);
select * from tbase;
ROLLBACK;
select * from tbase;

⚠️在ROLLBACK;执行之前同一连接进程查询不到id为3和4的数据。回滚执行之后,数据可查询到。

例2:

BEGIN;
delete from tbase where id =3;
select * from tbase;
savepoint A;
delete from tbase where id =4;
select * from tbase;
ROLLBACK to A;
COMMIT;

⚠️在ROLLBACK to A;执行之前同一进程查询不到id为3和4的数据。回滚到保存点之后,id为3数据可查询到。ROLLBACK to savepoint_name;不会结束事务,需使用COMMIT;提交事务。