SQL RENAME Database

In some situations, database users and administrators want to change the name of the database for some technical reasons. So, the Rename Database statement in SQL is used to change the name of the existing database.

Sometimes, the Rename Database statement is used because the developers think that the original name is not more relevant to the data of the database, or they want to give a temporary name to that database.

Syntax of Rename Database in SQL

ALTER DATABASE old_database_name MODIFY NAME = new_database_name;  
EXEC sp_renamedb'old_database_name' , 'new_database_name'  

Syntax of Rename Database in MySQL

RENAME DATABASE old_database_name TO new_database_name;   

This syntax is used when we want to change the name of the database in MySQL.

Examples of Rename Database in SQL

In this article, we have taken the following two examples which will help you how to run and perform the Rename Database query in SQL:

Example 1:

Suppose we want to rename the Student Database. For this, we have to type the following query in SQL:

ALTER DATABASE Student MODIFY NAME = College ;  

This query will change the name of the database from Student to College. To run this query, we must ensure that the database Student exists in the current database server. If not, then it will show an error in the output.

Example 2:

Suppose we want to rename the Department Database. For this, we have to type the following query in SQL:

ALTER DATABASE Department MODIFY NAME = Company ;  

This query changes the name of the database from Department to Company. To run this query, we must ensure that the database Department exists in the current database server. If not, then it will show an error in the output.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *