- SQL - Discussion
- SQL - Useful Resources
- SQL - Useful Functions
- SQL - Quick Guide
- SQL - Questions and Answers
- SQL - Datatype Functions
- SQL - Conversion Functions
- SQL - JSON Functions
- SQL - Cursor Functions
- SQL - Logical Functions
- SQL - Statistical Functions
- SQL - Text & Image Functions
- SQL - Numeric Functions
- SQL - Aggregate Functions
- SQL - String Functions
- SQL - Date Functions
- SQL - Database Tuning
- SQL - IN vs EXISTS
- SQL - Group By vs Order By
- SQL - Common Table Expression
- SQL - Cursors
- SQL - Date & Time
- SQL - Auto Increment
- SQL - Using Sequences
- SQL - Handling Duplicates
- SQL - Sub Queries
- SQL - Transactions
- SQL - NULL Values
- SQL - Stored Procedures
- SQL - Default Constraint
- SQL - Check Constraint
- SQL - Null Functions
- SQL - Min & Max
- SQL - Hosting
- SQL - Injection
- SQL - Comments
- SQL - Wildcards
- SQL - Non-Clustered Index
- SQL - Clustered Index
- SQL - Unique Index
- SQL - Primary Key
- - 工会诉Join
- SQL - Inner Join
- SQL - Using Joins
- SQL - Aliases
- SQL - EXCEPT Operator
- SQL - INTERSECT Operator
- SQL - UNION vs UNION ALL
- SQL - UNION Operator
- SQL - BETWEEN Operator
- SQL - NOT NULL
- SQL - IS NOT NULL
- SQL - IS NULL
- SQL - NOT EQUAL
- SQL - NOT Operator
- SQL - CASE
- SQL - EXISTS Operator
- SQL - ANY, ALL Operators
- SQL - IN Operator
- SQL - LIKE Operator
- SQL - BOOLEAN (BIT) Operator
- SQL - AND & OR
- SQL - Having Clause
- SQL - Group By Clause
- SQL - Order By Clause
- SQL - Distinct Clause
- SQL - Top Clause
- SQL - Where Clause
- SQL - Rename Views
- SQL - Drop Views
- SQL - Update Views
- SQL - Create Views
- SQL - Sorting Results
- SQL - Delete Query
- SQL - Update Query
- SQL - Insert Into Select
- SQL - Select Into
- SQL - Select Query
- SQL - Insert Query
- SQL - Constraints
- SQL - Delete Table
- SQL - Drop Table
- SQL - Alter Tables
- SQL - Temporary Tables
- SQL - Clone Tables
- SQL - Truncate Table
- SQL - Rename Table
- SQL - Show Tables
- SQL - Create Table
- SQL - Backup Database
- SQL - Show Database
- SQL - Rename Database
- SQL - Select Database
- SQL - Drop Database
- SQL - Create Database
- SQL - Expressions
- SQL - Operators
- SQL - Data Types
- SQL - Syntax
- SQL - Databases
- SQL - RDBMS Concepts
- SQL - Overview
- SQL - Home
5. 图瓦卢
- 页: 1
- 页: 1
- 结构-创建指数
- 页: 1
- 页: 1
- 页: 1
- SQL - Foreign Key
- 文 件
- ∗ E/CN.6/2009/1。
- 页: 1
- 页: 1
- 文 件
- 页: 1
- 页: 1
- 文 件
- 页: 1
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
SQL - Rename Database
There can be several reasons to rename a database name. One of the reasons could be to avoid naming confpcts 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 pst 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 psts 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 psts 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.Advertisements