2014-03-03 46 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分鐘。而已。