How to Restore Database from MDF File?

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 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

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:

Figure 1 – Connect to SQL Server Instance
Figure 2 – Select Attach Databases
Figure 3 – Select Add to Attach SQL Database
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.

Figure 5 ? SQL Database Files

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

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

Figure 6 – Connect to Database Engine
Figure 7 – Select New Query
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.

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.

Related Post