• support@answerspoint.com

How do I quickly rename a mysql database (change schema name)?

2203

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.

2Answer


0

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 do this:

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

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
  • answered 8 years ago
  • Sunny Solu

0

I think the solution is simpler and was suggested by some developers. phpMyAdmin has an operation for this.

From phpMyAdmin, select the database you want to select. In the tabs there's one called Operations, go to the rename section. That's all.

It does, as many suggested, create a new database with the new name, dump all tables of the old database into the new database and drop the old database.

Enter image description here

  • answered 8 years ago
  • Sandy Hook

Your Answer

    Facebook Share        
       
  • asked 9 years ago
  • viewed 2203 times
  • active 9 years ago

Best Rated Questions