如何从父表中删除只有在父表中的行

如何从父表中删除只有在父表中的行

本文介绍了如何从父表中删除只有在父表中的行,这是由子表的Foregin键引用的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想从父表中删除一行/元组,但是它会抛出一个错误信息,因为它的子表中有一个FOREIGN KEY引用。然而,在我的情况下,我想要删除只从父表中的记录,并维护子表中的数据



是否有可能实现这一点?



我知道,但我想知道是否有是我所描述的场景的解决方案吗?

解决方案

这可能与您的数据中的一些协议。要维护子表数据,您必须执行 ON DELETE SET NULL 。这将保留数据,但将FK设置为​​ NULL 值(在子表中)。这是因为数据完整性:当您可以保存您的数据时,您的FK 不能引用不存在的父表中的行来执行 FK 约束。因此,它会被设置为 NULL



如果您想保存值的FK - 那么你绝对不应该使用FK,因为这样的行为违反了FK。那么就不要使用这个约束,但要注意可能的完整性失败。


I want to delete a row/tuple from a parent table, but it is throwing an error message because it has a FOREIGN KEY reference in its child table.

However, in my case I want to delete the record only from the parent table and maintain the data in the child table.

Is it possible to achieve this?

I know the usage of ON DELETE CASCADE, but I want to know if there is a solution for the secenario I described?

解决方案

It is possible with some agreements in your data. To maintain child table data you'll have to do ON DELETE SET NULL. This will leave data, but set FK to NULL value (in child table). And that is because of data-integrity: while you can keep your data, your FK can not refer to non-existent row of parent table in terms of enforcing FK constraint. Thus, it will be set to NULL by this.

If you want to "save" value of FK - then you definitely should not use FK at all because such behavior violates what FK is. So then just don't use that constraint, but be aware of possible integrity fails.

这篇关于如何从父表中删除只有在父表中的行,这是由子表的Foregin键引用的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 01:30