2014-03-27 87 views
1

我做下面的SQL查詢:errno的150在MySQL與外鍵匹配的數據類型

CREATE TABLE discography (
    id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, 
    musician_id int(11) NOT NULL, 
    title VARCHAR(255) NOT NULL, 
    album_cover_url varchar(255) DEFAULT NULL, 
    year INT(11), 
    FOREIGN KEY (musician_id) REFERENCES musician (id) ON DELETE CASCADE 
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 

的音樂家領域ID外鍵musician_id被引用如下所示:

id     | int(11)  | NO | PRI | NULL | auto_increment | 

因此,它似乎是我爲musician_id使用匹配的數據類型。然而,我收到以下錯誤:

ERROR 1005 (HY000): Can't create table 'discography' (errno: 150) 

指示外鍵的問題。那是怎麼回事?

+0

你可以試試這個'musician_id int(11)NOT NULL' –

回答

1

退房MySQL手冊約foreign key約束:

If you re-create a table that was dropped, it must have a definition that conforms to the foreign key constraints referencing it. It must have the right column names and types, and it must have indexes on the referenced keys, as stated earlier. If these are not satisfied, MySQL returns error number 1005 and refers to error 150 in the error message.

幾個想法:

1.更好的跌落表和創建新的格式良好。

2.確保將ENGINE=InnoDB;添加到您的CREATE TABLE - command

3.確保InnoDB在您的MySQL服務器上啓用。爲了驗證這一點, 試試這個命令:

SHOW `VARIABLES LIKE` 'have_innodb'; - if it returns 
a YES, then InnoDB is enabled. 

4.檢查您的命令大寫和小寫的表 - 和 字段名。

5.檢查這不僅是一個你想創建的表,而且 外鍵所指的表。

6.確保您的引用表正確索引。

+0

@ user1427661,看看我的答案... – jmail

相關問題