且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

自引用外键的含义是什么?

更新时间:2022-10-18 10:50:49

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.

相关阅读

技术问答最新文章