首页 文章

MySQL ERROR 1005(HY000):无法创建表

提问于
浏览
1

+---------------+-------------+------+-----+---------+----------------+
| Field         | Type        | Null | Key | Default | Extra          |
+---------------+-------------+------+-----+---------+----------------+
| id            | int(11)     | NO   | PRI | NULL    | auto_increment |
| name          | varchar(64) | NO   |     | NULL    |                |
-----------------------------------------------------------------------

表2

ALTER TABLE Table2 
    ADD COLUMN person_id int(11), 
    ADD FOREIGN KEY fk_person_id(person_id) references Table(id);

这给了我一个错误,

ERROR 1005(HY000):无法创建表'table2 . #sql-3fb_7cf'(错误号:150)

表1 id 的主键是正确的 . 它还在哪里失败?

1 回答

  • 3

    在更改表并添加外键时,必须先将引用列添加为键,然后再将其添加为外键 .

    ALTER TABLE Table2 
        ADD COLUMN person_id int(11),
        ADD INDEX(person_id),
        ADD FOREIGN KEY fk_person_id(person_id) references Table(id);
    

相关问题