在SQL Server 2012管理工作室中,我尝试了很多时间来创建一些表并在表中插入一些值,但这里的问题与表有关:
INSERT语句与FOREIGN KEY约束冲突
错误:
消息8152,级别16,状态14,行116字符串或二进制数据将是 截断。
消息547,级别16,状态0,行122 INSERT语句冲突 使用FOREIGN KEY约束" eworkerFK"。冲突发生在 数据库" 7",表" dbo.member_clup",列' manager_id'。
声明已经终止。
Msg 547,Level 16,State 0,Line 128 INSERT语句与FOREIGN KEY约束冲突 " SALEDFk&#34 ;.冲突发生在数据库" 7",table" 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)

答案 0 :(得分:0)
这段代码很容易调试,你肯定可以解决它。在其上运行每个语句并查看其结果。如果失败,将其拆分为较小的语句并重复。如果你不能拆分它,它已经足够小,可以通过视力调试,它不会花很长时间来解决这个问题。
在调试之前,要知道这一点,从头开始重启有时是有用的/必要的,所以请准备好重启脚本。在这种情况下,只需要一堆DROP TABLE
,他们将删除表及其记录。
您的CREATE TABLE
声明工作正常,一件事情已经过去了。即使你遇到了问题,也可以逐个进行调试,而不是一次性调试。
你的INSERT
是有问题的,是的。逐个运行每个INSERT
块,对于失败的块,将其拆分为单独的INSERT
语句,每条记录一个,现在您将能够分辨哪一行是问题
即使您引用某些外键冲突问题,我也会遇到一堆不同的外键问题。仔细检查您提供的代码。也, 什么声明的顺序首先是重要的(我可以提供我家的三楼,而我甚至没有三楼和二楼?你可以取消你还没有预订的预订吗? ?)
最后,数据似乎有截断问题,无论表格列是否应该更大或者记录是什么?值不正确只是你可以说的。