MySQL中使用DDL命令:rename table快速修改库名(Schema)

需求背景

在某些情况下,我们想修改一个数据库的库名(schema),但没有现有的语句,如果新建一个schema,再将数据导入,数据量大的情况下,些操作会很慢,这时候,我们就可以通过rename table命令,来达到曲线救国的目的了

实验数据

  • 现有一个数据库test,test中的N多个表
  • 需要将test库改名为test2

解决方案

新建数据库test2

create database test2;

通过元数据库:information_schema拼接需要的DDL
select 
    concat('rename table test.', table_name, ' to test2.', table_name, ';') ddl 
from information_schema.tables 
where table_schema='test'; 
复制生成的DDL语句,然后执行,可将原test下的表转移到test2下
最后删除旧库test

drop database test;

评论

暂无评论