在MySQL里創建外鍵時(Alter table xxx add constraint fk_xxx foreign key),提示錯誤,但只提示很簡單的信息:ERROR 1005 (HY000): Can't create table '.\env_mon\#sql-698_6.frm' (errno: 150)。根本起不到解決問題的作用。
要看錯誤的詳細提示,可以使用命令:(在MySQL Manual里搜索“errno 150”時找到)
SHOW ENGINE INNODB STATUS;???? //針對用INNODB存儲方式的數據庫
在信息中有一組【LATEST FOREIGN KEY ERROR】會有最近錯誤的詳細描述和解決辦法。
如:
Cannot find an index in the referenced table where the referenced columns appear as the first columns, or column types in the table and the referenced table do not match for constraint.
(譯:不能在“被reference的表”里找到包含“被reference字段”的索引,或者是兩個關聯字段類型不匹配)
(我犯的錯誤是沒有在主表中設置主鍵-_-!,即第一種原因)
版權所有 羅明