必承其重 | 欲带皇冠 2008-09-15 21:22 采纳率: 0%
浏览 493
已采纳

如何快速重命名 MySQL 数据库(更改模式名称) ?

The MySQL manual at MySQL covers this.

Usually I just dump the database and reimport it with a new name. This is not an option for very big databases. Apparently RENAME {DATABASE | SCHEMA} db_name TO new_db_name; does bad things, exist only in a handful of versions, and is a bad idea overall.

This needs to work with InnoDB, which stores things very differently than MyISAM.

转载于:https://stackoverflow.com/questions/67093/how-do-i-quickly-rename-a-mysql-database-change-schema-name

  • 写回答

21条回答 默认 最新

  • csdnceshi62 2013-02-07 07:02
    关注

    For InnoDB, the following seems to work: create the new empty database, then rename each table in turn into the new database:

    RENAME TABLE old_db.table TO new_db.table;
    

    You will need to adjust the permissions after that.

    For scripting in a shell, you can use either of the following:

    mysql -u username -ppassword old_db -sNe 'show tables' | while read table; \ 
        do mysql -u username -ppassword -sNe "rename table old_db.$table to new_db.$table"; done
    

    Or

    for table in `mysql -u root -ppassword -s -N -e "use old_db;show tables from old_db;"`; do mysql -u root -ppassword -s -N -e "use old_db;rename table old_db.$table to new_db.$table;"; done;
    

    Notes: there is no space between the option -p and the password. If your database has no password, remove the -u username -ppassword part.

    Also, if you have stored procedures, you can copy them afterwards:

    mysqldump -R old_db | mysql new_db
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(20条)

报告相同问题?

悬赏问题

  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计
  • ¥23 (标签-bug|关键词-密码错误加密)
  • ¥66 比特币地址如何生成taproot地址
  • ¥20 数学建模数学建模需要
  • ¥15 关于#lua#的问题,请各位专家解答!
  • ¥15 什么设备可以研究OFDM的60GHz毫米波信道模型
  • ¥15 不知道是该怎么引用多个函数片段