Open In App

SQL Query to Rename Database

Last Updated : 13 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Renaming a database in SQL is an essential task that database administrators and developers frequently perform. Whether you’re reorganizing your data, correcting naming conventions, or simply updating your project structure, knowing how to rename a database properly is critical.

In this article, we'll cover everything you need to know about renaming a database using SQL, including the exact commands for various SQL-based database management systems (DBMS) such as MySQL, PostgreSQL, and SQL Server.

How to Rename Database in SQL?

To rename a database in SQL, we will need to use the ALTER DATABASE command. The syntax varies slightly between different SQL platforms, but the core functionality remains the same. The ALTER DATABASE statement allows us to modify the properties of a database, including its name.

However, it’s important to note that while SQL Server uses the ALTER DATABASE statement with a MODIFY NAME clause, MySQL no longer supports the RENAME DATABASE statement as of version 5.1.23. To change the name of a database in SQL, use the following syntax:

SQL Server:

ALTER DATABASE [current_database_name]
MODIFY NAME = [new_database_name];

MySQL:

For MySQL versions 5.1.23 and later, the RENAME DATABASE command is no longer supported. Instead, you need to follow an alternative approach to rename the database. The steps involves creating a new database and transferring the tables from the old database to the new one. Here's how you can do it:

1. Create a new database:

CREATE DATABASE new_database_name;

2. Transfer all the tables from the old database to the new one:

RENAME TABLE old_database_name.table1 TO new_database_name.table1;
RENAME TABLE old_database_name.table2 TO new_database_name.table2;
-- Repeat for all tables in the old database

This process works because MySQL does not allow the RENAME DATABASE command anymore due to potential issues with the operation. So, we need to create a new database, transfer the data and then remove the old one.

PostgreSQL:

For PostgreSQL, you can rename a database using the ALTER DATABASE command:

ALTER DATABASE current_database_name RENAME TO new_database_name;

SQL Rename Database Example

Let's look at an example of how to rename a database in SQL. First, we will create a database which will be renamed in the example:

Step 1: Create a Database

CREATE DATABASE Test;

Output:

database created

The test database is created.

Step 2: Rename Database in SQL Server

In this example, we will use the ALTER command with MODIFY NAME clause to rename the database in SQL Server

ALTER DATABASE Test MODIFY NAME = Example

Output:

database renamed

The database name is changed from Test to Example.

Important Considerations When Renaming a Database

  • Database Availability: Please be aware that renaming a database may temporarily render it inaccessible while the process is underway. It is advisable to schedule this task during off-peak hours, particularly in a production environment.
  • Dependencies: Ensure that any applications, scripts, or users relying on the database name are updated to reflect the new designation.
  • Permissions: Confirm that you possess the requisite permissions to rename the database, which typically necessitates administrative or root-level access.
  • Backups: Prior to renaming a database, it is prudent to create a backup, especially in a production environment, to mitigate the risk of data loss should any issues arise.

Troubleshooting Database Rename Issues

  • Database is in Use: If you encounter an error saying that the database is in use, you may need to disconnect active users or close any applications connected to the database before renaming it.
  • Insufficient Privileges: If you receive a permission error, ensure you have administrative privileges or sufficient rights to modify the database. You might need to check your user role and permissions.
  • Database Name Constraints: Some DBMSs have restrictions on certain characters or reserved words in database names. Ensure your new database name adheres to the naming conventions for the specific SQL system you are using.

Conclusion

Renaming a database in SQL is a straightforward process, but it requires careful consideration and proper syntax to avoid errors. Whether you're using SQL Server, MySQL, or PostgreSQL, knowing how to rename a database using the correct SQL commands is essential for efficient database management. Always ensure that you follow best practices, including making backups and ensuring that all dependencies are updated, to minimize disruptions when renaming your databases.


Next Article

Similar Reads