MySQL: Rename Name of a Database
Tip by Stefan Trost | 11/05/2012 at 13:46
In this tip, I want to show you how to change the name of a database quickly and easily.
Since the function RENAME DATABASE appeared only between the version 5.1.7 and 5.1.23 of MySQL and has been removed because of the risk of losing data, we have to make use of a simple trick to rename a database. In the example, we want to rename the database dbold to dbnew.
1. Create an empty database
First, we create an empty database with the desired name of our new database.
2. Move tables from the old database to the new one
Then, we use the following MySQL command to "move" the tables of our old database to the new database:
RENAME TABLE dbold.tab1 TO dbnew.tab1; RENAME TABLE dbold.tab2 TO dbnew.tab2; RENAME TABLE dbold.tab3 TO dbnew.tab3;
The principle is this: in the database "dbold", we have the tables "tab1", "tab2" and "tab3". Through the command shown, we can "rename" the tables in a way, that they are included in the database "dbnew" after renaming. Because the new database has to exist to do this, we have created it previously.
Even with large databases with several million records, this function works very quickly in a few milliseconds. So, this solution is much more easier than to export the database to import it again.
This possibility works with both, MyISAM and InnoDB databases.
About the Author
You can find Software by Stefan Trost on sttmedia.com. Do you need an individual software solution according to your needs? - sttmedia.com/contact
Show Profile | Message
Related Topics
Tutorial | 0 Comments
Important Note
Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.
Participate
Ask your own question or write your own articles on askingbox.com. How to do.