删除MySQL中名字首尾固定关键字相同的表

删除MySQL中名字首尾固定关键字相同的表

SELECT CONCAT('drop table ', group_concat(TABLE_NAME), ';') 
 FROM information_schema.`TABLES` 
 WHERE table_schema = 'test' AND TABLE_NAME LIKE 't_%_history' ;

查看表列表

SHOW TABLES;

show tables

通过上图观察发现所有的表都是以 t_ 开头 和以 _history 结尾的同名表
本次目标 一条 SQL 语句删除所有 t_开头 _history 结尾的所有表

思路:使用 drop table 表名1,表名2 语句删除不需要的表,
1、MySQL的information_schema是一个数据库,它存储了关于MySQL数据库的元数据信息,包括数据库、表、列、索引、用户权限等信息。
通过查询information_schema,可以获取数据库的结构和属性信息,以便进行数据库管理和查询优化等操作。

2、查询该库下所有的表然后将表名称使用逗号拼接

3、添加条件筛选是需要的表

4、使用 select 获取 拼接的 drop 语句

5、执行 drop 语句

SELECT GROUP_CONCAT(TABLE_NAME) FROM information_schema.`TABLES` ;

在这里插入图片描述

SELECT GROUP_CONCAT(TABLE_NAME) FROM information_schema.`TABLES` 
 WHERE table_schema = 'test' AND TABLE_NAME LIKE 't_%_history' ;

在这里插入图片描述

SELECT CONCAT('drop table ', group_concat(TABLE_NAME), ';') 
 FROM information_schema.`TABLES` 
 WHERE table_schema = 'test' AND TABLE_NAME LIKE 't_%_history' ;

在这里插入图片描述

drop table t_backstock_history,t_brand_history,t_category_brand_history,t_category_history,t_city_history,t_customer_address_history,t_customer_history,t_delivery_history,t_dept_history,t_emp_history,t_job_history,t_level_history,t_order_detail_history,t_order_history,t_productin_history,t_productin_purchase_history,t_province_history,t_purchase_history,t_rating_history,t_role_history,t_shop_history,t_shop_sku_history,t_sku_history,t_spec_group_history,t_spec_param_history,t_spu_history,t_supplier_history,t_supplier_sku_history,t_user_history,t_voucher_customer_history,t_voucher_history,t_warehouse_history,t_warehouse_sku_history;

在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_24330181/article/details/131460500