无法创建FOREIGN KEY MySQL错误150

时间:2012-10-05 17:25:37

标签: mysql

我在表votes上添加外键时遇到了问题。

mysql> describe votes;
+----------+------------------+------+-----+---------+----------------+
| Field    | Type             | Null | Key | Default | Extra          |
+----------+------------------+------+-----+---------+----------------+
| id       | int(11) unsigned | NO   | PRI | NULL    | auto_increment |
| user_id  | int(11) unsigned | NO   |     | NULL    |                |
| video_id | int(11) unsigned | NO   |     | NULL    |                |
| vote     | int(11)          | NO   |     | NULL    |                |
+----------+------------------+------+-----+---------+----------------+

mysql> describe user;
+----------+------------------+------+-----+---------+----------------+
| Field    | Type             | Null | Key | Default | Extra          |
+----------+------------------+------+-----+---------+----------------+
| id       | int(11) unsigned | NO   | PRI | NULL    | auto_increment |
| email    | varchar(256)     | NO   | MUL | NULL    |                |
| password | varchar(32)      | NO   |     | NULL    |                |
| name     | varchar(24)      | NO   |     | NULL    |                |
+----------+------------------+------+-----+---------+----------------+

Votes.user_id是User.id的外键。当我跑步时,Bot有相同的时间:

mysql> alter table `votes` 
       add CONSTRAINT `votes_FK_1` 
       FOREIGN KEY (`user_id`) REFERENCES `user` (`id`);

MySQL正在抛出一个好老150

ERROR 1005 (HY000): Can't create table 'crocko.#sql-6e1_3c5' (errno: 150)

我做错了什么?

1 个答案:

答案 0 :(得分:1)

您需要检查表中是否存储了任何数据..如果是,则需要将其删除..

或者,如果您的MySQL数据库引擎是 MyISAM ,则在创建表后您将无法添加外键。

您可以查看如何将其更改为 InnoDB ..