A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/sql/sql-query-to-rename-database/ below:

SQL Query to Rename Database

SQL Query to Rename Database

Last Updated : 23 Jul, 2025

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:

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:

The database name is changed from Test to Example.

Important Considerations When Renaming a Database Troubleshooting Database Rename Issues 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.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4