1511月/116
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;
批量复制一下到记事本或者 et之类的编辑工具中,然后批量替换 RENAME TO uc 成 RENAME TO 你想要的表前缀
完成后 再执行.
邵珠庆推荐文章
博文加载中...
2012年01月15日 23:43
Hi! I was surfing arround on Yahoo and came across this website. I like the articles! I also have a blog . Maybe you will visit my website too.
2012年03月04日 04:47
I’m grateful you made the post. It’s claeerd the air for me.
2012年04月06日 09:39
So excited I found this atircle as it made things much quicker!
2012年04月09日 12:41
We need more insights like this in this trehad.
2012年11月11日 16:59
Stands back from the keybarod in amazement! Thanks!
2012年03月06日 12:22
You’re the geraetst! JMHO