EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials MySQL Tutorial MySQL rename database
Secondary Sidebar
MySQL Tutorial
  • Advanced
    • MySQL Formatter
    • MySQL TINYINT
    • MySQL Grant All Privileges
    • MySQL DROP TABLE
    • MySQL rename database
    • MySQL Flush Privileges
    • MySQL super Privilege
    • MySQL Character Set
    • MySQL Log File
    • MySQL Flush Log
    • Grant Privileges MySQL
    • MySQL WHILE LOOP
    • IF Statement in MySQL
    • MySQL CASE Statement
    • MySQL IF Function
    • MySQL IF EXISTS
    • MySQL UUID
    • Views in MySQL
    • MySQL Replication
    • MySQL Partition
    • Toad for MySQL
    • Navicat for MySQL
    • MySQL AES_Encrypt
    • MySQL Performance Tuning
    • MySQL Transaction
    • MySQL? sort_buffer_size
    • MySQL? Sync
    • MySQL? Query Cache
    • MySQL Collation
    • MySQL ODBC Driver
    • MySQL Partitioning
    • MySQL InnoDB
    • MySQL Float vs Decimal
    • MySQL Union vs Union All
  • Basic
    • MySQL floor
    • MySQL DESCRIBE table
    • MySQL encryption
    • Introduction to MySQL
    • Is SQL Server a Database?
    • What is MySQL
    • Is MySQL Programming Language
    • MySQL Server
    • MySQL AB
    • MySQL Community Server
    • How To Install MySQL
    • MySQL Versions
    • MySQL OpenSource
    • MySQL GUI Tools
    • MySQL Grant
    • MySQL Error 1064
    • MySQL Drop Database
    • MySQL not equal
    • MySQL SELECT INTO Variable
    • MySQL Commands
    • MySQL Operators
    • What is MySQL Schema
    • Wildcards in MySQL
    • MySQL Constraints
    • MySQL Administration
    • MySQL Data Type
    • MYSQL COMMIT
    • MySQL FORMAT
    • Timestamp to Date in MySQL
    • MySQL DATEDIFF
    • MySQL?Incremental Backup
    • MySQL JSON Data Type
    • MySQL ENUM
    • MySQL Default Port
    • Cheat Sheet MySQL
  • Queries
    • MySQL Queries
    • MySQL Query Commands
    • SELECT in MySQL
    • MySQL INSERT IGNORE
    • MySQL having
    • ORDER BY in MySQL
    • MySQL Cheat Sheet
    • MySQL ORDER BY Random
    • MySQL ORDER BY DESC
    • MySQL GROUP BY
    • MySQL GROUP BY Count
    • MySQL GROUP BY month
    • MySQL WHERE Clause
    • MySQL WITH
    • MySQL FETCH
    • MySQL DDL
    • MySQL DML
    • MySQL WHERE IN Array
    • MySQL Fetch Array
    • MySQL ISNULL
    • MySQL Index Types
    • Mysql? Export Schema
    • Amazon RDS for MySQL
    • MySQL greatest
  • Database
    • What is Data Modeling
    • What is Data Processing
    • DBMS Architecture
    • DBMS Keys
    • Careers in Database Administration
    • What is MySQL Database
    • MySQL Relational Database
    • How to Connect Database to MySQL
    • MySQL Database Repair
    • RDBMS Interview Questions
    • DBMS Interview Questions
  • Functions
    • MySQL Function
    • MySQL Aggregate Function
    • MySQL String functions
    • MySQL Date Functions
    • MySQL Window Functions
    • MySQL Math Functions
    • MySQL Boolean
    • Cursor in MySQL
    • Condition in MySQL
    • MySQL BETWEEN
    • Insert in MySQL
    • MySQL IFNULL()
    • MySQL TIMESTAMPDIFF()
    • MySQL COALESCE()
    • MySQL count()
    • MIN() in MySQL
    • MySQL Numeric
    • MySQL field()
    • MySQL FIND_IN_SET()
    • MySQL avg()
    • MySQL MAX() Function
    • MySQL BIN()
    • MySQL Concat
    • MySQL DECODE()
    • MySQL REGEXP_REPLACE()
    • MySQL Asynchronous
    • MySQL innodb_buffer_pool_size
    • MySQL key_buffer_size
    • MySQL TRUNCATE()
    • MySQL ROW_NUMBER()
    • NOT in MySQL
    • MySQL IN Operator
    • LIKE in MySQL
    • ANY in MySQL
    • MySQL NOT IN
    • MySQL CHECK Constraint
    • MySQL DISTINCT
    • MySQL ALL
    • MySQL Union
    • MySQL UNION ALL
    • MySQL EXISTS
    • MySQL ON DELETE CASCADE
    • MySQL REGEXP
    • MySQL Index
    • MySQL Add Index
    • MySQL REINDEX
    • MySQL UNIQUE INDEX
    • MySQL Clustered Index
    • MySQL? InnoDB Cluster
    • Table in MySQL
    • ALTER TABLE MySQL
    • MySQL Temporary Table
    • MySQL Clone Table
    • MySQL Repair Table
    • MySQL Lock Table
    • MySQL Optimize Table
    • TRUNCATE TABLE MySQL
    • MySQL Table Dump
    • MySQL Update Set
    • MySQL ALTER TABLE Add Column
    • MySQL RANK()
    • MySQL CTE
    • MySQL LAG()
    • MySQL GROUP_CONCAT()
    • MySQL EXTRACT()
    • MySQL REPLACE
    • MySQL AUTO_INCREMENT
    • MySQL SYSDATE()
    • MySQL NULLIF()
    • MySQL Substring
    • MySQL SUBSTRING_INDEX()
    • MySQL LOWERCASE
    • MySQL Row
    • MySQL NOW
    • MySQL CEIL
    • MySQL Alias
    • MySQL Trigger
    • MySQL SHOW Triggers
    • MySQL UPDATE Trigger
    • MySQL DELETE Trigger
    • MySQL AFTER UPDATE Trigger
    • MySQL Stored Procedure
    • ROLLUP in MySQL
    • MySQL? INSTR()
    • MySQL Subquery
    • MySQL Timestamp
    • MySQL? Hour()
    • MySQL MOD()
    • MySQL DATE_FORMAT()
    • ALTER Column in MySQL
    • MySQL Rename Column
    • MySQL Interval
    • MySQL CURDATE
    • MySQL BIT
    • MySQL Binlog
    • MySQL Average
    • MySQL TEXT
    • MySQL SHOW
    • MySQL Offset
    • MySQL Timezone
    • mysql_real_escape_string
    • MySQL Datetime
    • MySQL DATE_SUB()
    • MySQL FULLTEXT
    • MySQL DATE_ADD()
    • MySQL sum()
    • MySQL Merge
    • MySQL BigInt
    • MySQL ROUND
    • MySQL VARCHAR
    • MySQL Decimal
    • MySQL Limit
    • MySQL today()
    • MySQL WEEKDAY
    • MySQL Split
    • MySQL Create Function
    • MySQL BLOB
    • MySQL encode()
    • MySQL Primary Key
    • MySQL Foreign Key
    • Unique Key in MySQL
    • MySQL Drop Foreign Key
    • MySQL DROP TRIGGER
    • MYSQL Database
    • Delete Database MySQL
    • MySQL Root
    • MySQL Root Password
    • MySQL Client
    • MySQL Users
    • MySQL?User Permissions
    • MySQL add user
    • MySQL List User
    • MySQL Show Users
    • MySQL User Password
    • MySQL?Cardinality
    • MySQL Workbench
    • MySQL Backup
    • MySQL REVOKE
    • MySQL Dump
    • MySQL Cluster
    • MySQL Partitioning
    • MySQL Full Text Search
    • MySQL Admin Tool
    • MySQL Export Database
    • MySQL Export to CSV
  • Joins
    • Joins in MySQL
    • MySQL Outer Join
    • Left Outer Join in MySQL
    • MySQL Self Join
    • Natural Join in MySQL
    • MySQL DELETE JOIN
    • MySQL Update Join
    • MySQL Cross Join
  • Interview Questions
    • MySQL Interview Questions

