您好,欢迎访问一九零五行业门户网

mysql批量剔除指定前缀的表,批量修改表名的SQL语句

mysql批量删除指定前缀的表,批量修改表名的sql语句 select concat( 'drop table ', table_name, ';' ) from information_schema.tables where table_name like 'uc_%'; 注意: like ‘uc_%’ 其中 uc_是你需要替换的表前缀. 执行查询,会自动生成出 drop table
mysql批量删除指定前缀的表,批量修改表名的sql语句
select concat( 'drop table ', table_name, ';' ) from information_schema.tables where table_name like 'uc_%'; 
注意: like ‘uc_%’ 其中 uc_是你需要替换的表前缀.
执行查询,会自动生成出 drop table table_name这样的sql语句.
然后复制 drop语句 可以执行删除的操作了.
这么一来也可以安全的审核一下语句,避免误操作..
顺便补充一下一个批量修改表名的操作方法
select concat( 'alter table ', table_name, 'rename to ', table_name,';' ) 
from information_schema.tables where table_name like 'uc_%'; 
下面这种代码是今天遇到的,表头前面是 db,但是没有下横线显得很乱,于是批量将”dbtable_name”改成”db_table_name”
主要用的函数是mysql的substring函数
mysql教程 substring 字符截取函数
substring(str,pos)语法
substring(filed,m):截取filed字段从第m个字符开始到结束的字符串;
substring(filed,m,n):截取filed字段从第m个字符开始的长度为n的字符串;
str,字符
pos,从第几个开始取
select concat( 'alter table ', table_name, 'rename to db_', substring(table_name,3),';' ) 
from information_schema.tables where table_name like 'db%'; 
会得到结果
alter table uc_aaa rename to uc_aaa; 
alter table uc_bbb rename to uc_bbb;
其它类似信息

推荐信息