mysql外鍵沒法刪除數據的狀況

最近碰到一次在msyql主從刪除數據,竟然報錯
java

delete from pt_game_server where id=2;
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`wonder_center`.`pt_server_area`, CONSTRAINT `pt_server_area_ibfk_1` FOREIGN KEY (`game_server_id`) REFERENCES `pt_game_server` (`id`))


有文章說多是mysql總從的緣由,應該刪除從表上的數據,再來刪除主表的數據。因而在從表上進行如下操做。發現仍是報一樣的錯誤mysql

mysql> delete from pt_game_server where id=2;
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`wonder_center`.`pt_server_area`, CONSTRAINT `pt_server_area_ibfk_1` FOREIGN KEY (`game_server_id`) REFERENCES `pt_game_server` (`id`))


繼續爬文。最後發現是外鍵形成的,原來子表還有劇,致使主表沒法刪除。sql

mysql> delete from  pt_server_area where  id=2;
Query OK, 1 row affected (0.00 sec)
mysql> delete from  pt_game_server where id=2;
Query OK, 1 row affected (0.00 sec)


先刪除子表的數據,而後再刪除主表的數據。ide

相關文章
相關標籤/搜索