mysql创建外链失败1005错误解决方法

mysql创建外链失败1005错误解决方法

错误号:1005错误信息:Can't create table 'webDB.#sql-397_61df' (errno: 150)解决方法

错误原因有四:

1、外键的引用类型不一样,主键是int外键是char,或者大小不一样也不行,int(11位) bigint(20位)
2、找不到主表中 引用的列
3、主键和外键的字符编码不一致
4、要先建立索引,没有建立索引也会出错。

本人错误原因是1,将外键所在的表的字段为int改成跟主表一致的bigint就不报这个错误了。

已执行的 SQL 语句:Alter table `webDB`.`adm_house_layout`
add constraint `FK_Relationship_911` foreign key (`house_type_id`) references `webDB`.`adm_recommend_house`(`r_id`)错误号:1005错误信息:Can't create table 'webDB.#sql-397_61df' (errno: 150)

错误号:1452错误信息 Cannot add or update a child row: a foreign key constraint fails (`webDB`.`#sql-397_61df`, CONSTRAINT `FK_Relationship_91` FOREIGN KEY (`house_type_id`) REFERENCES `adm_recommend_house` (`r_id`))
: a foreign key constraint fails (`webDB`.`#sql-397_61df`, CONSTRAINT `FK_Relationship_91` FOREIGN KEY (`house_type_id`) REFERENCES `adm_recommend_house` (`house_type_id`))

主表和依赖表的关系要弄清楚
alter table adm_house_layout add constraint FK_Reference_91 foreign key (house_type_id) references adm_recommend_house (house_type_id) on delete restrict on update restrict;
要反过来才行
Alter table `webDB`.`adm_recommend_house` add constraint `FK_Relationship_91` foreign key (`house_type_id`) references `webDB`.`adm_house_layout`(`house_type_id`);

原文地址:https://www.cnblogs.com/zdz8207/p/4886038.html