Renaming a Database in SQL Server

Sometimes it is asked in interview to write a query how to rename or change your DB name. I am explaning here two ways to change Database name.

We can rename a database using 2 ways
  • By using alter command
  • By using system stored Procedure.
So here i have a database named as practice.
 
 
 
 I need to change my database name
  • Using alter command.
For alter command the syntax will be
 
Alter Database Databasename modify name=newdatabaseName

So the query  will be
  1. alter database practice modify name=Practice1  
And thus the database name will be changed now.
 
 
 
So in this way we can change our database name using alter command.
  • By using system stored Procedure.
The syntax for this is
 
SP_RenameDB 'old DBname','New DB name'  
 
Now i want to change it back to practice
 
So my query will be
  1. sp_renameDB 'practice1','practice'  
 
Thus again it will change to Practice.