【问题标题】:mysql, how to drop tables that the table name starts with digitsmysql,如何删除表名以数字开头的表
【发布时间】:2015-12-16 23:38:41
【问题描述】:

我的数据库里有1200张表,其中一半的表名都是数字开头的,就是这个样子

mysql> SHOW TABLES;

    1000_quarterly_1000
    1001_quarterly_1001
    ...
    quarterly_1000
    quarterly_1001
    ....

我想删除所有名称以数字开头的表,例如必须删除表 1000_quarterly_1000。由于表已从当前数据库中删除,我想将它们存储到我定义的其他数据库中。

我如何写mysql query 来做到这一点,因为我可以使用Python 来做到这一点,但mysql query 可以更好。

【问题讨论】:

标签: mysql


【解决方案1】:

如果要在一个语句中删除表,可以将语句构造为:

select concat('drop table ', group_concat(t.table_schema, '.', t.table_name separator ', '))
from information_schema.tables
where left(t.table_name, 1) between '0' and '9';

然后您可以尝试执行命令:

declare @sql varchar(65000);

select @sql := concat('drop table ', group_concat(t.table_schema, '.', t.table_name separator ', '))
from information_schema.tables
where left(t.table_name, 1) between '0' and '9';

prepare s from @sql;

execute s;

【讨论】:

    【解决方案2】:
    select concat('drop table ',t.table_schema,'.',t.table_name,';') drop_ddl 
    from information_schema.tables t where t.table_name rlike '[0-9].'  
    

    【讨论】:

      猜你喜欢
      • 2014-07-25
      • 2010-09-05
      • 2022-08-17
      • 1970-01-01
      • 1970-01-01
      • 2019-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多