Updated June 1, 2023
Introduction to MySQL Rename Database
The database administrator may need to modify the name of the database on a server in certain instances. This usually happens when the client side implements a project or when a new project needs to use the same database. Now, you feel that the existing name of the database is not that relevant or meaningful as per the new context. The older versions of MySQL, versions before MySQL 5.1.23, had provided the simple method to rename the database using the RENAME DATABASE command. But this proved dangerous concerning security, so MySQL versions 5.1.23 and onwards removed the feature. To rename a database safely and effectively, follow these instructions:
How to Rename the Database?
You can rename a database using several different methods. Out of them, which method will be used depends on the context, like how big your database is, what type of tables are present in it, and so on. If your MYSQL version is older than MySQL 5.1.23, you can make use of the following commands:
RENAME DATABASE existing_DB_name TO name_Of_new_DB
or
ALTER DATABASE existing_DB_name MODIFY NAME = name_Of_new_DB
Methods of Renaming the Database
However, this method is not supported in the latest versions of MySQL for security reasons. Below is a list of some of the methods that can be followed to rename the database in the latest versions of MySQL:
Create a new database with the name you wish to change to the older database. Rename all the old database tables to new database names and then drop the old database.
Dump the existing database to generate backup and restore the backup with a new name of the database.
Write the script in case you use the Linux platform containing the code to get all the tables from the database and rename all the tables to the new database name. tablename format.
If all the tables in your existing database are of type MyISAM, then you can use one trick. Just close your MySQL server and then rename the directory of your database, i.e., the folder of the database then; that will automatically lead to a changed database name with the changed directory name.
Let us see each of the methods in detail one by one:
1. Renaming the tables
Step 1: The first step we need to perform is to create a new database with the name we want to rename the existing database. For that, you can execute the following statement.
Code:
CREATE database new_Name_Of_DB;
Explanation: where new_Name_Of_DB is the name of the new database.
Step 2: The next step is to rename the names of tables of the existing database. When referring to a table, we typically use the database name followed by a dot and the table name. If we want to rename a table and move it to a new database, we can easily do so by using the new database name, followed by a dot and the table name. To accomplish this, you can follow these steps.
Code:
RENAME TABLE old_Name_Of_DB.presentTable1 TO new_Name_Of_DB.presentTable1, old_Name_Of_DB.presentTable2 TO new_Name_Of_DB.presentTable2;
Step 3: The last thing to do is drop the old existing database that you wanted to rename. But before that, you can ensure all your tables are present in the new database, and the old one is empty to avoid data loss. Then you can execute the DROP DATABASE statement in the following manner:
Code:
DROP database old_Name_Of_DB;
Explanation: where old_Name_Of_DB is the name of your old database that existed, and you wanted to rename.
2. Dump and restore the database
One of the methods that can be followed to rename the database for small databases easily and effectively is to dump the database to create a backup file. Then restore the backup file with the new name of the database that you wish to have. But note that this method works well only with small databases as it creates a problem if the data is sufficiently big and large during backup and restoration.
Step 1: The first step is to create a new database for which you can make use of the following query:
Code:
mysql -u uname -p "pass" -e "CREATE DATABASE new_Name_Of_DB"
Explanation: where new_Name_Of_DB is the name you wanted to assign to the existing name and uname is the username of MySQL, and pass is the password of your user login if set.
Step 2: The next step is to export the backup file that dumps the existing database with the name old_Name_Of_DB.
Code:
mysqldump -u uname -p "pass" old_Name_Of_DB > exportedDatabaseFile.sql
Step 3: Now. You will have to import the database backup that restores the data from the exported file, in my case, exportedDatabaseFile.sql to the new database named new_Name_Of_DB in my case using the following query statement:
Code:
mysql -u uname -p "pass" new_Name_Of_DB < exportedDatabaseFile.sql
Step 4: Finally, you can drop the old database using the following query statement.
Code:
mysql -u uname -p "pass" -e "DROP DATABASE old_Name_Of_DB"
3. script to be executed on command-line
You can execute the following one-line script instead of converting each table’s name as in the first method. The script statement is somewhat like the following: you can replace the old_Name_Of_DB and new_Name_Of_DB with your existing and new database names.
Code:
$ mysql -u uname -p "pass" old_Name_Of_DB -sNe 'show tables' | while read table; do mysql -u uname -p "pass" -sNe "RENAME TABLE old_Name_Of_DB.$table TO new_Name_Of_DB.$table";
Conclusion
We cannot use the RENAME DATABASE command in the newer versions of MySQL for renaming the database. However, there are different alternatives available that can be followed to rename the database. Some of them are renaming the names of tables to new database names, while another method suggests dumping the database to create the backup file and restoring it with another name, the database’s new name. To rename a database of MyISAM type, you need to shut down the MySQL server, rename the database folder with a new name of your choice, and then restart the MySQL server.
Recommended Articles
We hope that this EDUCBA information on “MySQL rename database” was beneficial to you. You can view EDUCBA’s recommended articles for more information.