How can all writes from a failed import batch be undone together?

An explicit transaction groups writes behind one commit boundary. ROLLBACK discards its uncommitted changes.

SQLite example and interpretation

The staging table exists before the transaction. After two writes and a simulated validation failure, rollback restores zero rows while the table remains.

CREATE TABLE st(id INTEGER);
BEGIN;
INSERT INTO st VALUES(1),(2);
ROLLBACK;
SELECT count(*) FROM st;

Result [[0]]

中文

如何让一批未通过校验的导入全部撤销?

显式事务把多条写操作包在一个提交边界内,ROLLBACK 撤销该事务中尚未提交的修改。

SQLite 示例与适用条件

暂存表先存在,再在一个事务内写两行并模拟质检失败。撤销后计数回到零;事务外的表仍存在。

CREATE TABLE st(id INTEGER);
BEGIN;
INSERT INTO st VALUES(1),(2);
ROLLBACK;
SELECT count(*) FROM st;

结果 [[0]]

References / 参考来源

Keep reading / 继续阅读