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

clip_image002

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

clip_image004

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

clip_image006

Right click LibSys and click Properties

Change Restrict Access property to Single User

clip_image008

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.

clip_image010

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

how to fixed run time error 430 in vb6 and my database is sql server 2005, my connection is via DSN pls help me, thanks advance and more power from this site

Add new comment