SQLite触发器是否保证始终通过其相应的交易执行?
如果我有“ my_table”表,并且是在“ my_table”上插入“ my_table”上的触发器,将记录插入“另一个_table”中,那么如果程序崩溃到“ my_table”和触发操作之间会发生什么?从任何时候失败都会流产触发器和更新的意义上,整个交易原子是吗?
陈述的一个示例:
CREATE TRIGGER trigger AFTER INSERT ON my_table
BEGIN
DELETE FROM another_table WHERE id = NEW.id;
INSERT INTO another_table VALUES(
NEW.id,
IF(NEW.a is NULL, OLD.a, NEW.a)
);
INSERT INTO another_table VALUES(
NEW.id,
IF(NEW.b is NULL, OLD.b, NEW.b)
);
END
INSERT INTO my_table VALUES (1, 2, 3);
If I have the table "my_table" and a trigger for updates or inserts on "my_table" that inserts records into "another_table", what happens if the program crashes between the changes to "my_table" and the trigger operations? Is the whole transaction atomic, in the sense that a failure at any point will abort both the trigger and update?
An example of the statements:
CREATE TRIGGER trigger AFTER INSERT ON my_table
BEGIN
DELETE FROM another_table WHERE id = NEW.id;
INSERT INTO another_table VALUES(
NEW.id,
IF(NEW.a is NULL, OLD.a, NEW.a)
);
INSERT INTO another_table VALUES(
NEW.id,
IF(NEW.b is NULL, OLD.b, NEW.b)
);
END
INSERT INTO my_table VALUES (1, 2, 3);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
sqlite文档 详细解释了原子订单的工作原理。没有提及触发器。
我相信这是因为触发器没有什么特别的:交易处理数据库的变化相同,无论是源于语句还是触发器。
因此,如果在语句和触发器之间发生某些事情(包括操作系统崩溃或电源故障),则所有更改将回滚。
实际上,我认为考虑触发器发生在之后,该语句已经完成,特别是“每行”。
The SQLite documentation explains at length how atomic commit works. There is no mention of triggers.
I believe this is because there is nothing special with triggers : transactions handle database changes the same, whether they originate from statements or from triggers.
So if something happens (including operating system crash or power failure) between the statement and the trigger, all the changes will be rolled back.
In fact I think it doesn't really make sense to consider that the trigger happens after the statement has completed, specially with 'FOR EACH ROW'.