2017-05-29 142 views
0

在SQL Server 2012管理工作室中,我嘗試了很多時間來創建一些表並在表中插入一些值,但這裏的問題與表關聯:Msg 547,INSERT語句與FOREIGN KEY約束衝突

INSERT語句衝突與外鍵約束

的錯誤:

消息8152,級別16,狀態14,行116字符串或比娜ry的數據將被截斷爲 。

Msg 547,Level 16,State 0,Line 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, 
 
\t floor int, 
 
\t room_type varchar(8) 
 
\t 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',10,'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

此代碼是簡單的調試,你一定能做出來。運行它自己的每條語句並查看其結果。如果失敗,則將其拆分爲更小的語句並重復。如果你不能分割它,它已經足夠小,可以通過視力進行調試,不需要很長時間就可以確定問題。

在調試之前,要知道這一點,從頭開始重新啓動有時是有用/必要的,因此準備好重新啓動腳本。在這種情況下,只有一堆DROP TABLE是必要的,他們將刪除表和他們的記錄。

您的CREATE TABLE聲明工作正常,這是一件事通過。即使你遇到了問題,也要逐個運行它們來調試,而不是一次全部運行。

您的INSERT有問題,是的。對每個INSERT塊逐個運行,對於失敗的塊,將其拆分爲單個INSERT語句,每個記錄一個,現在您可以分辨哪一行是問題。

即使你引用某些外鍵衝突問題,我得到了一堆不同的外鍵問題。仔細檢查你提供的代碼。另外, 什麼聲明的順序是首先運行(,我可以提供我家的三樓,而我甚至沒有三樓和二樓?你可以取消預訂,你還沒有預訂?

最後,數據似乎有截斷問題,無論表列是否應該更大或記錄的值是不正確的,只有你可以告訴。

相關問題