Question about SQL triggers
Say there are two relations r and s such that the foreign key B of r references the primary key A 开发者_运维百科of s. How can the trigger mechanism be used to implement the on delete cascade option, when a tuple is deleted from s.
In SQLite syntax:
CREATE TRIGGER
AFTER DELETE ON s
FOR EACH ROW
BEGIN
DELETE FROM r WHERE r.B = old.A;
END;
This creates a trigger that run on each row deleted from s. The trigger deletes the corresponding records from r, given the foreign key relationship you indicated.
精彩评论