自引用外键是什么意思? [英] What is the meaning of self referencing foreign key?

查看:37
本文介绍了自引用外键是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我检查了一个遗留数据库,发现有几个外键引用了一个列本身.引用的列是主键列.

I went over a legacy database and found a couple of foreign keys that reference a column to itself. The referenced column is the primary key column.

ALTER TABLE [SchemaName].[TableName]  WITH CHECK ADD  
CONSTRAINT [FK_TableName_TableName] FOREIGN KEY([Id])
REFERENCES [SchemaName].[TableName] ([Id])

这是什么意思?

推荐答案

ALTER TABLE [SchemaName].[TableName]  WITH CHECK ADD  
CONSTRAINT [FK_TableName_TableName] FOREIGN KEY([Id])
REFERENCES [SchemaName].[TableName] ([Id])

这个外键完全是多余的,没有意义,只要删除它.它永远不会被违反,因为一行匹配自己验证约束.

This foreign key is completely redundant and pointless just delete it. It can never be violated as a row matches itself validating the constraint.

在分层表中,关系将在两个不同的列之间(例如 IdParentId)

In a hierarchical table the relationship would be between two different columns (e.g. Id and ParentId)

至于为什么它很可能是通过使用可视化设计器创建的,如果您在对象资源管理器中右键单击Keys"节点并选择New Foreign Key"然后关闭对话框而不删除创建的外键和然后在打开的表设计器中进行一些其他更改并保存它会创建这种冗余约束.

As for why it may have been created quite likely through use of the visual designer if you right click the "Keys" node in object explorer and choose "New Foreign Key" then close the dialogue box without deleting the created foreign key and then make some other changes in the opened table designer and save it will create this sort of redundant constraint.

这篇关于自引用外键是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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