添加外键出现There are no primary or candidate keys in the referenced table ×××××的错误
2012-10-10 21:36
591 查看
有时候在数据表中天叫一个外键的时候,居然弹出了:
There are no primary or candidate keys in the referenced table ×××××
的错误提示!!!
这时要怎么解决呢??
我在网上看到了一篇博文(http://www.cnblogs.com/dmgactive/archive/2011/06/07/2074392.html),有些很好的解决办法:
SQL:Add foreign key in exist table
Error Message:
Server: Msg 1776, Level 16, State 1, Line 1
There are no primary or candidate keys in the referenced
table 'Table Name' that match the referencing
column list in the foreign key 'Foreign Key Constraint
Name'.
Causes:
This error is encountered when creating a FOREIGN KEY constraint on a table and the column being referenced as a FOREIGN KEY is not a PRIMARY KEY on the other table.
To illustrate, let’s say you have the following table definition:
CREATE TABLE [dbo].[Department] (
[DepartmentID] INT NOT NULL IDENTITY,
[DepartmentName] VARCHAR(50)
)
GO
CREATE TABLE [dbo].[Employee] (
[EmployeeID] INT NOT NULL IDENTITY,
[FirstName] VARCHAR(50),
[LastName] VARCHAR(50),
[DepartmentID] INT
)
Based on the business requirements, an employee can only belong to one department. To make sure that the DepartmentID assigned to the employee exists in the [dbo].[Department] table, you create a FOREIGN KEY constraint on the column:
ALTER TABLE [dbo].[Employee]
ADD CONSTRAINT [FK_Employee_Department]
FOREIGN KEY ( [DepartmentID] ) REFERENCES [dbo].[Department] ( [DepartmentID] )
But since the DepartmentID in the [dbo].[Department] is not designated as a PRIMARY KEY on that table, the following error is encountered:
Server: Msg 1776, Level 16, State 1, Line 1
There are no primary or candidate keys in the referenced table 'dbo.Department'
that match the referencing column list in the foreign key 'FK_Employee_Department'.
Server: Msg 1750, Level 16, State 1, Line 1
Could not create constraint. See previous errors.
Solution/Workaround:
To avoid this error, you have to first create the PRIMARY KEY constraint in the table to be referenced in a FOREIGN KEY constraint.
ALTER TABLE [dbo].[Department]
ADD CONSTRAINT [PK_Department] PRIMARY KEY ( [DepartmentID] )
GO
ALTER TABLE [dbo].[Employee]
ADD CONSTRAINT [FK_Employee_Department]
FOREIGN KEY ( [DepartmentID] ) REFERENCES [dbo].[Department] ( [DepartmentID] )
GO
If the table to be referenced by the FOREIGN KEY constraint already has a PRIMARY KEY and it’s not the column to be referenced by the FOREIGN KEY, you can create a UNIQUE index or UNIQUE constraint on the column.
Using a UNIQUE index:
CREATE UNIQUE INDEX [IX_DepartmentID]
ON [dbo].[Department] ( [DepartmentID] )
GO
ALTER TABLE [dbo].[Employee]
ADD CONSTRAINT [FK_Employee_Department]
FOREIGN KEY ( [DepartmentID] ) REFERENCES [dbo].[Department] ( [DepartmentID] )
GO
Using a UNIQUE constraint:
ALTER TABLE [dbo].[Department]
ADD CONSTRAINT [IX_DepartmentID] UNIQUE ( [DepartmentID] )
GO
ALTER TABLE [dbo].[Employee]
ADD CONSTRAINT [FK_Employee_Department]
FOREIGN KEY ( [DepartmentID] ) REFERENCES [dbo].[Department] ( [DepartmentID] )
GO
There are no primary or candidate keys in the referenced table ×××××
的错误提示!!!
这时要怎么解决呢??
我在网上看到了一篇博文(http://www.cnblogs.com/dmgactive/archive/2011/06/07/2074392.html),有些很好的解决办法:
SQL:Add foreign key in exist table
Error Message:
Server: Msg 1776, Level 16, State 1, Line 1
There are no primary or candidate keys in the referenced
table 'Table Name' that match the referencing
column list in the foreign key 'Foreign Key Constraint
Name'.
Causes:
This error is encountered when creating a FOREIGN KEY constraint on a table and the column being referenced as a FOREIGN KEY is not a PRIMARY KEY on the other table.
To illustrate, let’s say you have the following table definition:
CREATE TABLE [dbo].[Department] (
[DepartmentID] INT NOT NULL IDENTITY,
[DepartmentName] VARCHAR(50)
)
GO
CREATE TABLE [dbo].[Employee] (
[EmployeeID] INT NOT NULL IDENTITY,
[FirstName] VARCHAR(50),
[LastName] VARCHAR(50),
[DepartmentID] INT
)
Based on the business requirements, an employee can only belong to one department. To make sure that the DepartmentID assigned to the employee exists in the [dbo].[Department] table, you create a FOREIGN KEY constraint on the column:
ALTER TABLE [dbo].[Employee]
ADD CONSTRAINT [FK_Employee_Department]
FOREIGN KEY ( [DepartmentID] ) REFERENCES [dbo].[Department] ( [DepartmentID] )
But since the DepartmentID in the [dbo].[Department] is not designated as a PRIMARY KEY on that table, the following error is encountered:
Server: Msg 1776, Level 16, State 1, Line 1
There are no primary or candidate keys in the referenced table 'dbo.Department'
that match the referencing column list in the foreign key 'FK_Employee_Department'.
Server: Msg 1750, Level 16, State 1, Line 1
Could not create constraint. See previous errors.
Solution/Workaround:
To avoid this error, you have to first create the PRIMARY KEY constraint in the table to be referenced in a FOREIGN KEY constraint.
ALTER TABLE [dbo].[Department]
ADD CONSTRAINT [PK_Department] PRIMARY KEY ( [DepartmentID] )
GO
ALTER TABLE [dbo].[Employee]
ADD CONSTRAINT [FK_Employee_Department]
FOREIGN KEY ( [DepartmentID] ) REFERENCES [dbo].[Department] ( [DepartmentID] )
GO
If the table to be referenced by the FOREIGN KEY constraint already has a PRIMARY KEY and it’s not the column to be referenced by the FOREIGN KEY, you can create a UNIQUE index or UNIQUE constraint on the column.
Using a UNIQUE index:
CREATE UNIQUE INDEX [IX_DepartmentID]
ON [dbo].[Department] ( [DepartmentID] )
GO
ALTER TABLE [dbo].[Employee]
ADD CONSTRAINT [FK_Employee_Department]
FOREIGN KEY ( [DepartmentID] ) REFERENCES [dbo].[Department] ( [DepartmentID] )
GO
Using a UNIQUE constraint:
ALTER TABLE [dbo].[Department]
ADD CONSTRAINT [IX_DepartmentID] UNIQUE ( [DepartmentID] )
GO
ALTER TABLE [dbo].[Employee]
ADD CONSTRAINT [FK_Employee_Department]
FOREIGN KEY ( [DepartmentID] ) REFERENCES [dbo].[Department] ( [DepartmentID] )
GO
相关文章推荐
- SQL Server Error Messages - Msg 1776 : There are no primary or candidate keys in the referenced Table
- tomcat添加项目(Add and Remove...)出现There are no resources that can be added or removed from the server
- Tomcat添加web项目出现There are no resources that can be added or removed from the server
- in the prefix, you've chosen, there are no KDE headers installed.
- tomcat启动时出现There are no resources that can be added or removed from the server.
- Myeclipse2016 部署webapp 至 tomcat 上出现 “There are no resources that can be added or removed from the server”
- 部署webapp 至 tomcat 上出现 “There are no resources that can be added or removed from the server”
- 常见错误tomcat部署项目报错There are no resources that can be added or removed from the server
- No grammar constraints (DTD or XML Schema) referenced in the document. java项目xml文件报错或者出现警告
- eclipse出现There are no resources that can be added or removed from the server
- truncate table 错误 ORA-02266: unique/primary keys in table referenced by enabled foreign keys
- xml文件出现警告:No grammar constraints (DTD or XML Schema) referenced in the document.
- eclise 部署web工程报 There are no resources that can be added or removed from the server.
- PHP 出现 ‘continue’ not in the ‘loop’ or ‘switch’ context错误
- <<< Tomcat 部署项目There are no resources that can be added or removed from the server
- ubuntu添加qmake 出现错误 qmake: could not exec '/usr/lib/x86_64-linux-gnu/qt4/bin/qmake': No such file or
- .net4 wpf App 使用log4net 错误:The type or namespace name 'log4net' could not be found (are you missing a using directive or an assembly reference?)
- VS2010 The project file ' ' has been renamed or is no longer in the solution
- Eclipse部署项目报There are no resources that can be added or removed from the server.
- ORA-02266: unique/primary keys in table referenced by enabled foreign keys