SQL RENAME DATABASE



SQL RENAME DATABASE Statement

The SQL RENAME DATABASE statement is used to change the name of an existing database. This operation helps in reorganizing and updating the database schema while preserving all the data and tables within the database. Note that support for this statement may vary among different SQL database systems.


Syntax

ALTER DATABASE old_database_name
RENAME TO new_database_name;
  • ALTER DATABASE: This is the SQL keyword used to modify a database.
  • old_database_name: This specifies the current name of the database you want to rename.
  • RENAME TO: This is the SQL keyword used to specify the new name for the database.
  • new_database_name: This specifies the new name you want to assign to the database.

Example

Let's go through a complete example that includes creating a database, renaming it, and verifying the changes. Note that renaming a database is not supported by all SQL database systems. For demonstration purposes, we will assume the system supports it.

Step 1: Creating a Database

This step involves creating a new database named example_db.

CREATE DATABASE example_db;

In this example, we create a database named example_db.

Step 2: Renaming the Database

This step involves renaming the example_db database to new_example_db.

ALTER DATABASE example_db
RENAME TO new_example_db;

This command will rename the example_db database to new_example_db. Ensure your SQL database system supports this operation, as it may not be available in all systems.

Step 3: Verifying the Change

To verify that the database has been renamed, you can list all databases and check for the new name.

SHOW DATABASES;

This command will list all databases, allowing you to verify that example_db has been successfully renamed to new_example_db.