如何在没有 DROP 数据库权限的情况下从命令行删除所有 MySQL 表?

2021-11-20 00:00:00 command-line mysql sql-drop

如何使用命令提示符删除 Windows MySQL 中的所有表?我想这样做的原因是我们的用户可以访问数据库删除,但无权重新创建数据库本身,因此我们必须手动删除表.有没有办法一次删除所有表?请记住,大多数表都与外键相关联,因此必须按特定顺序删除它们.

How do I drop all tables in Windows MySQL, using command prompt? The reason I want to do this is that our user has access to the database drops, but no access to re-creating the database itself, for this reason we must drop the tables manually. Is there a way to drop all the tables at once? Bear in mind that most of the tables are linked with foreign keys so they would have to be dropped in a specific order.

推荐答案

你可以像这样生成语句: DROP TABLE t1, t2, t3, ... 然后使用准备好的语句来执行它:

You can generate statement like this: DROP TABLE t1, t2, t3, ... and then use prepared statements to execute it:

SET FOREIGN_KEY_CHECKS = 0; 
SET @tables = NULL;
SELECT GROUP_CONCAT('`', table_schema, '`.`', table_name, '`') INTO @tables
  FROM information_schema.tables 
  WHERE table_schema = 'database_name'; -- specify DB name here.

SET @tables = CONCAT('DROP TABLE ', @tables);
PREPARE stmt FROM @tables;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET FOREIGN_KEY_CHECKS = 1; 

相关文章