强制drop mysql绕过外键约束

时间:2010-02-19 23:49:28

标签: mysql foreign-keys sql-drop

我正在尝试删除数据库中的所有表,除了一个,我最终得到以下错误:

  

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

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

谷歌针对某些建议使用以下方法的网站:

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

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

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

想法?

7 个答案:

答案 0 :(得分:357)

这可能对从搜索到此处结束的人有用。 确保您尝试删除表格而不是视图

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将重新设置外键检查。关闭检查时,可以删除表,然后重新打开检查以保持表结构的完整性。

答案 1 :(得分:14)

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

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

答案 2 :(得分:5)

你可以使用以下步骤,它可以帮我删除带有约束的表格,解决方案已经在上面的评论中解释过,我刚刚添加了屏幕截图 - enter image description here

答案 3 :(得分:3)

在所有版本的MySQL中都存在Drop数据库。但是如果你想保留表结构,这是一个想法

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

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

然后,登录mysql和

source dump.sql;

答案 4 :(得分:0)

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

这涉及到您的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 shell

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

答案 5 :(得分: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数据了。

答案 6 :(得分:-20)

由于您不想保留任何数据,drop the entire database并创建一个新数据。