网络营销电子商务研究中心  
How to buy the best prescription safety glasses in Canada? Let's study!
Go Back   网络营销电子商务研究中心 > 服务器与数据库 > MySQL
User Name
Password
 
FAQ Members List Calendar Cheap Glasses

Reply
 
Thread Tools Display Modes
  #1   IP: 180.125.107.111
Old 2014-07-04, 08:49 AM
Aledo Aledo is offline
初级会员
 
Join Date: 2006-02-07
Posts: 3
Aledo 现在声名狼藉
Default mysql批量删除指定前缀的表,批量修改表名的SQL语句

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;
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Prescription-glasses.com offers prescription glasses online at discount prices.
All times are GMT +8. The time now is 02:04 AM.


Powered by vBulletin Version 3.8.7
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.