SQL Server 2008:表中的列与现有的主键或唯一约束不匹配
我需要对 SQL Server 2008 数据库进行一些更改.
I need to make some changes to a SQL Server 2008 database.
这需要创建一个新表,并在新表中插入一个引用现有表的主键的外键.所以我想在我的新 tblTwo 之间建立一个关系,它引用了 tblOne 的主键.
This requires the creation of a new table, and inserting a foreign key in the new table that references the Primary key of an already existing table. So I want to set up a relationship between my new tblTwo, which references the primary key of tblOne.
但是,当我尝试执行此操作(通过 SQL Server Management Studio)时,出现以下错误:
However when I tried to do this (through SQL Server Management Studio) I got the following error:
表 'tblOne' 中的列不匹配现有的主键或唯一约束
The columns in table 'tblOne' do not match an existing primary key or UNIQUE constraint
我不太确定这意味着什么,我想知道是否有任何解决方法?
I'm not really sure what this means, and I was wondering if there was any way around it?
推荐答案
说明 tblOne 中的主键没有正确声明 - 你需要去 tblOne 并重新添加 PRIMARY KEY 约束.
It means that the primary key in tblOne hasn't been properly declared - you need to go to tblOne and add the PRIMARY KEY constraint back onto it.
p>
如果您确定 tblOne 确实具有 PRIMARY KEY 约束,那么您的数据库中可能有多个 tblOne 表,属于不同的模式,并且您的 FK 约束中的引用子句选择了错误的.
If you're sure that tblOne does have a PRIMARY KEY constraint, then maybe there are multiple tblOne tables in your DB, belonging to different schemas, and your references clause in your FK constraint is picking the wrong one.
如果有一个复合键(您的评论会指出),那么您还必须在外键引用中包含这两列.请注意,一个表不能有多个主键 - 但如果它有一个复合键,您会在作为主键一部分的每一列旁边看到一个键符号.
If there's a composite key (which your comment would indicate), then you have to include both columns in your foreign key reference also. Note that a table can't have multiple primary keys - but if it has a composite key, you'll see a key symbol next to each column that is part of the primary key.
相关文章