开发者

Question about SQL triggers

开发者 https://www.devze.com 2023-01-26 19:34 出处:网络
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, w

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消