Home » Blog » SQL » How to Restore Database from MDF File?

How to Restore Database from MDF File?

  author
Published By Ashwani Tiwari
Anuraag Singh
Approved By Anuraag Singh
Modified On February 4th, 2026
Reading Time 8 Minutes Reading

How to Restore Database from MDF File in SQL Server? When a SQL Server database suddenly becomes inaccessible and only the MDF file is available well it can quickly turn into a critical situation if the LDF (log) file is missing, deleted or corrupted. This scenario is commonly occurs after a SQL Server crash, improper shutdown or hardware failure during server migration.

In such cases administrators are often left and start asking whether it is possible to restore a database from an MDF file only without rebuilding everything from scratch or losing valuable data but the good news is that SQL Server provides manual ways to attach or restore MDF files but let me tell you these methods work only under certain conditions and must be handled carefully.

You will learn from this article how to restore a database from an MDF file in SQL Server using both SQL Server Management Studio (SSMS) and T-SQL commands. We will also discuss common errors that may appear during the process and what to do when manual methods completely fail.

MDF file is the primary storage file of SQL Server in which all physical data are stored. SQL Server also uses some other files LDF (transaction log file), NDF (secondary storage file). Now, we talk about how to restore the database from the MDF file without the LDF file. There are some situations where we need to restore data from MDF files such as if we migrate SQL Server if we refuse to use old SQL servers, etc.

Now the question is how to attach MDF file to the SQL server? – There are two ways available to perform this task, in this post we cover both methods in a detailed manner. So, let’s start-

Common Situations Where You Need to Restore Database from MDF File

There are several situations where SQL Server administrators are left with only an MDF file and need to restore or attach the database without the original log file. You can check below some of the most common scenarios that includes:

  • SQL Server crash or unexpected shutdown
    A sudden power outage or system crash can damage and delete the LDF file and make the database unavailable while the MDF file remains safe.
  • LDF file deleted accidentally
    Do you know manual cleanup and incorrect maintenance tasks may remove the transaction log file requiring you to restore the database from MDF file only.

  • SQL Server migration or upgrade
    During server migration when databases are moved between different environments then sometimes only the MDF file is available and needs to be attached to a new SQL Server instance.

  • Database stuck in SUSPECT mode
    When a database enters SUSPECT mode because of the corruption or log issues so the administrators usually try to recover data by attaching the MDF file separately.

  • Hardware failure/disk corruption
    Well the hard drive issues and storage failure can corrupt your database components and make it necessary to attach an MDF file in SQL Server to regain access to the data.

In all above cases just understanding how to restore an MDF file in SQL Server without LDF becomes critical before attempting any manual or automated recovery method.

You can also read: How to Repair Corrupt SQL Server Database Easily

How to Restore Database From MDF File in SQL Server?

Here we will describe two methods to attach or restore MDF files in SQL Server

  1. By using SQL Server Management Studio
  2. By using T-SQL

Restore MDF File in SQL Server Without LDF by using SQL Server Management Studio

Follow all the given steps to successfully attach the .mdf file in SQL Server.

    1. Open SQL Server Managment Studio.
Step 1 to Restore Database from MDF File in SQL Server
    1. In Object Explorer, right-click on Database & then select attach.
Select Attach
    1. Now Attach Database window open, click on Add button.
Attach Data to Restore Database from MDF File
    1. Browse location of MDF file, then select the file and click on OK button.
Browse Location
  1. Now, you can see the database detail, to attach MDF file without LDF file you have select LDF file and then click on Remove button, then click OK.
Attach The Database to Restore

SQL Server will create an LDF file when the MDF file is attached successfully. Now, you have to check the database in the database folder.

Important Safety Warning Before Using T-SQL Method

Before attaching or restoring your MDF file using the FOR ATTACH_REBUILD_LOG T-SQL command it is strongly recommended to create a backup copy of your MDF file.

