- SQL Tutorial
- SQL - Home
- SQL - Overview
- SQL - RDBMS Concepts
- SQL - Databases
- SQL - Syntax
- SQL - Data Types
- SQL - Operators
- SQL - Expressions
- SQL Database
- SQL - Create Database
- SQL - Drop Database
- SQL - Select Database
- SQL - Rename Database
- SQL - Show Database
- SQL - Backup Database
- SQL Table
- SQL - Create Table
- SQL - Show Tables
- SQL - Rename Table
- SQL - Truncate Table
- SQL - Clone Tables
- SQL - Temporary Tables
- SQL - Alter Tables
- SQL - Drop Table
- SQL - Delete Table
- SQL - Constraints
- SQL Queries
- SQL - Insert Query
- SQL - Select Query
- SQL - Select Into
- SQL - Insert Into Select
- SQL - Update Query
- SQL - Delete Query
- SQL - Sorting Results
- SQL Views
- SQL - Create Views
- SQL - Update Views
- SQL - Drop Views
- SQL - Rename Views
- SQL Operators and Clauses
- SQL - Where Clause
- SQL - Top Clause
- SQL - Distinct Clause
- SQL - Order By Clause
- SQL - Group By Clause
- SQL - Having Clause
- SQL - AND & OR
- SQL - BOOLEAN (BIT) Operator
- SQL - LIKE Operator
- SQL - IN Operator
- SQL - ANY, ALL Operators
- SQL - EXISTS Operator
- SQL - CASE
- SQL - NOT Operator
- SQL - NOT EQUAL
- SQL - IS NULL
- SQL - IS NOT NULL
- SQL - NOT NULL
- SQL - BETWEEN Operator
- SQL - UNION Operator
- SQL - UNION vs UNION ALL
- SQL - INTERSECT Operator
- SQL - EXCEPT Operator
- SQL - Aliases
- SQL Joins
- SQL - Using Joins
- SQL - Inner Join
- SQL - Left Join
- SQL - Right Join
- SQL - Cross Join
- SQL - Full Join
- SQL - Self Join
- SQL - Delete Join
- SQL - Update Join
- SQL - Left Join vs Right Join
- SQL - Union vs Join
- SQL Keys
- SQL - Unique Key
- SQL - Primary Key
- SQL - Foreign Key
- SQL - Composite Key
- SQL - Alternate Key
- SQL Indexes
- SQL - Indexes
- SQL - Create Index
- SQL - Drop Index
- SQL - Show Indexes
- SQL - Unique Index
- SQL - Clustered Index
- SQL - Non-Clustered Index
- Advanced SQL
- SQL - Wildcards
- SQL - Comments
- SQL - Injection
- SQL - Hosting
- SQL - Min & Max
- SQL - Null Functions
- SQL - Check Constraint
- SQL - Default Constraint
- SQL - Stored Procedures
- SQL - NULL Values
- SQL - Transactions
- SQL - Sub Queries
- SQL - Handling Duplicates
- SQL - Using Sequences
- SQL - Auto Increment
- SQL - Date & Time
- SQL - Cursors
- SQL - Common Table Expression
- SQL - Group By vs Order By
- SQL - IN vs EXISTS
- SQL - Database Tuning
- SQL Function Reference
- SQL - Date Functions
- SQL - String Functions
- SQL - Aggregate Functions
- SQL - Numeric Functions
- SQL - Text & Image Functions
- SQL - Statistical Functions
- SQL - Logical Functions
- SQL - Cursor Functions
- SQL - JSON Functions
- SQL - Conversion Functions
- SQL - Datatype Functions
- SQL Useful Resources
- SQL - Questions and Answers
- SQL - Quick Guide
- SQL - Useful Functions
- SQL - Useful Resources
- SQL - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
SQL - Rename Database
There can be several reasons to rename a database name. One of the reasons could be to avoid naming conflicts or to separate different types of data into different databases. Another reason can be to arrange them in an organized way that can make them more descriptive and easier to identify.
SQL provides a simple ALTER DATABASE…MODIFY command to rename an existing database.
SQL Server also provides a built-in stored procedure called sp_renamedb, which can be used to rename a database name.
Note − If you have decided to rename a database, make sure there are no active transactions where the old database name is being used, otherwise the complete operation might halt once you rename the database.
ALTER DATABASE…MODIFY Query
The ALTER DATABASE…MODIFY query in SQL is used to rename the name of an existing user-created database in an SQL Server.
Syntax
Following is the syntax of the ALTER DATABASE…MODIFY command −
ALTER DATABASE existing_database_name MODIFY NAME = new_database_name;
Example
Before renaming the database, let us list all the databases present in the SQL server. To perform that we need to use the following query −
SQL> SELECT name From sys.databases
Following are the databases that exist in the SQL server −
+-------------+ | name | +-------------+ | master | | tempdb | | model | | msdb | +-------------+
Here, the master, tempdb, model, and msdb are the databases that are created automatically at the time of SQL server installation.
Now, let us create a new database on the SQL server using the query below −
SQL> CREATE DATABASE testDB;
As we can see, the database has been created.
+-------------+ | name | +-------------+ | master | | tempdb | | model | | msdb | | tempDB | +-------------+
Now, let us try to rename the user-created database named “testDB” to “tutorialsDB” using the ALTER DATABASE…MODIFY query.
SQL> ALTER DATABASE testDB MODIFY NAME = tutorialsDB;
Output
When we execute the above query, the output is obtained as follows −
The database name 'tutorialsDB' has been set.
Note − Before executing the above query, you need to make sure that the database "testDB" is present in the current database server. Else, the above query will return an error.
Verification
Let us verify whether the database's name has been changed or not using the following query −
SQL> SELECT name From sys.databases;
The above query lists all the names of the databases and we can see that the name of the database has changed.
+-------------+ | name | +-------------+ | master | | tempdb | | model | | msdb | | tutorialsDB | +-------------+
Renaming database using sp_renamedb
Another way to rename the existing user-created database in an SQL server is by using the stored procedure.
Syntax
Following is the syntax of the stored procedure sp_renamedb −
EXEC sp_renamedb 'existing_database_name', 'new_database_name'
Example
Let us try to rename the user-created database “tutorialsDB” to “testDB” using the following query.
SQL> Exec sp_renamedb 'tutorialsDB', 'testDB';
Output
When we execute the above query, the output is obtained as follows −
The database name 'tutorialsDB' has been set.
Verification
Let us verify whether the database's name has been changed or not using the following query −
SQL> SELECT name From sys.databases
The above query lists all the names of the databases and we can see that the name of the database has changed.
+-------------+ | name | +-------------+ | master | | tempdb | | model | | msdb | | testDB | +-------------+
Renaming a System Database
While we install the SQL server, some databases are automatically created and those are called “System Databases”.
Example
Let us try to rename the system-created database “master” to “student” using the query below −
SQL> ALTER DATABASE master MODIFY NAME = student;
Error
As we can see in the output below, the above query returned an error because we cannot rename the system-created database.
Cannot change the name of the system database master.