Related Courses

MS SQL Certification Courses

Oracle Certification Courses

PL/SQL Certification Courses

MySQL rename database

MySQL rename database

Introduction to MySQL rename database

Sometimes, the database administrator may feel a necessity to change the name of the database that is present on a database server such as, when the project is to be deployed to the client-side or new project needs to share the same database and 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 that is versions before MySQL 5.1.23 had provided the simple method to rename the database using the RENAME DATABASE command. But this proved to be dangerous concerning the security and hence it was removed in all the new versions of MySQL starting from 5.1.23. In this article, we will learn about some of the techniques that can be used to change the name of the database safely and effectively.

How to Rename the database?

Many methods can be followed to rename the database. Out of them, which method is to be used depends on the context like how big is your database, what type of tables are present in your database, and so on. If your MYSQL version is older than MySQL 5.1.23, you can make the use of following commands:

RENAME DATABASE existing_DB_name TO name_Of_new_DB

or

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

ALTER DATABASE existing_DB_name MODIFY NAME = name_Of_new_DB

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,408 ratings)

Methods of Renaming the database

However, this method is not supported in the latest versions of MySQL for security reasons. Below is the 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 that you wish the older database to be changed. Rename all the tables of the old database 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 are using the Linux platform that will contain the code to get all the tables from the database and renaming all the tables to the database to the new database name. tablename format.

If all the tables present 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 that we need to perform is to create a new database with the name with which we wanted to rename the existing database. For that, you can simply 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. As we know that the table is completely referred to by using the name of the database in which it resides then dot operator and then the actual table name. So here we will rename it to the new database name then dot and then the table name that will lead the table to be placed and belong to the new database. This can be done in the following manner.

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 to drop the old database that existed and you wanted to rename. But before that, you can make sure that all your tables are present in the new database and the old database 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 to restore the backup file with the new name of the database that you wished 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 while backup and restoration.

Step 1: The first step is to create a new database for which you can make the 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 which 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 even 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 where 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 make use of 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 then restoring it with another name that is the new name of the database. One of the techniques that can be used if all of the tables inside your database are of MyISAM type is to shut down the MySQL server, rename the database folder name to a new name with which you wish to rename the database, and the restart MySQL server.

Recommended Articles

This is a guide to the MySQL rename database. Here we an introduction to MySQL rename database, how to do it, methods with an explanation. You can also go through our other related articles to learn more –

  1. Table in MySQL
  2. MySQL Queries
  3. Data Definition Language
  4. SQL Keywords
Popular Course in this category
MySQL Training Program (12 Courses, 10 Projects)
  12 Online Courses |  10 Hands-on Projects |  92+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

MS SQL Training (16 Courses, 11+ Projects)4.9
Oracle Training (14 Courses, 8+ Projects)4.8
PL SQL Training (4 Courses, 2+ Projects)4.7
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more