gpt4 book ai didi

sql - ALTER TABLE 语句与 FOREIGN KEY 约束冲突

转载 作者:太空狗 更新时间:2023-10-30 01:36:51 26 4
gpt4 key购买 nike

为什么向tblDomare 表添加外键会导致此错误?

The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK__tblDomare__PersN__5F7E2DAC". The conflict occurred in database "almu0004", table "dbo.tblBana", column 'BanNR'.

代码

CREATE TABLE tblDomare
(PersNR VARCHAR (15) NOT NULL,
fNamn VARCHAR (15) NOT NULL,
eNamn VARCHAR (20) NOT NULL,
Erfarenhet VARCHAR (5),
PRIMARY KEY (PersNR));

INSERT INTO tblDomare (PersNR,fNamn,eNamn,Erfarenhet)
Values (6811034679,'Bengt','Carlberg',10);

INSERT INTO tblDomare (PersNR,fNamn,eNamn,Erfarenhet)
Values (7606091347,'Josefin','Backman',4);

INSERT INTO tblDomare (PersNR,fNamn,eNamn,Erfarenhet)
Values (8508284163,'Johanna','Backman',1);

CREATE TABLE tblBana
(BanNR VARCHAR (15) NOT NULL,
PRIMARY KEY (BanNR));

INSERT INTO tblBana (BanNR)
Values (1);

INSERT INTO tblBana (BanNR)
Values (2);

INSERT INTO tblBana (BanNR)
Values (3);

ALTER TABLE tblDomare
ADD FOREIGN KEY (PersNR)
REFERENCES tblBana(BanNR);

最佳答案

这是因为您尝试创建一个从 tblDomare.PersNRtblBana.BanNR 的外键,但是/和 tblDomare.PersNR 中的值> 与 tblBana.BanNR 中的任何值都不匹配。您不能创建违反参照完整性的关系。

关于sql - ALTER TABLE 语句与 FOREIGN KEY 约束冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21839309/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com