2013-06-13 61 views
-3

這裏表SEARCHAUTHOR refrying CATALOGUEBASE但代碼產生以下錯誤:創建外鍵獲取錯誤(錯誤CIDE:1005)

Error Code: 1005. Can't create table 'easylibdb1.SEARCHAUTHOR' (errno: 150)

表結構的CATALOGUEBASE

CREATE TABLE IF NOT EXISTS CATALOGUEBASE(

     ID INT UNSIGNED NOT NULL AUTO_INCREMENT, 
     TITLE VARCHAR(250), 
     SUBTITLE VARCHAR(250), 
     AUTHOR VARCHAR(100), 
     EDITION VARCHAR(500) DEFAULT '', 
     TITLENUMBER VARCHAR(500), 
     PRIMARY KEY (ID) 
    ); 

SEARCHAUTHOR

CREATE TABLE IF NOT EXISTS SEARCHAUTHOR(

     ID INT UNSIGNED NOT NULL AUTO_INCREMENT, 
     TITLENUMBER VARCHAR(500), 
     AUTHOR VARCHAR(250) DEFAULT '', 
     SEQUENCE INT UNSIGNED, 
     PRIMARY KEY (ID), 
     FOREIGN KEY (TITLENUMBER) REFERENCES CATALOGUEBASE (TITLENUMBER)   
    ); 

提前致謝。

+0

請不要SHOUT。 – GolezTrol

+0

我刪除了大喊。 –

+0

@ L-Three:出錯? – Roopendra

回答

0

外鍵必須是mysql中的索引。

InnoDB需要對外鍵和引用鍵進行索引,以便外鍵檢查可以快速且不需要表掃描。閱讀更多here

CREATE TABLE IF NOT EXISTS CATALOGUEBASE(

    ID INT UNSIGNED NOT NULL AUTO_INCREMENT, 
    TITLE VARCHAR(250), 
    SUBTITLE VARCHAR(250), 
    AUTHOR VARCHAR(100), 
    EDITION VARCHAR(500) DEFAULT '', 
    TITLENUMBER varchar(200), 
    PRIMARY KEY (ID), 
    INDEX(TITLENUMBER) 
); 

CREATE TABLE IF NOT EXISTS SEARCHAUTHOR(

    ID INT UNSIGNED NOT NULL AUTO_INCREMENT, 
    TITLENUMBER varchar(200), 
    AUTHOR VARCHAR(250) DEFAULT '', 
    SEQUENCE INT UNSIGNED, 
    PRIMARY KEY (ID), 
    FOREIGN KEY (TITLENUMBER) REFERENCES CATALOGUEBASE(TITLENUMBER)   
); 

此查詢正在工作。 DEMO