条件CASCADE操作的外键约束? [英] Conditional CASCADE operation for foreign key constraint?

查看:200
本文介绍了条件CASCADE操作的外键约束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有父母小孩表格其中小孩有一个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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