2013-11-09 46 views
0

我試過創建一個表,有2個外鍵但是當我試圖寫入到mysql時,出現錯誤提示「ERROR 1005(HY000):Can not創建表(errno:150)「有人幫我嗎?這裏是我的表:用外鍵創建sql表錯誤號:150

create table store (
    StoreName varchar(255), 
    Category varchar(20), 
    primary key(StoreName,Category)); 




create table shopps (
StoreName varchar(255), 
Category varchar(20), 
primary key(StoreName,Category), 
foreign key(StoreName) References store); 
foreign key(Category) References store); 
+0

在未經編輯的問題是另一個名稱(購物)的表格。您確定發生錯誤是因爲商店或購物? – LINQ2Vodka

回答

0

由於您的store表有一個複合主鍵,外鍵在shopps表引用它必須是複合材料,以及:

create table shopps (
    StoreName varchar(255), 
    Category varchar(20), 
    primary key(StoreName,Category), 
    foreign key(StoreName,Category) References store (StoreName,Category) 
) 

Demo on sqlfiddle.