繁体   English   中英

MySQL中具有外键匹配数据类型的Errno 150

[英]Errno 150 in MySQL with Foreign Key Matching Datatype

我正在执行以下SQL查询:

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;

外键musician_id所引用的musician字段上的id如下所示:

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

因此,似乎我正在为musicer_id使用匹配的数据类型。 但是我收到以下错误:

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

指示外键有问题。 那是怎么回事?

查看有关外键约束的MySQL手册:

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.确保在您的MySQL服务器上启用了InnoDB。 要验证这一点,请尝试以下命令:

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

4.检查命令中表名和字段名中的大小写。

5.不仅要检查要创建的表,还要检查外键引用的表。

6.确保您引用的表已正确索引。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM