2012-07-03 98 views
1

我想創建一系列具有多個外鍵關係的表。兩個表中的第一個有兩列主鍵,一次由我們指定,另一個由製造商指定。當嘗試第三個查詢時發生該錯誤。我不確定這裏出了什麼問題,所以任何幫助都會受到真誠的讚賞。MySQL錯誤150(外鍵問題?)

SQL

CREATE TABLE rugs (
    id INTEGER NOT NULL AUTO_INCREMENT, 
    manufacturer_id INTEGER NOT NULL, 
    name VARCHAR(255), 
    description TEXT, 
    PRIMARY KEY (id, manufacturer_id) 
); 

CREATE TABLE carpets (
    id INTEGER NOT NULL AUTO_INCREMENT, 
    manufacturer_id INTEGER NOT NULL, 
    name VARCHAR(255), 
    warranty TEXT, 
    description TEXT, 
    fiber_name VARCHAR(255), 
    brand_name VARCHAR(255), 
    texture_name VARCHAR(255), 
    PRIMARY KEY (id, manufacturer_id) 
); 

CREATE TABLE carpet_styles (
    id INTEGER NOT NULL AUTO_INCREMENT, 
    carpet_id INTEGER, 
    style_name VARCHAR(255), 
    PRIMARY KEY (id), 
    FOREIGN KEY (carpet_id) REFERENCES carpets (manufacturer_id) 
); 

CREATE TABLE carpet_colors (
    id INTEGER NOT NULL AUTO_INCREMENT, 
    carpet_id INTEGER, 
    color_name VARCHAR(255), 
    color_category VARCHAR(255), 
    PRIMARY KEY (id), 
    FOREIGN KEY (carpet_id) REFERENCES carpets (manufacturer_id) 
); 

CREATE TABLE carpet_custom_names (
    id INTEGER NOT NULL AUTO_INCREMENT, 
    carpet_id INTEGER, 
    custom_name VARCHAR(255), 
    PRIMARY KEY (id), 
    FOREIGN KEY (carpet_id) REFERENCES carpets (manufacturer_id) 
); 

CREATE TABLE rug_styles (
    id INTEGER NOT NULL AUTO_INCREMENT, 
    rug_id INTEGER, 
    style_name VARCHAR(255), 
    PRIMARY KEY (id), 
    FOREIGN KEY (rug_id) REFERENCES rugs (manufacturer_id) 
); 

CREATE TABLE rug_colors (
    id INTEGER NOT NULL AUTO_INCREMENT, 
    rug_id INTEGER, 
    color_name VARCHAR(255), 
    color_category VARCHAR(255), 
    PRIMARY KEY (id), 
    FOREIGN KEY (rug_id) REFERENCES rugs (manufacturer_id) 
); 

CREATE TABLE rug_shapes (
    id INTEGER NOT NULL AUTO_INCREMENT, 
    rug_id INTEGER, 
    shape_name VARCHAR(255), 
    shape_category VARCHAR(255), 
    PRIMARY KEY (id), 
    FOREIGN KEY (rug_id) REFERENCES rugs (manufacturer_id) 
); 

CREATE TABLE catalog_contents (
    id INTEGER NOT NULL AUTO_INCREMENT, 
    catalog_id INTEGER, 
    carpet_id INTEGER, 
    rug_id INTEGER, 
    PRIMARY KEY (id), 
    FOREIGN KEY (rug_id) REFERENCES rugs (id), 
    FOREIGN KEY (carpet_id) REFERENCES carpets (id), 
    FOREIGN KEY (catalog_id) REFERENCES catalogs (id) 
); 
+0

'顯示的InnoDB status'真正將傾一噸的信息,並在所有的中間,這將是一個「最後的外鍵錯誤」部分,用的究竟是什麼導致了更多的細節你得到的150個代碼。 –

+0

不幸的是,這不再是一個與最新版本的MySQL的選項(意思是我在發佈問題到stackoverflow.com之前試過這個) – bulletshot60

回答

2

這似乎是由於這樣的事實,雖然carpets.manufacturer_id是複合鍵的一部分,它有沒有自己的指數。您必須從複合PRIMARY KEY分開創建一個索引。這同樣適用於rugs.manufacturer_id

CREATE TABLE rugs (
    id INTEGER NOT NULL AUTO_INCREMENT, 
    manufacturer_id INTEGER NOT NULL, 
    name VARCHAR(255), 
    description TEXT, 
    /* manufacturer_id gets its own index */ 
    PRIMARY KEY (id, manufacturer_id), 
    INDEX idx_rugs_manufacturer_id (manufacturer_id) 
); 


CREATE TABLE carpets (
    id INTEGER NOT NULL AUTO_INCREMENT, 
    manufacturer_id INTEGER NOT NULL, 
    name VARCHAR(255), 
    warranty TEXT, 
    description TEXT, 
    fiber_name VARCHAR(255), 
    brand_name VARCHAR(255), 
    texture_name VARCHAR(255), 
    PRIMARY KEY (id, manufacturer_id), 
    /* manufacturer_id gets its own index */ 
    INDEX idx_manufacturer_id (manufacturer_id) 
); 
+1

固定小提琴:http://sqlfiddle.com/#!2/111c6 – biziclop

+0

@biziclop所以這在您爲manufacturer_id創建'KEY/INDEX'後,模式似乎會正確構建。 –

+0

是的,我添加了其中的一些,並且註釋了一些對省略的「目錄」表的引用。 – biziclop