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

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

问题描述

我去了一个遗留数据库,发现一些引用自己​​的列的外键。被引用的列是主键列。
$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ [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])

这个外键是完全多余的,只是把它删掉而已。它不能被违反,因为一行匹配自己验证约束。

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



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

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])

What is the meaning of it?

解决方案

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.

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

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天全站免登陆