2014-03-24 123 views
0

我不能創建表Item_CopyERROR 1005(HY000):無法創建表「Item_Copy」(錯誤:150)無法創建表Item_Copy

CREATE TABLE Library 
(
Library_ID int primary key, 
Library_Address varchar(40), 
Library_Phone bigint 
); 

CREATE TABLE Branch 
(
Library_ID int, 
Branch_Number int, 
Branch_Name varchar(40), 
Branch_Address varchar(40), 
Branch_Phone bigint, 
Branch_Hours varchar(40), 
primary key (Library_ID,Branch_Number), 
foreign key (Library_ID) references Library(Library_ID) 
); 


CREATE TABLE Item_Copy 
(
Item_ID int primary key, 
Copy_Number int, 
Copy_Condition varchar(40), 
Copy_Date_Acquired date, 
Copy_Cost int, 
Library_ID int, 
Branch_Number int, 
foreign key (Library_ID) references Branch(Library_ID), 
foreign key (Branch_Number) references Branch(Branch_Number), 
foreign key (Item_ID) references Item(Item_ID) 
); 
+0

只用ITEM_ID再次嘗試。如果可行,請刪除它並添加下一個字段。重複,直到找到導致錯誤的行。 –

+0

沒有涵蓋'分支(Branch_Number)'字段的索引。該問題也缺少「Item」表定義。 – zerkms

回答

0

只是一個科學的猜測(將下降,如果這是錯的)(是基於一個假設,即Item_ID定義相符):

你想要一個複合外鍵,而不是2個獨立的FKS:

foreign key (Library_ID, Branch_Number) references Branch(Library_ID, Branch_Number) 
0

要定義一個子關係,父列必須已編入索引。

在父表Branch中,將鍵定義爲組合。

primary key (Library_ID,Branch_Number), 

但是,子表Item_Copy,你正在試圖定義獨立於每個列的參考項。

foreign key (Library_ID) references Branch(Library_ID), 
foreign key (Branch_Number) references Branch(Branch_Number), 

只有當這些字段中的每一個引用相應的父鍵時纔可以接受。
Branch(Branch_Number)沒有在Branch表中定義的密鑰。因此是錯誤。

更改

foreign key (Library_ID) references Branch(Library_ID), 
foreign key (Branch_Number) references Branch(Branch_Number), 

foreign key (Library_ID, Branch_Number) 
    references Branch(Library_ID, Branch_Number), 
相關問題