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

来源:互联网 发布:红木进口数据 编辑:程序博客网 时间:2024/04/28 22:49

执行以下语句时
alter table t_price add constraint FK_PRICE_REF_APP foreign key (app_id)       references t_app (ID) on delete cascade on update restrict
报告:
ERROR 1005 (HY000): Can't create table 'iread.#sql-f0b_6629b' (errno: 150)

解决办法:
1) 检查表的存储引擎是否是 InnoDB,如果不是的话,将其改为InnoDB. 在MyIsam模式下,无法创建外键。

修改my.cnf,在[mysqld]下加上

default-storage-engine=InnoDB

修改已经建成表的引擎:

alter table tablename type=InnoDB


2) 先禁用外键检查,然后创建外键,最后再打开 
set foreign_key_checks = 0;
alter table....foreign key...;
set foreign_key_checks = 1;

 一切OK

原创粉丝点击