SQL:Add foreign key in exist table

本文介绍了解决SQL Server中创建外键约束时遇到的错误Msg1776的方法。主要原因是被引用的列不是主键。文章提供了通过创建主键约束或唯一约束来解决此问题的步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

转载于:https://www.cnblogs.com/dmgactive/archive/2011/06/07/2074392.html

### Error updating database. Cause: java.sql.SQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`ry-vue`.`sys_score`, CONSTRAINT `fk_course_name` FOREIGN KEY (`course_name`) REFERENCES `sys_course` (`course_name`) ON DELETE RESTRICT ON UPDATE CASCADE) ### The error may exist in file [C:\Users\Admin\Downloads\RuoYi-Vue-master\ruoyi-admin\target\classes\mapper\score\SysScoreMapper.xml] ### The error may involve com.ruoyi.score.mapper.SysScoreMapper.updateSysScore-Inline ### The error occurred while setting parameters ### SQL: update sys_score SET student_name = ?, course_name = ?, regular_score = ?, exam_score = ?, create_time = ? where id = ? ### Cause: java.sql.SQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`ry-vue`.`sys_score`, CONSTRAINT `fk_course_name` FOREIGN KEY (`course_name`) REFERENCES `sys_course` (`course_name`) ON DELETE RESTRICT ON UPDATE CASCADE) ; Cannot add or update a child row: a foreign key constraint fails (`ry-vue`.`sys_score`, CONSTRAINT `fk_course_name` FOREIGN KEY (`course_name`) REFERENCES `sys_course` (`course_name`) ON DELETE RESTRICT ON UPDATE CASCADE); nested exception is java.sql.SQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`ry-vue`.`sys_score`, CONSTRAINT `fk_course_name` FOREIGN KEY (`course_name`) REFERENCES `sys_course` (`course_name`) ON DELETE RESTRICT ON UPDATE CASCADE)
04-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值