繁体   English   中英

强制删除mysql绕过外键约束

[英]Force drop mysql bypassing foreign key constraint

我试图从数据库中删除除一个表之外的所有表,但最终出现以下错误:

无法删除或更新父行:外键约束失败

当然,我可以反复试验以查看这些关键约束是什么,并最终删除所有表,但我想知道是否有一种快速方法可以强制删除所有表(因为我将能够重新插入那些我不想删除)。

谷歌将我瞄准了一些建议使用以下方法的网站:

mysql> SET foreign_key_checks = 0;
mysql> drop table ...
mysql> SET foreign_key_checks = 1;

简短的回答是它并没有真正起作用,因为我最终收到了同样的错误,而我能够删除更多的表。 我已经在 Stack Overflow 上看到了将所有外键链接到某个表的方法,但这太耗时了,除非我编写所有脚本(在没有其他选择的情况下这是可行的)

数据库是 4.1 所以我不能使用DROP DATABASE

想法?

这对于从搜索结果到这里的人可能很有用。 确保您尝试删除table而不是view

SET foreign_key_checks = 0;
-- Drop tables
drop table ...
-- Drop views
drop view ...
SET foreign_key_checks = 1;

SET foreign_key_checks = 0是将外键检查设置为关闭,然后SET foreign_key_checks = 1是将外键检查设置为重新打开。 当检查关闭时,可以删除表,然后重新打开检查以保持表结构的完整性。

如果您使用的是phpmyadmin,那么此功能已经存在。

  • 选择要删除的表
  • 从表列表底部的下拉列表中,选择 drop
  • 将打开一个新页面,底部有一个复选框,上面写着“外键检查”,取消选中它。
  • 通过接受“是”确认删除。

您可以使用以下步骤,它对我有用以删除带有约束的表,解决方案已在上面的评论中进行了解释,我只是为此添加了屏幕截图 -在此处输入图片说明

删除数据库存在于所有版本的 MySQL 中。 但是如果你想保持表结构,这里有一个想法

mysqldump --no-data --add-drop-database --add-drop-table -hHOSTNAME -uUSERNAME -p > dump.sql

这是一个程序,而不是一个mysql命令

然后,登录mysql并

源转储.sql;

从终端一次删除所有表的简单解决方案。

这涉及您的 mysql shell 中的几个步骤(虽然不是一步解决方案),这对我有用并挽救了我的一天。

适用于服务器版本:5.6.38 MySQL Community Server (GPL)

我遵循的步骤:

 1. generate drop query using concat and group_concat.
 2. use database
 3. turn off / disable foreign key constraint check (SET FOREIGN_KEY_CHECKS = 0;), 
 4. copy the query generated from step 1
 5. re enable foreign key constraint check (SET FOREIGN_KEY_CHECKS = 1;)
 6. run show table

MySQL外壳

$ mysql -u root -p
Enter password: ****** (your mysql root password)
mysql> SYSTEM CLEAR;
mysql> SELECT CONCAT('DROP TABLE IF EXISTS `', GROUP_CONCAT(table_name SEPARATOR '`, `'), '`;') AS dropquery FROM information_schema.tables WHERE table_schema = 'emall_duplicate';
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| dropquery                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| DROP TABLE IF EXISTS `admin`, `app`, `app_meta_settings`, `commission`, `commission_history`, `coupon`, `email_templates`, `infopages`, `invoice`, `m_pc_xref`, `member`, `merchant`, `message_templates`, `mnotification`, `mshipping_address`, `notification`, `order`, `orderdetail`, `pattributes`, `pbrand`, `pcategory`, `permissions`, `pfeatures`, `pimage`, `preport`, `product`, `product_review`, `pspecification`, `ptechnical_specification`, `pwishlist`, `role_perms`, `roles`, `settings`, `test`, `testanother`, `user_perms`, `user_roles`, `users`, `wishlist`; |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> USE emall_duplicate;
Database changed
mysql> SET FOREIGN_KEY_CHECKS = 0;                                                                                                                                                   Query OK, 0 rows affected (0.00 sec)

// copy and paste generated query from step 1
mysql> DROP TABLE IF EXISTS `admin`, `app`, `app_meta_settings`, `commission`, `commission_history`, `coupon`, `email_templates`, `infopages`, `invoice`, `m_pc_xref`, `member`, `merchant`, `message_templates`, `mnotification`, `mshipping_address`, `notification`, `order`, `orderdetail`, `pattributes`, `pbrand`, `pcategory`, `permissions`, `pfeatures`, `pimage`, `preport`, `product`, `product_review`, `pspecification`, `ptechnical_specification`, `pwishlist`, `role_perms`, `roles`, `settings`, `test`, `testanother`, `user_perms`, `user_roles`, `users`, `wishlist`;
Query OK, 0 rows affected (0.18 sec)

mysql> SET FOREIGN_KEY_CHECKS = 1;
Query OK, 0 rows affected (0.00 sec)

mysql> SHOW tables;
Empty set (0.01 sec)

mysql> 

Table1 {T_Id, T_Name, TT_Id(Nullable)(Table2上TT_ID列的外键)}

表 2 {TT_ID,TT_Title}

1- make the foreign Key relation null able on the table1
2- update table1 set TT_ID = null where T_ID = ?
3- delete from table1

现在您可以删除 table1 数据并保留 table2 数据。

由于您对保留任何数据不感兴趣,因此删除整个数据库并创建一个新数据库

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM