Home > Database > Mysql Tutorial > body text

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

WBOY
Release: 2016-06-01 13:28:29
Original
808 people have browsed it

bitsCN.com

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;

bitsCN.com
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!