消息 547,INSERT 语句与 FOREIGN KEY 约束冲突

Posted

技术标签:

【中文标题】消息 547,INSERT 语句与 FOREIGN KEY 约束冲突【英文标题】:Msg 547, The INSERT statement conflicted with the FOREIGN KEY constraint 【发布时间】:2017-05-29 10:53:30 【问题描述】:

在 SQL Server 2012 Management Studio 中,我多次尝试创建一些表并在表中插入一些值,但这里的问题与表有关:

INSERT 语句与 FOREIGN KEY 约束冲突

错误:

消息 8152,级别 16,状态 14,行 116 字符串或二进制数据将是 被截断。

消息 547,级别 16,状态 0,第 122 行 INSERT 语句冲突 具有 FOREIGN KEY 约束“eworkerFK”。冲突发生在 数据库“7”,表“dbo.member_clup”,列“manager_id”。

语句已终止。

消息 547,级别 16,状态 0,行 128 INSERT 语句与 FOREIGN KEY 约束冲突 “SALEDFk”。冲突发生在数据库“7”,表“dbo.worker”中, 列'worker_number'。

谁能帮我将插入到正确 表?

CREATE TABLE address
(
code_place int,
PLACE_NAME VARCHAR (15),
OUT_israel varchar (15)
CONSTRAINT address1 PRIMARY KEY(code_place)

)

create table member_clup
(
manager_id int,
manager_name varchar(15),
manager_last varchar(12),
phone_num int,
type varchar(10),
CONSTRAINT manager1PK PRIMARY KEY(manager_id)
)


create table worker
(
worker_number int,
worker_name varchar(50),
worker_last varchar(49),
id varchar(9),
type varchar(50),
manager_id int
CONSTRAINT workerPK PRIMARY KEY(worker_number)
CONSTRAINT eworkerFK FOREIGN KEY(manager_id)
REFERENCES member_clup(manager_id)

)

  CREATE TABLE rooms(
     room_number int,
	 floor int,
	 room_type varchar(8)
	 CONSTRAINT roomsKEY PRIMARY KEY(room_number)

  )

 CREATE TABLE cars
(
cars_number int,
car_type varchar(15),
car_model int,
CONSTRAINT carsPK PRIMARY KEY(cars_number)
) 

CREATE TABLE tourest(
   Cust_number int ,
   Cust_name varchar(50),
   cust_lastname varchar(50),
   cust_phone varchar(10),
   code_place int,
   Room_id int,
   Saledby int
   CONSTRAINT tourest1 PRIMARY KEY(Cust_number)
   CONSTRAINT addressFK FOREIGN KEY(code_place)
   REFERENCES address(code_place),
    CONSTRAINT SALEDFk FOREIGN KEY(Saledby)
   REFERENCES worker(worker_number),
     CONSTRAINT roomsFK FOREIGN KEY(Room_id)
   REFERENCES rooms(room_number)
   )
   CREATE TABLE kesher
(
Cust_number int,
cars_number int,
CONSTRAINT custPK1 PRIMARY KEY(Cust_number,cars_number),
CONSTRAINT kesher1FK FOREIGN KEY(Cust_number)
   REFERENCES cars(cars_number),
     CONSTRAINT kesherFK FOREIGN KEY(Cust_number)
   REFERENCES tourest(Cust_number)
)

insert into cars VALUES
(100100,'mazda',2002),
(100205,'ford',2017),
(100206,'porch',1998),
(100207,'mazda',2017),
(100208,'opel',2002),
(100209,'mazda',2016),
(100210,'pijuot',2002),
(100211,'mazda',2015),
(100212,'mazda',2010),
(100213,'volvo',2002),
(100215,'ford',20012)

insert into rooms VALUES
(100,1,'single'),
(101,1,'double'),
(102,1,'single'),
(103,1,'double'),
(201,2,'signle'),
(202,2,'signle'),
(203,2,'signle'),
(204,2,'signle'),
(300,3,'double'),
(301,3,'double'),
(302,3,'double'),
(303,3,'double'),
(304,3,'signle')

