sql – INSERT语句与FOREIGN KEY SAME TABLE约束冲突

前端之家收集整理的这篇文章主要介绍了sql – INSERT语句与FOREIGN KEY SAME TABLE约束冲突前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我创建了表Employee
Create table Employee
(   
    FName varchar(20) Not Null,LName varchar(20) Not Null,SSN int Not Null,BDate Datetime,Address varchar(50),Sex char(1),Salary money,Super_SSN int,Primary Key(SSN),Foreign Key(Super_SSN) references Employee(SSN)
)
@H_403_4@当我尝试插入第一行时,

insert into Employee(FName,LName,SSN,BDate,Address,Sex,Salary,Super_SSN)
values('John','Smith',12345,'1965-01-09','Houston,TX','M',30000,33344)
@H_403_4@我收到的错误就像..

@H_403_4@错误

@H_403_4@The INSERT statement conflicted with the FOREIGN KEY SAME TABLE constraint “FK_Employee_Employee”. The conflict occurred in database “Company”,table “dbo.Employee”,column ‘SSN’.

解决方法

您需要首先对SSN’33344’进行INSERT记录,并将Super_SSN值设置为NULL.
INSERT INTO  Employee(FName,Super_SSN)
VALUES (<FName>,<LName>,'33344',<BDate>,<Address>,<Sex>,<Salary>,NULL)
@H_403_4@插入之后

INSERT INTO Employee (FName,Super_SSN)
VALUES ('John',33344)
@H_403_4@如果SSN’33344’具有任何Super_SSN,则更新SSN值(该记录应在表中可用).

原文链接:/mssql/76451.html

猜你在找的MsSQL相关文章