You can execute the following one-line script instead of converting each table’s name as in the first method. Step 4: Finally, you can drop the old database using the following query statement.Ĭode: mysql -u uname -p "pass" -e "DROP DATABASE old_Name_Of_DB" 3. 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:Ĭode: mysql -u uname -p "pass" new_Name_Of_DB < exportedDatabaseFile.sql Mysqldump -u uname -p "pass" old_Name_Of_DB > exportedDatabaseFile.sql Step 2: The next step is to export the backup file that dumps the existing database with the name old_Name_Of_DB. Step 1: The first step is to create a new database for which you can make use of the following query:Ĭode: mysql -u uname -p "pass" -e "CREATE DATABASE new_Name_Of_DB"Įxplanation: 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. 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. Then restore the backup file with the new name of the database that you wish to have. 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 you can execute the DROP DATABASE statement in the following manner:Įxplanation: where old_Name_Of_DB is the name of your old database that existed, and 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. Step 3: The last thing to do is drop the old existing database that you wanted to rename. To accomplish this, you can follow these steps.Ĭode: RENAME TABLE old_Name_Of_DB.presentTable1 TO new_Name_Of_DB.presentTable1, old_Name_Of_DB.presentTable2 TO new_Name_Of_DB.presentTable2 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. When referring to a table, we typically use the database name followed by a dot and the table name. Step 2: The next step is to rename the names of tables of the existing database. For that, you can execute the following statement.Įxplanation: where new_Name_Of_DB is the name of the new database. 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. Let us see each of the methods in detail one by one: 1. 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. If all the tables in your existing database are of type MyISAM, then you can use one trick. 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. Rename all the old database tables to new database names and then drop the old database.ĭump the existing database to generate backup and restore the backup with a new name of the database. Below is a list of some of the methods that can be followed to rename the database in the latest versions of MySQL:Ĭreate a new database with the name you wish to change to the older database. However, this method is not supported in the latest versions of MySQL for security reasons. Or ALTER DATABASE existing_DB_name MODIFY NAME = name_Of_new_DB Methods of Renaming the Database RENAME DATABASE existing_DB_name TO name_Of_new_DB
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |