Mysql Rename Multiple tables with a pattern

前端 未结 4 1633
孤街浪徒
孤街浪徒 2021-01-28 15:00

I know how to run

RENAME TABLE onetable_test TO onetable;

But is there a method to rename many tables with a pattern and don\'t write a lot of

相关标签:
4条回答
  • 2021-01-28 15:08

    For example:

    select group_concat(v.name SEPARATOR ' ') 
    from (
          select concat('rename table ', t.table_name, ' to ', substring(reverse(v.name), instr(reverse(v.name), '_') + 1, length(v.name)) name 
          from information_schema.tables t
          where 
              table_schema = 'put_your_table_schema'
              and table_name like '%_test'
         ) v;
    
    1. Running this query will return script that should be executed to rename tables.
    2. substring(reverse.....) rather than simple replace(v.name, '_test', '') used because we need to be sure that we will replace only _test occurrences only at the end of the string.

    Hope it helps

    0 讨论(0)
  • 2021-01-28 15:22

    I wanted to update what worked for me since this post is pretty old. I needed to add the ` mark before and after the table names. And don't forget to add the last ; because this can sometimes throw a syntax error.

    SET group_concat_max_len=5000;
    SELECT group_concat(v.name separator '; ')
    FROM (
        SELECT concat('RENAME TABLE `', t.table_name, '` TO `', replace(t.table_name, 'wp_', 'wplocal_'), '`') name
        FROM information_schema.tables t
        WHERE table_name like 'wp_%'
    ) v;
    
    0 讨论(0)
  • 2021-01-28 15:23

    Use below stored procedure ::

    DELIMITER $$
    

    CREATE

    PROCEDURE `Rename_Tables`()
    
    BEGIN
    DECLARE done INT DEFAULT FALSE;
    DECLARE tableName VARCHAR(50);
    DECLARE newTableName VARCHAR(70);
    DECLARE t_query VARCHAR(500);
    DECLARE cur1 CURSOR FOR SELECT table_name FROM information_schema.tables WHERE table_name LIKE '%table_test' AND table_schema='test' ;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
    
    OPEN cur1;
    
    read_loop: LOOP
        FETCH cur1 INTO tableName;
        IF done THEN
            LEAVE read_loop;
        END IF;
        SET newTableName = SUBSTRING(tableName, 1,LOCATE('_',tableName)-1);
        SET t_query = CONCAT('RENAME TABLE ', tableName, ' TO ', newTableName);
    
        SET @myQuery = t_query;
        PREPARE stmt FROM @myQuery;
        EXECUTE stmt;
        DEALLOCATE PREPARE stmt;
    END LOOP;
    
    END$$
    

    DELIMITER ;

    Replace table_schema with your table_schema name.

    0 讨论(0)
  • 2021-01-28 15:30

    A slight modification on @ravnur's answer:

    SET group_concat_max_len=5000;
    SELECT group_concat(v.name separator '; ')
    FROM (
        SELECT concat('RENAME TABLE ', t.table_name, ' TO ', replace(t.table_name, 'wp_', 'blog_')) name
        FROM information_schema.tables t
        WHERE table_name like 'wp_%'
    ) v;
    

    Useful when you want to rename your Wordpress tables from the default prefix of wp_ to blog_ (for example). Copy and paste the output of that command into the mysql shell or phpMyAdmin.

    0 讨论(0)
提交回复
热议问题