MySQL: Rename Name of a Database
Tip by Stefan Trost | Last update on 2023-01-18 | Created on 2012-05-11
In this tip, I want to show you how to change the name of a MySQL 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 quickly because of the risk of losing data, we have to make use of a simple trick to rename a database.
As an example, we want to rename the database db_old to db_new. We need two steps for the renaming:
Step 1: Create an empty Database
First, we create an empty database with the desired name of our new database.
The easiest way is to just use the functions of phpMyAdmin for that. Of course, we can also use the MySQL command "CREATE DATABASE", which would result in "CREATE DATABASE db_new" in our case.
Step 2: Move Tables from the old Database to the new one
In the next step, we use the following MySQL command for each of our tables to "move" the tables of our old database to the new database:
RENAME TABLE db_old.tab1 TO db_new.tab1; RENAME TABLE db_old.tab2 TO db_new.tab2; RENAME TABLE db_old.tab3 TO db_new.tab3; ...
The principle is the following: in the database "db_old", 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 "db_new" 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 using another new name.
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
Related Topics
Rename File to its Folder Name
Tutorial | 0 Comments
MySQL: CSV Export as automatic Download
Tutorial | 0 Comments
PHP: Save MySQL Result in Array
Info | 0 Comments
Change uppercase and lowercase Writing of multiple Files
Tutorial | 0 Comments
HTML: Preassign HTML Form with Data
Tutorial | 0 Comments
PHP/MySQL: How to store dynamic field's values in Database
Open Question | 0 Answers
MySQL: CSV Export as File stored on the Server
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 article on askingbox.com. That’s how it’s done.
In phpMyAdmin there is also a function for that. The function can be found when you have selected the database under "Operations".
However, there are two problems with it:
Hence a very good and useful tip!
2023-01-17 at 19:47