問題描述
我有兩個表,table1
是帶有 ID
列的父表和帶有 IDFromTable1
列的 table2
>(不是實際名稱)當(dāng)我在 IDFromTable1
到 ID
上添加 FK 到 table1
時,我收到錯誤 Foreign key constraint is錯誤形成的錯誤
.如果 table1
記錄被刪除,我想刪除表 2 記錄.感謝您的幫助
I have two tables, table1
is the parent table with a column ID
and table2
with a column IDFromTable1
(not the actual name) when I put a FK on IDFromTable1
to ID
in table1
I get the error Foreign key constraint is incorrectly formed error
. I would like to delete table 2 record if table1
record gets deleted. Thanks for any help
ALTER TABLE `table2`
ADD CONSTRAINT `FK1`
FOREIGN KEY (`IDFromTable1`) REFERENCES `table1` (`ID`)
ON UPDATE CASCADE
ON DELETE CASCADE;
如果需要任何其他信息,請告訴我.我是 mysql 的新手
Let me know if any other information is needed. I am new to mysql
推薦答案
對于遇到此問題的任何人,只需運行顯示引擎 INNODB 狀態(tài)
并查看最新外鍵錯誤部分了解詳情.
For anyone facing this problem, just run
SHOW ENGINE INNODB STATUS
and see the LATEST FOREIGN KEY ERROR section for details.
這篇關(guān)于mysql 外鍵約束格式錯誤的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!