How to Rename a Database and its Logical and Physical Files

By | In SQL Server | July 05th, 2016

As part of the database migration steps, you should also add the steps to rename the database if needed and its logical and physical files. While there are no requirements that the logical and physical file names need to align with the actual database name, providing self-documenting names will avoid confusion. In this blog, we will explore the required steps to query the existing logical and physical file name, rename a database, rename the logical file names, and rename the physical file names.

In a previous blog, How to Restore Your Backups from Striped Backup Files, we restored the AdventureWorks2012 database to create a new database, AdventureWorks2012_NEW. We can query the sys.master_files system table to view its logical and physical file names:

Script: QueryingLogicalPhysicalFilesName.sql


Logical File Names

Screen-shot: Logical and Physical File Names

In the following exercises, we will:

  1. Rename the AdventureWorks2012_NEW database as AW.
  2. Modify the logical files for the AW database to reflect the new database name.
  3. Set the AW database offline so that we can rename the physical files.
  4. Enable the xp_cmdshell system stored procedure.
  5. Rename the physical files of the AW database.
  6. Set the AW database online.
  7. Verify the new names of the logical and physical files.

To rename the AdventureWorks2012_NEW database as AW, run the following script:

Script: RenameDB.sql

If you query the sys.master_files system table again, you will notice that while the database name was modified, the logical and physical file names stayed the same as they were with the previous database name:

Script: QueryingLogicalPhysicalFileNamesAWDB.sql


AW Physical Logical Filenames

Screen-shot: AW database logical and physical file names

We will now modify the logical file names and then query the sys.master_files system table to check on the logical file name. As you can see, the logical file names have been updated.

Script: RenameLogicalFileNames.sql


Logical File Names Updated

Screen-shot: Logical File Names Updated

To rename the physical file name, we will need to set the database offline so that the files will not be in use:

Script: SetAWDBOffline.sql

While you can browse to the corresponding folders to rename the data and log files through the GUI, we will use the extended stored procedure xp_cmdshell to rename the files. First, you must enable it:

Script: EnableXP_CMDSHELL.sql

We will now rename the physical files of the AW database:

Script: RenamePhysicalFileNamesAW.sql

To update the file names and bring the AW database online, run the following script:

Script: UpdateFileNames.sql

If you query the sys.master_files system table again for the AW database, you will see that the database name, logical file names, and physical file names are now all updated:

Script: QueryingLogicalPhysicalFileNamesAWDB.sql


AW database logical and physical file names updated

Screen-shot: AW database logical and physical file names updated

Datavail Script: Terms & Conditions

By using this software script (“Script”), you are agreeing to the following terms and condition, as a legally enforceable contract, with Datavail Corporation (“Datavail”). If you do not agree with these terms, do not download or otherwise use the Script. You (which includes any entity whom you represent or for whom you use the Script) and Datavail agree as follows:

  1. CONSIDERATION. As you are aware, you did not pay a fee to Datavail for the license to the Script. Consequently, your consideration for use of the Script is your agreement to these terms, including the various waivers, releases and limitations of your rights and Datavail’s liabilities, as setforth herein.
  2. LICENSE. Subject to the terms herein, the Script is provided to you as a non-exclusive, revocable license to use internally and not to transfer, sub-license, copy, or create derivative works from the Script, not to use the Script in a service bureau and not to disclose the Script to any third parties. No title or other ownership of the Script (or intellectual property rights therein) is assigned to you.
  3. USE AT YOUR OWN RISK; DISCLAIMER OF WARRANTIES. You agree that your use of the Script and any impacts on your software, databases, systems, networks or other property or services are solely and exclusively at your own risk. Datavail does not make any warranties, and hereby expressly disclaims any and all warranties, implied or express, including without limitation, the following: (1) performance of or results from the Script, (2) compatibility with any other software or hardware, (3) non-infringement or violation of third party’s intellectual property or other property rights, (4) fitness for a particular purpose, or (5) merchantability.

You hereby release Datavail from any claims, causes of action, losses, damages, costs and expenses resulting from your downloading or other use of the Script.

5. AGREEMENT. These terms and conditions constitute your complete and exclusive legal agreement between you and Datavail.

Contact Us
JP Chen
Practice Leader of SQL Server Services
JP has over 13 years of experience in the IT industry specializing in web development, data analysis, and database administration. He has worked on supporting enterprise level corporations in the financial, retailing, pharmaceutical, aerospace, fashion, and education industries as a DBA and Tech Lead. He has initiated, designed, and developed comprehensive solutions on database documentation, monitoring and alerting, stabilization, and continuous improvements with performance monitoring and tuning. He enjoys sharing his new findings, knowledge, and practical hands-on experiences on SQL Server via his two blogs: and

Leave a Reply

Your email address will not be published.
Required fields are marked (*).