Mysql Rename Multiple tables with a pattern

前端 未结 4 1700
孤街浪徒
孤街浪徒 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

提交回复
热议问题