您好,登錄后才能下訂單哦!
新增外鍵時報錯:
錯誤代碼: 1215 Cannot add foreign key constraint
表key_list:
CREATE TABLE key_list
(server_app_id
INT(11) NOT NULL , server_name
CHAR(13) NOT NULL ,
PRIMARY KEY (server_app_id
)
) ENGINE=INNODB DEFAULT CHARSET=utf8
表for_list:
CREATE TABLE for_list
(for_id
INT(11) NOT NULL,for_name
CHAR(13) NOT NULL,
PRIMARY KEY (for_id
)
) ENGINE=INNODB DEFAULT CHARSET=utf8
在表key_list中新增外鍵server_app_id_ibfk:
ALTER TABLE key_list
ADD CONSTRAINT server_app_id_ibfk
FOREIGN KEY (server_app_id
) REFERENCES for_list
(for_id
);
執行正常
在表key_list中新增外鍵server_name_id_ibfk:
ALTER TABLE key_list
ADD CONSTRAINT server_name_id_ibfk
FOREIGN KEY (server_name
) REFERENCES for_list
(for_name
);
執行報錯。錯誤代碼: 1215 Cannot add foreign key constraint
原因:
在新增外鍵時,關聯表(for_list)中的相關字段(for_name)必須為index
解決:
for_list表for_name字段新增索引
ALTER TABLE for_list ADD INDEX for_index_name(for_name);
再次執行新增外鍵操作:
ALTER TABLE key_list
ADD CONSTRAINT server_name_id_ibfk
FOREIGN KEY (server_name
) REFERENCES for_list
(for_name
);
發現操作成功:
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。