总所周知,mysql下要想删除某个库下的某张表,只需要切换到该库下,执行语句"drop table tablename"即可删除!但若是该库下有成百上千张表,要是再这样一次次执行drop语句,就太费劲了!

正确的批量删除某个库下的所有表的方法只需如下两步:
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)

确实采用上面直接复制拼接语句查询出来结果的方法,在多表情况下比较复杂,却需要调整格式。优化方案是:将查询结果直接导出到文件,再直接source 执行文件。做法如下:

1)在mysql配置文件里添加数据导出导入权限。
[root@localhost ~]# vim /etc/my.cnf
.......
[mysqld]
secure_file_priv=/opt/mysql/data //表示打开mysql数据导出导入权限,且限制导出导入只能发生在/opt/mysql/data目录下 给指定的/opt/mysql/data目录授予sql用户权限
[root@localhost ~]# chown -R mysql.mysql /opt/mysql 重启mysql服务
[root@localhost ~]# /etc/init.d/mysqld restart
Stopping mysqld: [ OK ]
Starting mysqld: [ OK ] 将kevin库下的所有表导出到授权的/opt/mysql/data目录下
mysql> select concat('drop table ',table_name,';') from information_schema.TABLES where table_schema='kevin';
+--------------------------------------+
| concat('drop table ',table_name,';') |
+--------------------------------------+
| drop table anan; |
| drop table beibei; |
| drop table haha; |
| drop table hehe; |
| drop table heihei; |
| drop table huihui; |
| drop table huohuo; |
| drop table jiajia; |
| drop table jingjing; |
| drop table liuliu; |
| drop table popo; |
| drop table qiuqiu; |
| drop table renren; |
| drop table youyou; |
+--------------------------------------+
14 rows in set (0.00 sec) 现在开始批量删除kevin库下的所有表
[root@localhost ~]# cat /opt/mysql/data/table.txt
drop table anan;
drop table beibei;
drop table haha;
drop table hehe;
drop table heihei;
drop table huihui;
drop table huohuo;
drop table jiajia;
drop table jingjing;
drop table liuliu;
drop table popo;
drop table qiuqiu;
drop table renren;
drop table youyou; mysql> select concat('drop table ',table_name,';') from information_schema.TABLES where table_schema='kevin' into outfile '/opt/mysql/data/table.txt';
Query OK, 14 rows affected (0.00 sec) mysql> source /opt/mysql/data/table.txt;
Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) mysql> show tables;
Empty set (0.00 sec) mysql>

==================关于Mysql数据导出导入参数secure_file_priv说明===================
MySQL 5.7版本后引入了secure_file_priv参数,这个参数用来限制数据导入和导出操作的效果,比如用来限制LOAD DATA, SELECT ... OUTFILE, and LOAD_FILE()传到哪个指定目录的,这些操作需要用户具有FILE权限。 在mysql配置文件my.cnf的[mysqld]区域下配置:
1)如果这个参数设为空或"/",则MySQL服务允许将将数据导出到任意目录。
2)如果这个参数设为一个具体的目录名,则MySQL服务只允许在此目录中执行文件导入和导出操作。这个目录必须存在且设置为mysql用户权限,MySQL服务不会创建它;
3)如果这个参数为NULL,则MySQL服务会禁止导入和导出操作。
4)如果没有这个参数配置,则Mysql服务默认是没有打开这个功能,即不能进行数据导入导出操作。

配置示例:

1)允许mysql数据导入导出,且导出到任意目录(注意导出到的目录权限要是mysql.mysql)
[root@localhost ~]# vim /usr/local/my.cnf
[mysqld]
......
secure_file_priv= //或者配置成secure_file_priv="/" [root@localhost ~]# /etc/init.d/mysqld restart
Stopping mysqld: [ OK ]
Starting mysqld: [ OK ] 比如导出到/mnt/data目录下,则
[root@localhost ~]# mkdir /mnt/data
[root@localhost ~]# chown -R mysql.mysql /mnt/data/ 登录mysql查看secure-file-priv参数的值:
mysql> select version();
+------------+
| version() |
+------------+
| 5.6.39-log |
+------------+
1 row in set (0.02 sec) mysql> show global variables like '%secure%';
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| secure_auth | OFF |
| secure_file_priv | / |
+------------------+-------+
2 rows in set (0.00 sec) 导出到指定的/mnt/data/data.txt文件中,该文件不能是已存在状态,mysql数据导出的时候会自动创建该文件。
[root@localhost ~]# mysql -p123456
mysql> select concat('drop table ',table_name,';') from information_schema.TABLES where table_schema='kevin' into outfile '/mnt/data/table.txt'; 删除kevin库下的所有表
mysql> use kevin;
mysql> source /mnt/data/table.txt;
mysql> show tables; //查询kevin库,发现所有表已经被删除。 2)如果允许mysql数据导出导入,并限制导出到指定的/opt/kevin/data下,则配置:
[root@localhost ~]# vim /usr/local/my.cnf
[mysqld]
......
secure_file_priv= /opt/kevin/data [root@localhost ~]# mkdir -p /opt/kevin/data
[root@localhost ~]# chown -R mysql.mysql /opt/kevin/data 3)禁止mysql服务进行数据导出导入操作。
[root@localhost ~]# vim /usr/local/my.cnf
[mysqld]
......
secure_file_priv= NULL [root@localhost ~]# mkdir -p /opt/kevin/data
[root@localhost ~]# chown -R mysql.mysql /opt/kevin/data mysql> select concat('drop table ',table_name,';') from information_schema.TABLES where table_schema='kevin' into outfile '/opt/data/table.txt';
ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
mysql> 4)如果my.cnf里没有secure_file_priv这个参数配置,则默认是关闭这个功能,即不允许数据导入导出操作。
mysql> select concat('drop table ',table_name,';') from information_schema.TABLES where table_schema='kevin' into outfile '/opt/data/table.txt';
ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement

mysql下批量清空某个库下的所有表(库不要删除,保留空库)的更多相关文章

  1. mysql通过mysqldump工具,对某个库下的表进行备份

    需求描述: 使用mysqldump工具对某个库下的表进行备份的方法. 操作过程: 1.通过mysqldump工具完成此目的 [mysql@redhat6 MysqlDb_Backup]$ mysqld ...

  2. mysql5.7 mysql库下面的user表没有password字段无法修改密码

    如题所述,mysql5.7  mysql库下面的user表没有password字段无法修改密码, 5.7版本已经不再使用password来作为密码的字段了  而改成了authentication_st ...

  3. 将MySQL库的表转入到MSSQL中的某个库中(Employees下的Employees表 → pubs库下)_2

    将MySQL库的表转入到MSSQL中的某个库中(Employees下的Employees表 → pubs库下, 此pubs下的表名是employee,不冲突),方法大致以下几个(另有其他方法待补充), ...

  4. python--同一mysql数据库下批量迁移数据

    最近接手些mysql数据库维护,发现mysql在批量操作方面就是个渣渣啊,比起MS SQL SERVER简直就是“不可同日而语”. 咨询了下MySQL的高手,对于数据迁移这种问题,一种处理方式就是直接 ...

  5. 第十三章.MySQL数据库与JDBC编程(下)

    JDBC的典型用法: JDBC4.2常用接口和类简介: DriverManager:用于管理JDBC驱动的服务类,程序中使用该类的主要功能是获取Connection对象,该类包含如下方法: publi ...

  6. linux 下配置mysql区分大小写(不区分可能出现找不到表的情况)怎么样使用yum来安装mysql

    Linux 默认情况下,数据库是区分大小写的:因此,要将mysql设置成不区分大小写 在my.cof 设置 lower_case_table_names=1(1忽略大小写,0区分大小写) 检查方式:在 ...

  7. sqlserver查询当前库下,一张表的表名,字段名,字段类型,字段长度

    sqlserver版: 查询当前数据库下所有表名: select * from sys.tables; 查询当前库下,一张表的表名,字段名,字段类型,字段长度: select a.name 表名,b. ...

  8. Python —— 批量替换指定目录下的所有文件中指定字符串

    参考:http://blog.csdn.net/zcwfengbingdongguke/article/details/13951527 代码: #!/usr/bin/python import os ...

  9. (5.10)mysql高可用系列——percona-toolkit工具下的pt-table-checksum 在线验证主从一致性【续写中】

    关键词:percona-toolkit 工具包中包含 pt-table-checksum工具,在线验证主从一致性 [1]percona-toolkit 工具包 [1.1]percona-toolkit ...

随机推荐

  1. Java —— 对象

    创建对象 int[] b = new int[30]; 等号右侧:创建了一个数组对象  // 等号左侧:变量 b 称为该对应的引用  // 称作 变量 b 指向了一个对象  // 有时也简称为: b ...

  2. 高德地图JS API 开发小结

    项目中有一块功能要用到高德地图,所以,想把编码小结一下. 首先是地图的初始化 var map = new AMap.Map("mapDiv", {                  ...

  3. 在ASP.NET Core中使用多环境

    原文地址:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/environments?view=aspnetcore-2.1#star ...

  4. Head First Android --- Enable USB debugging on your device

    1. Enable USB debugging on your device    On your device, open “Developer options” (in Android 4.0 o ...

  5. matplotlib numpy scipy 的安装

    一:windows 端的安装 #cmd指令 python -m pip install --user numpy scipy matplotlib ipython jupyter pandas sym ...

  6. Leviticus

    The head is empty and empty. Just practicing English will not have any effect. The best effect is to ...

  7. Arcgis10.3在添加XY数据时出现问题

    准备通过excel表格(xls格式)中的经纬度生成点数据,但是选择数据的时候报错:连接到数据库失败,常规功能故障,外部表不是预期的格式.如下图所示: 解决方法: 将xls格式的表格另存为csv格式,重 ...

  8. 前端性能优化成神之路--图片懒加载(lazyload image)

    图片懒加载(当然不仅限于图片,还可以有视频,flash)也是一种优化前端性能的方式.使用懒加载可以想要看图片时才加载图片,而不是一次性加载所有的图片,从而在一定程度从减少服务端的请求 什么是懒加载 懒 ...

  9. ActivityThread main

    public static void main(String[] args) { Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "Ac ...

  10. SQL数据库中日期时间类型,按日期group by 实现

    每天学习一点点 编程PDF电子书免费下载: http://www.shitanlife.com/code cast(starttime as date):  时间转日期类型 实例SQL: SELECT ...