MySQL:删除CHILDREN行时删除PARENTS

时间:2014-06-21 21:42:39

标签: mysql cascade

假设我有一个父表:FAMILY_MEMBER,每个都有unique ID个ID。

我还有FAMILYmother_idfather_iddaughter_id,每个表都指向FAMILY_MEMBER行。

当我删除FAMILY行时,我希望数据库自动删除3 FAMILY_MEMBER个父行。这似乎与ON DELETEON UPDATE条款的目的完全相反。

1 个答案:

答案 0 :(得分:0)

create trigger t1
    after delete on FAMILY
        for each row
            BEGIN
            delete from family_member
                where id = old.mother_id;
            delete from family_member
                where id = old.fa=ther_id;
            delete from family_member
                where id = old.daughter_id;
            END

或者这个(您可以使用分隔符作为触发器和表的名称):

create trigger `t1`
    after delete on `FAMILY`
        for each row
            BEGIN
            delete from family_member
                where id = old.mother_id;
            delete from family_member
                where id = old.fa=ther_id;
            delete from family_member
                where id = old.daughter_id;
            END