By using this method it will forces SQL Server to rebuild the transaction log file which result in the following-

  • Data inconsistency
  • Loss of uncommitted transactions
  • Structural issues if the MDF file is already corrupted

Now this approach is used only when the LDF file is missing and no recent backup is available and if your MDF file contains critical business data or shows signs of corruption then avoid repeated attach attempts.

👉 Pro Tip: If you have tried all manual methods and all of them are failed or errors occur during the attachment – consider using a professional MDF recovery solution instead of retrying risky commands.

Attach or Restore MDF File in SQL Server with T-SQL Script

To attach MDF file in SQL Server by using T-SQL you have run the following T-SQL script –

CREATE DATABASE testdatabase ON
(FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\testdatabase.mdf')
FOR ATTACH_REBUILD_LOG
GO

By using the above solutions you can easily restore the database from MDF file in SQL Server, but sometimes due to some errors users are not able to attach MDF in SQL Server. Some errors are discussed below –

Consider the user’s queries –

1 – “For some reason, I have to restore the database from MDF file only, this MDF file is stored in my machine. When I try to attach the .mdf file into SQL Server by using T-SQL I got an error 5123. So what I have to do in this situation”

Solution – You are getting a 5123 error message from SQL Server because there are permission issues in your MDF or database file. Cause of these issues you are not able to attach MDF file in SQL Server or you cannot restore the database from MDF file in SQL Server. To solve this error you have to change the permission as the owner of MDF file & then attach the file to the SQL server by following the above-given solutions.

2 – “I have used T-SQL script to restore database from MDF file in SQL server, but when I execute the command SQL Server display a 5172 error (the header for file mdf is not a valid database file header. The file size property incorrect)” so how do I attach .mdf in SQL Server.”

Solution – This error occurs when header information of the MDF file gets corrupted and the database becomes inaccessible, so to resolve these issues you have to repair the MDF file. To remove error 5172 you need to use the Tool.

 

Download the Tool –

After repairing the MDF file by using the software you can attach MDF in SQL Server. This Software allows the user to recover deleted SQL database objects as well as deleted SQL table records. The user can easily repair the Primary as well as Secondary files by using this software. Users can also save the recovered data as SQL Server Compatible script or CSV File. The tool provides an option to export the data into a live SQL Server present in the same machine or a server network. Moreover, This software supports Microsoft SQL Server 2019 / 2017 / 2016 / 2014 / 2012 and below version.

Follow the below-mentioned steps to Restore database from MDF file only

  1. Install and Run the Application and Click on Open.
Browse the MDF file

2. Browse the MDF file from your system. Next Select the SQL Server version and the Advanced Scan Mode. ( The user can also check the recover deleted objects option if required.)

Preview the SQL database objects SQL Table

3. Preview the SQL database objects SQL Table, stored procedure, functions, views, indexes, etc. ( This software shows the deleted SQL database objects and table records in red color.)

Choose an Export option

4. Choose an Export option and fill in the required details to restore data from MDF file.

choose the database object you want to save

5. Now, choose the database object you want to save and then click on Export button.

Click on The Export Option

You way also interested knowing:- Recover Database from Emergency Mode in SQL Server – Check all Effective Methods.

Conclusion

In many situations it is possible to restore a SQL Server database from an MDF file when the LDF file is missing or also corrupted. As this guide explains well the most safest ways is always to start with proven manual methods like attaching your MDF file by using SQL Server Management Studio (SSMS) and carefully executing T-SQL commands if it is necessary.

If all your manual methods are failed due to MDF corruption or header errors or database inconsistency then you should move to a reliable MDF recovery tool as this becomes the practical and best option for you.

The software solutions are fully designed to handle your complex situation like corrupted MDF files or missing log files and databases stuck in SUSPECT mode where SQL Server built in methods are not able to restore your database successfully.

This situation highlights the importance of regular SQL Server backups. Having recent backups of both the MDF and LDF files can prevent future downtime and data loss also emergency recovery efforts.