SQL Server无匹配键错误消息

时间:2011-12-12 07:22:28

标签: sql sql-server sql-server-2008

我正在尝试执行我通过用于Microsoft Visio的Forward Engineer Addin创建的数据库脚本。

以下是我收到的错误以及错误消息引用的代码部分:

错误1:

Msg 1776, Level 16, State 0, Line 12
There are no primary or candidate keys in the referenced table 'dbo.Account_BillingAccount' that match the referencing column list in the foreign key 'Account_BillingAccount_InvoiceDetail_FK1'.
Msg 1750, Level 16, State 0, Line 12
Could not create constraint. See previous errors.

代码参考1:

ALTER TABLE [dbo].[InvoiceDetail] WITH CHECK ADD CONSTRAINT [Account_BillingAccount_InvoiceDetail_FK1] FOREIGN KEY (
[BillingAccountNumber]
, [AccountNumber]
)
REFERENCES [dbo].[Account_BillingAccount] (
[BillingAccountNumber]
, [AccountNumber]
)

错误2:

Msg 1776, Level 16, State 0, Line 2
There are no primary or candidate keys in the referenced table 'dbo.ManagerContract' that match the referencing column list in the foreign key 'ManagerContract_RegionalCoordinators_FK1'.
Msg 1750, Level 16, State 0, Line 2
Could not create constraint. See previous errors.

代码参考2:

ALTER TABLE [dbo].[RegionalCoordinators] WITH CHECK ADD CONSTRAINT [ManagerContract_RegionalCoordinators_FK1] FOREIGN KEY (
[AssociateID]
, [WritingNumber]
)
REFERENCES [dbo].[ManagerContract] (
[AssociateID]
, [WritingNumber]
)

删除上述错误的最有效方法是什么?

1 个答案:

答案 0 :(得分:0)

您必须在引用的表中具有与列列表匹配的主键或唯一键。因此,ManagerContract AssociateID, WritingNumber Account_BillingAccount需要BillingAccountNumber, AccountNumber上的alter table dbo.ManagerContract add constraint UNQ_ManagerContract_AssociateID_WritingNumber unique (AssociateID, WritingNumber) 需要一个唯一键。{/ 1}}

在这里查看如何创建唯一约束。 http://msdn.microsoft.com/en-us/library/ms191166.aspx

  

ALTER TABLE [dbo]。[TableName] ADD CONSTRAINT   UNQ_ TableName _ColumnName UNIQUE([ColumnName])

你的情况就是这样。

{{1}}