MAO-EYE 2008-09-15 21:22 采纳率: 100%
浏览 622
已采纳

如何快速重命名 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

  • 写回答

23条回答 默认 最新

  • 10.24 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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(22条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度