insert into address VALUES
(500,'Akko','no'),
(501,'Haifa','no'),
(502,'Nahariya','no'),
(503,'Nataniya','no'),
(504,'carmieal','no'),
(505,'Nahef','no'),
(507,'Nitsrat','no'),
(510,'OUT','yes')

insert into member_clup VALUES
(5400,'shmolek','snaa','0525732572','General'),
(5696,'malloc','ali','0525552501','Rooms'),
(5991,'ramada','hassan','0532731212','Rooms & Tips'),
(5210,'meri','yako','0525022572','General Manager')

insert into worker 
VALUES(1234,'halaa','khaled',1234567,'none',5696),
(2234,'fares','adoon',6542897,'none',5696),
(6670,'halaa','khaled',1001234,'none',5991),
(2554,'halaa','khaled',5658741,'none',5210),
(9987,'halaa','khaled',1123456,'none',5400)
insert into tourest VALUES
(1510,'moshe','yke','0525732579',500,101,2234),
(1520,'ninar','lait','052655541',500,102,6670),
(1521,'hasan','ahmad','0532578741',501,101,2234),
(1522,'ameer','karm','0545222741',500,104,6670),
(1523,'aliel','sraa','0525771572',504,100,2234),
(1524,'hasa','veto','0525122579',505,303,6670),
(1525,'saed','snaa','05255632579',505,303,2234),
(1526,'yakov','mero','0528132579',502,202,6670),
(1527,'mece','loka','0525962579',502,302,9987),
(1528,'ana','yokaf','0525791179',502,302,9987),
(1529,'lelya','mandlina','0527832579',505,203,9987),
(1530,'mnal','khokha','0525758579',507,204,5991),
(1531,'moka','panana','0525805579',507,200,2234)


insert into kesher VALUES

(1510,100100),
(1520,100209),
(1521,100100),
(1522,100209),
(1523,100206),
(1524,100206),
(1525,100213),
(1526,100206),
(1527,100213),
(1528,100213),
(1529,100209)

【问题讨论】:

这能回答你的问题吗? The INSERT statement conflicted with the FOREIGN KEY constraint 【参考方案1】:

这段代码调试起来很简单,你肯定可以解决的。单独运行每个语句并查看其结果。如果失败,请将其拆分为更小的语句并重复。分不开的话,已经小到可以用肉眼调试了,用不了多久就能发现问题。

但在调试之前,请了解这一点,从头开始有时会有所帮助/必要,因此请准备好重新启动脚本。在这种情况下,只需要一堆 DROP TABLEs,它们会删除表及其记录。

您的CREATE TABLE 语句运行良好,这是通过的一件事。即使您对它们有问题,也要一个一个地运行它们进行调试,而不是一次全部运行。

你的INSERTs 有问题,是的。逐个运行每个INSERT 块,对于失败的块,将其拆分为单独的INSERT 语句,每条记录一个,现在您将能够判断哪一行是问题所在。

即使您引用了某些外键冲突问题,我也得到了一堆不同的外键问题。仔细检查您提供的代码。还, 首先运行什么语句的顺序很重要(我可以在我什至没有三楼和二楼的情况下提供我家的三楼吗?您可以取消尚未预订的预订吗?)

最后,数据似乎有截断问题,表列是否应该更大或记录的值是否不正确,只有你自己知道。

【讨论】:

以上是关于消息 547,INSERT 语句与 FOREIGN KEY 约束冲突的主要内容,如果未能解决你的问题,请参考以下文章

SqlException:INSERT 语句与使用 EF 代码优先 C# 的 FOREIGN KEY 约束冲突

当插入为 NULL 时,“INSERT 语句与 FOREIGN KEY 约束冲突”

Asp.net core Identity “INSERT 语句与 FOREIGN KEY 约束冲突”

INSERT 语句与 FOREIGN KEY 约束“FK_PostTag_Tag_TagId”冲突

SqlException:INSERT 语句与 FOREIGN KEY 约束冲突 - asp.net-core

为啥这个 Check 约束不起作用?