条件CASCADE操作的外键约束? [英] Conditional CASCADE operation for foreign key constraint?
问题描述
我有父母
和小孩
表格其中小孩
有一个FK指向父表的PK。当我删除父表中的某些内容时,我可以通过删除 ON DELETE CASCADE
。
I have parent
and child
table where child
has a FK pointing to the PK of parent table. When I delete something in parent table I can have child records deleted as well by having ON DELETE CASCADE
.
来删除子记录。 ,在我的父母
表中我根本不删除记录。相反,我设置了列 state =passive
。我想删除子
表中的相关条目。
However, in my parent
table I don't delete records at all. Instead I set the column state = "passive"
. I want to delete related entries in the child
table.
我们是否有类似于conditional CASCADE Postgres的?或者是手动删除子元素
表中的元素的解决方案?
Do we have something like a "conditional CASCADE" in Postgres? Or is the solution to manually delete entries in the child
table?
推荐答案
你将不得不在一个触发器上执行这个操作ON UPDATE。在NEW.state =passive的地方,删除子行。
You would have to do this in a trigger that takes action ON UPDATE. Where the NEW.state = "passive", delete the child rows.
这篇关于条件CASCADE操作的外键约束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!