SQL Server 2008:表中的列与现有主键或唯一约束不匹配 [英] SQL Server 2008: The columns in table do not match an existing primary key or unique constraint

查看:1990
本文介绍了SQL Server 2008:表中的列与现有主键或唯一约束不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要对SQL Server 2008数据库进行一些更改。

这需要创建一个新表,并在新表中插入一个引用已有表的主键的外键。所以我想在我的新tblTwo之间建立一个关系,它引用了tblOne的主键。但是,当我试图做到这一点(通过SQL Server管理工作室),我得到了以下错误:




表'tblOne'中的列不是
匹配一个现有的主键或
UNIQUE约束


我不太确定这是什么意思,我想知道是否有任何解决方法? 解决方案

这意味着tblOne中的主键还没有被正确的声明 - 你需要去tblOne并且把PRIMARY KEY约束添加回去。



如果你'确保tblOne确实有一个PRIMARY KEY约束,那么你的数据库中可能有多个tblOne表,属于不同的模式,你的FK约束中的引用子句选错了一个。



如果有一个组合键(您的注释会显示),则必须在外键引用中同时包含这两列。请注意,一个表不能有多个主键 - 但是如果它有一个组合键,则会在主键的每一列旁边看到一个键符号。


I need to make some changes to a SQL Server 2008 database.

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.

However when I tried to do this (through SQL Server Management Studio) I got the following error:

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?

解决方案

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.

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.

这篇关于SQL Server 2008:表中的列与现有主键或唯一约束不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