删除作为另一个表的外键的主键行 [英] delete primary key row which is foreign key of another table

查看:36
本文介绍了删除作为另一个表的外键的主键行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设有一个包含主键的主表,还有另一个包含该主表的外键的表.所以如果我们只删除主表的行而不删除子表.

Suppose there is a main table containing a primary key and there is another table which contains a foreign key to this main table. So if we delete the row of main table only not child table.

推荐答案

你应该在你的 FKey 上设置一些特定的选项,比如 ON DELETE {CASCADE, SET NULL, SET DEFAULT}

You should set some specific options on your FKey, such as ON DELETE {CASCADE, SET NULL, SET DEFAULT}

相反,您将无法删除引用的行,因为由于引用完整性,它被 sql server 禁止.

Instead you'll not be able to delete referenced row, because it is prohibited by sql server due to referrential integrity.

因此,选项是将引用表的值设置为 NULL 或任何其他 DEFAULT 值.

So, the option is to set referencing table's value to NULL or any other DEFAULT value.

或者也删除它

或者,如果您的引用行在没有父行的情况下具有某种意义 - 那么您的数据库设计有问题 - 您不需要 FKey 或架构不是标准化.

Or, if your referencing row has some meaning without parent row - then something is wrong with your DB design - either you do not need the FKey or schema is not normalized.

这篇关于删除作为另一个表的外键的主键行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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