How to change all table prefix in a single query

∥☆過路亽.° 提交于 2019-12-10 23:19:03

问题


I am pretty amateur in mysql..can you please tell me how can I change table prefixes of my whole database in a single query... I can do it manually, but its quite time consuming to change all the tables prefixes. Please help me out. Like isc_administrator_log to cus_administrator_log means isc_ to cus_

I found these two solutions but do not understand either of them.

SELECT 
GROUP_CONCAT('RENAME TABLE `', TABLE_SCHEMA, '`.`', TABLE_NAME, '` TO `', 
TABLE_SCHEMA, '`.`prefix_', TABLE_NAME, '`;' SEPARATOR ' ')
FROM `TABLES` WHERE `TABLE_SCHEMA` = "test";

and

SELECT 
CONCAT('RENAME TABLE ', GROUP_CONCAT('`', TABLE_SCHEMA, '`.`', TABLE_NAME,
'` TO `', TABLE_SCHEMA, '`.`prefix_', TABLE_NAME, '`')) AS q
FROM 
`information_schema`.`Tables` WHERE TABLE_SCHEMA='test';

回答1:


You can do Dynamic SQL Query For MySQL 5.0.13


delimiter // 
CREATE PROCEDURE dynamic(IN tbl CHAR(64), IN col CHAR(64))
BEGIN
    SET @s = CONCAT('SELECT 'RENAME TABLE ', 
             GROUP_CONCAT('', TABLE_SCHEMA, ''.'', TABLE_NAME,
             ' TO ', TABLE_SCHEMA, ''='.prefix_''', TABLE_NAME, '')) AS q
             FROM 
             information_schema.Tables WHERE TABLE_SCHEMA='test'';;'
    PREPARE stmt FROM @s;
    EXECUTE stmt;
END
//
delimiter ;

Got the Reference from here




回答2:


Both statements generate strings which happen to be SQL statements that you can then copy/paste at your prompt to rename your tables.

Just replace the following two strings with the actual values your need:

prefix_ : the prefix you want to add

'test' : the name of the database containing the tables you want to rename

Both statements are almost identical, the only difference is that information_schema is not explicitely mentioned in the first statement. Therefore the first statement must be run from the information_schema database (issue USE information_schema beforehands).




回答3:


phpmyadmin : select database ; tab structure => Check all => (with selected list) select add prefix to table .

(is not query but it works)



来源:https://stackoverflow.com/questions/16339636/how-to-change-all-table-prefix-in-a-single-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!