'Are sqlite triggers guaranteed to always be executed with their corresponding transaction?
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);
Solution 1:[1]
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'.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | bwt |
