简体   繁体   English

添加自引用外键SQL Server时出错

[英]Error When Adding Self-Refrencing Foreign Key SQL Server

I have a table called User that has columns like: 我有一个名为User的表,其表如下:

|UserID numeric(18,0)
|...
|SupervisorUserID numeric(18,0)

UserID is the primary key and I have other foreign key relationships working (table to table). UserID是主键,我还有其他的外键关系(表对表)起作用。 But when I try to add a relation between SupervisorUserID and UserID I get the following error in SQL Server Management Studio: 但是,当我尝试在SupervisorUserIDUserID之间添加关系时,在SQL Server Management Studio中收到以下错误:

SQL Server Management Studio错误

What could be the issue here? 这里可能是什么问题?

The message looks quite straightforward. 该消息看起来非常简单。 Check if your index is unique. 检查您的索引是否唯一。

When you create a foreign key relationship in the same table make sure you put the primary key field under "Primary key table" and the foreign key field under "Foreign key table". 在同一表中创建外键关系时,请确保将主键字段放在“主键表”下,并将外键字段放在“外键表”下。 Doing the reverse will give you the error described. 进行相反操作将给您所述的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM