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`);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM