【问题标题】:How to remove a prefix name from every table name in specific MySQL database如何从特定 MySQL 数据库中的每个表名中删除前缀名
【发布时间】:2021-06-23 22:43:07
【问题描述】:

特定MySQL数据库中的所有表,例如my_db,都以长度为17字符的前缀开头。我想从所有表的名称中删除前缀。我该怎么做?

【问题讨论】:

    标签: mysql database alter-table


    【解决方案1】:

    我在here 描述的语句中添加了where table_schema = 'my_db',以获取用于重命名my_db 数据库中所有表的语句,而不是重命名MySQL 服务器实例中所有数据库的所有表:

    select concat('RENAME TABLE ', concat(table_name, concat(' TO ', concat(substr(table_name, 18), ';')))) from information_schema.tables where table_schema = 'my_db';
    

    注意前缀的长度是17,我在substr命令中使用了18。

    【讨论】:

      【解决方案2】:

      改进了hasanghaforian的回答... 删除了额外的 concat 函数.. 仍然产生相同的输出

      select concat('RENAME TABLE ', table_name, ' TO ' , substr(table_name,18) , ' ; ' ) from information_schema.tables where table_schema = 'my_db';
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-19
        • 2023-03-13
        • 2017-06-12
        • 1970-01-01
        • 2020-06-10
        • 2018-04-08
        相关资源
        最近更新 更多