使用默认引擎 (MyIsam),我可以使用以下语法创建外键:
alter table `codes` add constraint foreign key(`associated_code_id`) references ask_codes(code_id) on update cascade on delete cascade;
但是,当我使用 INNODB 创建表时,我无法再创建外键(mysql 给我错误 1005 (HY000): Can't create table 'my_table.#sql-3311_16115' (errno: 150) ))。
这是语法问题吗?感谢您的帮助,
最佳答案
您的问题是两列没有相同的类型 - 它们不完全匹配:
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.
关于Mysql,innodb - 无法创建外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10108426/