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.
I did it this way: Take backup of your existing database. It will give you a db.zip.tmp and then in command prompt write following
You can use SQL to generate an SQL script to transfer each table in your source database to the destination database.
You must create the destination database before running the script generated from the command.
You can use either of these two scripts (I originally suggested the former and someone "improved" my answer to use
GROUP_CONCAT
. Take your pick, but I prefer the original):or
($1 and $2 are source and target respectively)
This will generate a SQL command that you'll have to then run.
Note that
GROUP_CONCAT
has a default length limit that may be exceeded for databases with a large number of tables. You can alter that limit by runningSET SESSION group_concat_max_len = 100000000;
(or some other large number).You may use this shell script:
Reference: How to rename a MySQL database?
It's working:
It is possible to rename all tables within a database to be under another database without having to do a full dump and restore.
However any triggers in the target db will not be happy. You'll need to drop them first then recreate them after the rename.
I've only recently came across a very nice way to do it, works with MyISAM and InnoDB and is very fast:
I don't remember where I read it but credit goes to someone else not me.
Emulating the missing
RENAME DATABASE
command in MySQL:Create the rename queries with:
Run that output
It was taken from Emulating The Missing RENAME DATABASE Command in MySQL.