自引用外键的含义是什么? [英] What is the meaning of self referencing foreign key?
问题描述
$ 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屋!