mysql外键无法删除数据的情况

最近碰到一次在msyql主从删除数据,居然报错

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


先删除子表的数据,然后再删除主表的数据。

你可能感兴趣的:(mysql,外键无法删除数据)