Here we will learn how to change or rename or alter databases in sql server using query or script or management studio.
To alter or rename or change name of SQL database we have two different methods one is using Query and another one is directly using SQL Server Management Studio
By using “ALTER Database” statement we can change name of SQL database. Generally to alter or rename database in SQL our syntax will be like as shown below
We have two ways to change name of database in SQL using query those are
Following is the syntax to alter or change sql database using ALTER Database statement
ALTER DATABASE olddbname MODIFY NAME= newdbname
Following is the syntax to change or rename sql database using EXEC sp_renamedb statement
EXEC sp_renamedb'olddbname', 'newdbname'
Here “olddbname” means name of the present database and “newdbname” means its new name for database
The following SQL statements will rename or change database name from “sqltutorialexamples” to “sqltutorialexample”.
Following is the example to alter database name using ALTER DATABASE statement in sql server
ALTER DATABASE sqltutorialexamples MODIFY NAME= sqltutorialexample
Following is the example rename or change database name using EXEC sp_renamedb statement in sql server
EXEC sp_renamedb'sqltutorialexamples', 'sqltutorialexample'
Refresh your sql database section and check for database with new name that would be like as shown below
To change database name open SQL Server Management Studio --> Select your database in Databases section --> Right click and Select Rename Database like as shown below
If you are getting any problem to rename database by using SQL management studio might be that problem because of already another process or application using database with same name to fix this problem try to restart SQL Server running service it will abort all the applications or services which using this database.
To restart service Search for Services à Select SQL Server à Right click Restart Service like as shown below
Once you restart service now right click on your database --> select Rename Database and change name. Once we rename database that will be like as shown below. Here we renamed database “sqltutorialexamples” to “sqltutorialexample”