How to Rename SQL Server Database and the Database Filename (.mdf file)
In this tutorial I will teach you on how to rename an SQL Server Database and its filename.
For this purpose, we will use "LibSys" database on my SQL Server Management tools and rename it to "LibrarySystem". Remember that we have to rename the following:
- Filename
- Logical name
- Database name
As opposed to other tutorial, they only rename either Logical name or Filename of the database. In the case of this tutorial, we will rename all of them.
Now open your SQL Server Management and follow the screen below choosing the database of your choice in your SQL Server.
Click the image to enlarge.
Create New Query window
Type the following command in the SQL Query window:
Alter Database LibSys Modify File ( NAME = 'LibSys', FILENAME = 'D:\Data\LibrarySystem.mdf')
Alter Database LibSys Modify File ( NAME = 'LibSys_log', FILENAME = 'D:\Data\LibrarySystem_log.ldf')
Change Logical Name of the database and its log file
Create another SQL Query file and paste the following code:
ALTER DATABASE LibSys MODIFY FILE (NAME = LibSys, NEWNAME = LibrarySystem)
ALTER DATABASE LibSys MODIFY FILE (NAME = LibSys_log, NEWNAME = LibrarySystem_log)
Rename Database Name
Right click LibSys and click Properties
Change Restrict Access property to Single User
Click Options then select SINGLE_USER in the drop down list of Restrict Access property.
Click Yes if ask to close all connections to the database.
Right click the database and click Rename.
Now, we're done. All you have to do now is to detach the database and attach it again. If you received an error, right click Databases and click Refresh.
Browse the database using Windows Explorer and rename the physical file to:
LibrarySystem.mdf
LibrarySystem_log.log
For more information on how to Attach/Detach database please follow this link: http://www.sourcecodester.com/Tutorials/how-attach-and-detach-a-database.html
Comments
Add new comment
- Add new comment
- 72 views