154

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

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

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

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

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

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

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

想法?

4

7 回答 7

451

This might be useful to someone ending up here from a search. Make sure you're trying to drop a table and not a view.

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

SET foreign_key_checks = 0 is to set foreign key checks to off and then SET foreign_key_checks = 1 is to set foreign key checks back on. While the checks are off the tables can be dropped, the checks are then turned back on to keep the integrity of the table structure.

于 2010-05-20T13:13:21.863 回答
19

If you are using phpmyadmin then this feature is already there.

  • Select the tables you want to drop
  • 从表格列表底部的下拉列表中,选择 drop
  • 将打开一个新页面,底部有“外键检查”复选框,取消选中它。
  • 接受“是”确认删除。
于 2015-01-09T14:44:59.150 回答
4

You can use the following steps, its worked for me to drop table with constraint,solution already explained in the above comment, i just added screen shot for that -在此处输入图像描述

于 2014-07-02T15:46:42.100 回答
3

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

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

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

然后,登录mysql并

源转储.sql;

于 2010-02-20T00:55:21.617 回答
2

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

这涉及到您的 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> 
于 2019-03-05T17:29:04.980 回答
0

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 数据。

于 2021-04-11T08:10:45.353 回答
-47

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

于 2010-02-19T23:53:44.313 回答