简体   繁体   English

尝试在 SQL Server 2008R2 中创建新的外键关系时出错

[英]Error while trying to create a new foreign key relationship in SQL Server 2008R2

while trying to create a new foreign key constraint in SQL Server using the following syntax:尝试使用以下语法在 SQL Server 中创建新的外键约束时:

alter table fkTable add constraint FK_fkTable_pkTable 
foreign key (pkTable_Id) references pkTable(pkTable_Id)

I get the following error:我收到以下错误:

Msg 50000, Level 16, State 0, Line 218 Could not create constraint.消息 50000,级别 16,状态 0,第 218 行无法创建约束。 See previous errors.请参阅以前的错误。

this was literally the only message, there were no previous errors.这实际上是唯一的消息,之前没有错误。

The problem was that my tables were defined as:问题是我的表被定义为:

create table pkTable (
    pkTable_Id smallint not null constraint PK_pkTable primary key identity(1,1),
    --other columns...
)

create table fkTable (
    fkTable_Id uniqueidentifier not null constraint primary key,
    pkTable_Id int not null,
    --other columns...
)

Where the pkTable_Id column is defined as a smallint on the pkTable, and int on the fkTable.其中 pkTable_Id 列在 pkTable 上定义为smallint ,在 fkTable 上定义为int Might seem pretty obvious but the error was not at all helpful!可能看起来很明显,但错误根本没有帮助!

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

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