Press "Enter" to skip to content

mysql下批量清空某个库下的所有表(库不要删除,保留空库)

正确的批量删除某个库下的所有表的方法只需如下两步:
1)第一步(只需将下面的"库名"替换成实际操作中的库名即可)

select concat('drop table ',table_name,';') from information_schema.TABLES where table_schema='库名';

2)第二步
切换到这个库下,把第一步的执行结果导出,然后全部执行

例如:
批量删除kevin库下的所有表

mysql> select concat('drop table ',table_name,';') from information_schema.TABLES where table_schema='kevin';
+--------------------------------------+
| concat('drop table ',table_name,';') |
+--------------------------------------+
| drop table haha;                     |  #只需要复制这里的drop语句,放在一起批量粘贴执行即可!(这里kevin库下就2张表,如果是N张表,就执行复制->粘贴执行)
| drop table heihei;                   |
+--------------------------------------+
2 rows in set (0.00 sec)


mysql> use kevin;   #切换到kevin库下,然后执行将上面复制的drop语句,直接粘贴执行即可!
Database changed
mysql> drop table haha;
Query OK, 0 rows affected (0.09 sec)

mysql> drop table heihei;
Query OK, 0 rows affected (0.08 sec)

注:本文转自https://www.cnblogs.com/kevingrace/p/9439025.html

Be First to Comment

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注