2014-03-03 52 views
0

部分字符串,我有表名称,如xyz_table1xyz_table2 ...替换表名+ mysql的

我想与某些特定的字符串如abc更换xyz

所以表名会像abc_table1abc_table2 ...

如果我想表名与RENAME TABLE重命名,那么我将不得不写RENAME TABLE脚本所有表。如果有超过300个表格,那么这是太多的手工工作,即我将不得不为所有表格编写RENAME TABLE脚本。
那么还有其他方法可以替换表名中的部分字符串吗?

+0

尝试使用存储过程,http://stackoverflow.com/questions/11642020/mysql-rename-multiple-tables-with-a-pattern – Shin

+0

@shin ::感谢您的回复。我正在为此寻求单行的sql脚本。 –

回答

0

你可以在信息架构表运行CONCAT:

您可以检查它的输出

select concat("alter table " . TABLE_NAME . " to " .replace(TABLE_NAME , "xyz", "abc"). ";") from information_schema.TABLES where TABLE_SCHEMA="database_name" ; 

和之后,你可以运行它:

mysql --batch --skip-column-names --execute \ 
'select concat("alter table " . TABLE_NAME . " to " .replace(TABLE_NAME , "xyz", "abc"). ";") 
from information_schema.TABLES where TABLE_SCHEMA="database_name"' | mysql 
0

你可以做如下, `

1) show tables. 
2) copy all the table names. 
3) paste it in an excel sheet. 
4) apply formula for creating a rename query and drag it to apply for remaining 300 tables. 
5) copy the queries and run.` 

这将需要2-3分钟。而已。