Table of Content
    SQL Database Repair

    How to Restore Database from MDF File?


    Table of Content

      Summary: This blog describes how to restore database from MDF file in SQL Server using SQL Server Management Studio (SSMS) or T-SQL query. If the MDF file used to restore SQL database is corrupt or damaged, try using Stellar Repair for MS SQL software to repair MDF file and restore the database to its original form.

      SQL Server uses the primary data file (.MDF) to store data in form of tables and other database objects. Sometimes, you may need to restore the SQL database from an MDF file. Some of them are as follows:

      • SQL Server crash  
      • Upgrading the current SQL Server version to a higher version
      • Migrating a database to another location

      SQL Server uses an MDF file to restore data from a database and a transaction log file to restore the database logs. However, if you don’t have a log file, you can use the primary data file alone to perform the restore process. But, you can only attach an MDF file without a transaction log file (LDF) if your database was properly shutdown and there were no open transactions during the shutdown.

      Methods to Restore Database from MDF File in SQL Server

      In order to restore a SQL database from the master database file (.mdf), you need to:

      Method 1 – Attach SQL Server Database using MDF File

      • Attach a Database using SQL Server Management Studio (SSMS)
      • Attach a SQL Database using Transact-SQL (T-SQL)

      Method 2 – Use SQL Repair Tool to Restore Database

      Let’s discuss both these methods in detail:

      Method 1 – Attach SQL Server Database using MDF File

      Note: You cannot attach a damaged (or corrupted) MDF file in SQL Server. In that case, use DBCC CHECKDB with repair options or a specialized MDF file repair tool to fix the corrupted file and restore database from mdf file.

      Following are the two methods to attach SQL database using an .mdf file:

      Attach a Database using SQL Server Management Studio (SSMS)

      Follow these steps to attach a database using SSMS:

      • Open SSMS and connect to an instance of SQL Server.
      Connect to SQL Server instance
      Figure 1 – Connect to SQL Server Instance
      • Right-click on Databases, and then select Attach.
      Select Attach Databases
      Figure 2 – Select Attach Databases
      • From the ‘Attach Databases’ window, click Add to select the database you want to attach.
      Click Add to Attach SQL Database
      Figure 3 – Select Add to Attach SQL Database
      • From the ‘Locate Database Files’ dialog box, browse and select the .mdf file of the database to be attached. Press OK.
      Select MDF File to be Attached
      Figure 4 – Select MDF File to be Attached

      Note: Are you receiving ‘Attach database failed for server’ error message with 5173 error code? Refer to this link for information on how to fix the error.

      • The ‘Attach Databases’ window appears with MDF and LDF files displayed under the database details: section.
      Stellar
      Figure 5 – SQL Database Files
      • Click OK to restore your database (i.e. TestDB in our case).

      Attach a SQL Database Using Transact-SQL (T-SQL)

      Follow these steps to attach a SQL Server database using T-SQL:

      • Open SSMS and connect to the Database Engine.
      Connect to Database Engine
      Figure 6 – Connect to Database Engine
      • Click on the New Query tab.
      Select New Query
      Figure 7 – Select New Query
      • Execute the following CREATE DATABASE command with “FOR ATTACH” clause to attach your SQL Server database:
      CREATE DATABASE TestDB ON (FILENAME = 'C:\MySQLServer\TestDB_Data.mdf'), (FILENAME = 'C:\MySQLServer\ TestDB_Log.ldf') FOR ATTACH; 

      Note: Replace ‘TestDB’ with the database you want to attach in SQL Server.

      Method 2 – Use SQL Repair Tool to Restore Database

      If you failed to attach a database due to corrupted or damaged MDF file, use Stellar Repair for MS SQL software to repair the MDF file. Once the file is repaired, you can restore the database from MDF file keeping all the file data intact.

      Stellar

      The SQL database repair tool can repair a severely corrupted MDF file and recover all its data in just a few clicks. It can handle all types of database corruption errors like 824, 825, 5172, 9001, etc. in SQL Server.

      Conclusion

      In the event of SQL Server crash, upgrading SQL Server to the latest version, or migrating your database to another location, you will need to restore your database from the primary data file (MDF). You can restore database from MDF files in SQL Server using the methods discussed in this blog. These methods apply to all supported SQL Server versions 2019, 2017, 2016, 2014, and earlier. You can attach the MDF file using SSMS or T-SQL and restore the database. However, if the file is corrupt, you may use DBCC CHECKDB with ‘REPAIR_ALLOW_DATA_LOSS’ to fix MDF file corruption. Or else, use Stellar Repair for MS SQL software to repair MDF file without any risk of losing data.

      Was this article helpful?

      No NO

      About The Author

      Charanjeet Kaur linkdin

      Charanjeet is a Technical Content Writer at Stellar®who specializes in writing about databases, e-mail recovery, and e-mail migration solutions. She loves researching and developing content that helps database administrators, organizations and novices to fix multiple problems related to MS SQL and MySQL databases and Microsoft Exchange.

      Leave a comment

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

      Image Captcha
      Refresh Image Captcha

      Enter Captcha Here :

      Related Posts

      WHY STELLAR® IS GLOBAL LEADER

      Why Choose Stellar?

      • 0M+

        Customers

      • 0+

        Years of Excellence

      • 0+

        R&D Engineers

      • 0+

        Countries

      • 0+

        PARTNERS

      • 0+

        Awards Received