简体   繁体   English

SQL Server外键问题

[英]SQL Server Foreign Key Problem

I am trying to create a foreign key constraint consisting of an int column and a datetime column (both not null). 我试图创建一个由int列和datetime列(都不为null)组成的外键约束。

I am getting the error "There are no primary or candidate keys in the referenced table X that match the referencing column list in the foreign key FK". 我收到错误“在引用表X中没有与外键FK中的引用列列表匹配的主键或候选键”。

Normally this error means that the column(s) in the target table is not unique. 通常,此错误意味着目标表中的列不是唯一的。 However it is the primary key so it is definitely unique. 但是,它是主键,因此绝对是唯一的。

The table which the foreign key is referencing is partitioned across multiple file groups - whereas the table on which the foreign key is being created resides on the primary file group. 外键引用的表被划分为多个文件组-而要在其上创建外键的表则位于主文件组上。 Could this be the problem? 这可能是问题吗? Can datetimes be part of a foreign key? 日期时间可以作为外键的一部分吗?

Table X: 表X:

LineId (int not null) (PK)
CreatedAt (datetime not null) (PK)
Message (nvarchar(max))
userId (int not null)

Table Y: 表Y:

LineId (int not null) (PK) (FK)
SId (int not null) (PK)
LineCreatedAt (datetime not null) (FK)
CreatedAt (datetime not null)

SQL command: SQL命令:

ALTER TABLE Y
ADD CONSTRAINT [FK1] 
FOREIGN KEY(LineId,LineCreatedAt)
REFERENCES X(LineId, CreatedAt)

Any ideas appreciated. 任何想法表示赞赏。

Thanks 谢谢

No, the error you receive does not mean that the columns are not unique. 不,您收到的错误并不意味着这些列不是唯一的。 It means that the columns you're referencing to, do not match with the columns in your foreign table. 这意味着您引用的列与外部表中的列不匹配。 Can you display the 2 tables and your FK definition ? 您可以显示2个表格和FK定义吗?

I have tested this in SQL2005 Dev edition, with both tables X and Y created on PRIMARY file group. 我已经在SQL2005 Dev版中对此进行了测试,并在PRIMARY文件组上创建了表X和Y。 I had no trouble (as I suspected I wouldn't) in creating the foreign key you describe. 在创建您描述的外键时,我没有遇到任何麻烦(因为我怀疑我不会这样做)。 I also doubt (but I'm a lot less sure about) that having the table partitioned on multiple file groups should affect this in this manner. 我也怀疑(但是我不太确定)将表分区到多个文件组上是否会以这种方式影响这一点。

Can you please provide the SQL you used to create your tables? 您能否提供用于创建表的SQL? This may shed some light on the cause of your problems, particularly if there are some wierd settings which are active (and SQL-Server is chock full of wierd settings.) 这可能可以弄清问题的原因,特别是如果有一些处于活动状态的奇怪设置(并且SQL-Server充满了奇怪的设置)。

Apparently the problem was in the ordering of the columns within the keys. 显然问题出在键中列的顺序上。 Since the primary key of table X was (CreatedAt,LineId), changing the script to: 由于表X的主键是(CreatedAt,LineId),因此将脚本更改为:

ALTER TABLE Y
ADD CONSTRAINT [FK1] 
FOREIGN KEY(LineCreatedAt,LineId)
REFERENCES X(CreatedAt,LineId)

worked successfully. 工作成功。 A bad mistake to make, but when looking at the object explorer you only see the key columns, not their order within the key. 这是一个很糟糕的错误,但是在查看对象资源管理器时,您只会看到键列,而不是键在键中的顺序。

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

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