How to Repair MDF File in SQL Server Database?

MDF (Master Database File) is the primary database file in MS SQL Server. Each SQL Server database contains at least one MDF file. This primary data file contains the SQL database schema and all the data, including indexes, views, tables, triggers, stored procedures, and other critical components. Therefore, this file is essential for data storage in MS SQL Server environment.

However, sometimes, the SQL database MDF file gets corrupted or damaged due to various internal and external factors. When the MDF file gets corrupted, the entire SQL database becomes unavailable. In addition, you may experience various error messages when accessing the SQL database. In this article, we will see the causes for corruption in MDF file and discuss some effective ways to repair the MDF file in SQL Server and recover the data.

Possible Causes for Corruption in MDF File

Following are some common reasons that may lead to corruption in SQL database MDF file:

  • Sudden power failure or server shutdown
  • Bugs in the SQL Server
  • Faulty operating system
  • The hard drive where MDF file is stored has bad sectors
  • Unexpected (abrupt) system shutdown or crash when the database file is open
  • Hardware or software issues
  • Virus or malware infection
  • Lack of storage space on the hard drive where the file is stored

Effective Ways to Repair MDF File

Here are some effective ways to repair corrupted MDF file and recover the data.

Method 1 - Restore MDF File from Recent Backup

If your MDF file is corrupted or damaged, the first thing you can do is try to restore the file from backup, if available. Before restoring, check the integrity of the backup file using the RESTORE VERIFY ONLY command as given below:

RESTORE VERIFYONLY FROM DISK = 'backup_with_checksum.bak’

This command helps you verify if the backup file is readable, its format is valid, the backup set is complete, and CHECKSUM is present in it.

Once verified, you can run the below command to restore the backup file:

RESTORE DATABASE AdventureWorks2022 

FROM DISK = 'Z:\SQLServerBackups\AdventureWorks2022.bak' ;

RESTORE DATABASE stellardb

Method 2 – Repair MDF File using the DBCC CHECKDB Command

If the backup file is not available or is obsolete, then you can use the Database Console Command (DBCC CHECKDB) to repair the damaged or corrupted MDF file.

Note: To use the DBCC CHECKDB command, you must have administrator privileges.

First, run the DBCC CHECKDB command to check integrity issues and corruption errors in the database (MDF) file as given below:

DBCC CHECKDB Test1

If you fail to access the file, then change the database status to Emergency mode to provide read-only access to the administrator. To set the database to EMERGENCY mode, run the below command:

ALTER DATABASE Test1 SET EMERGENCY

Then, again run the DBCC CHECKDB command. If the DBCC CHECKDB command detects corruption in the database, it will recommend an appropriate repair option. You can use the repair option to repair the corrupt MDF file in SQL Server.

Before repairing, you need to set the database to single user mode. You need to have the ALTER permissions on the database to set it to single user mode. Run the following command to set the database to single user mode:

ALTER DATABASE Test 1 SET SINGLE_USER

After setting the database to single user mode, you can repair the database by using the following options:

  • REPAIR_FAST
  • REPAIR_REBUILD
  • REPAIR_ALLOW_DATA_LOSS

Let’s see how to use these options.

If you want to rebuild the MDF file quickly, then use REPAIR_FAST. It only checks the logical consistency and maintains syntax for backward compatibility. It does not help perform any repair actions. The syntax for this repair option is:

DBCC CHECKDB (‘DB Name’, REPAIR_FAST)

The REPAIR_REBUILD command helps rebuild corrupt pages or repair missing rows in non-clustered indexes. However, this command does not repair database that includes FILESTREAM data. Here’s the command to repair the database using the REPAIR_REBUILD option:

DBCC CHECKDB (‘DB Name’, REPAIR_REBUILD)

If the above repair options fail to repair the database file, then you need to use the REPAIR_ALLOW_DATA_LOSS option to repair the database. However, this command may result in data loss. So, you should use this repair option as the last resort. Here’s the command to repair the database using the REPAIR_ALLOW_DATA_LOSS option:

DBCC CHECKDB (‘DB Name’, REPAIR_ALLOW_DATA_LOSS)

Method 3 - Repair MDF File using a Third-Party SQL Database Repair Software

If the above methods fail to repair the damaged or corrupted MDF file, then the best solution is to use a trustworthy SQL database MDF file repair software, like Stellar Repair for MS SQL. It is an advanced software to repair corrupt SQL database (MDF/NDF) files without any data loss. It can even fix severe corruption issues within the MDF and NDF files. It can recover all the objects from the corrupt MDF/NDF file with complete integrity. The software supports SQL 2022, 2019, 2017, 2016, and earlier versions.

Here are some remarkable features of this software:

  • Repairs corrupt SQL database (both MDF and NDF) files
  • Recovers tables, triggers, indexes, keys, rules, and other objects
  • Repairs corrupt partition tables in the SQL database
  • Recovers deleted records from corrupted SQL database tables
  • Compatible with Windows and Linux operating systems
  • Allows searching and recovering specific database objects
  • Facilitates saving repaired file in MS SQL (MDF), HTML, XLS, and CSV formats
  • Supports ROW and PAGE compressed data recovery
  • Recovers all the data in original format
  • Interactive and easy to follow user interface

Follow the steps below to repair the MDF file using the software:

  • Download, install, and launch the Stellar Repair for MS SQL software.
  • In the Select Database window, click Browse to select the MDF file (You can use the Find option if you do not know where your data file is located).
click on browse to select the database to repair
  • Next, click on the Repair button.
  • In the Repair Complete window, click OK.
repair complete window
  • Once repaired, the software shows a preview of the repaired file.
  • You can select the objects you want to save. Then, click the Save button. You can save the repaired data to a live database, in a new database, or other formats, like CSV, HTML, and Excel.
select the saving option to save repaired database
  • Select the saving option (for example, New Database) and then click Next.
  • Under Connect To Server, fill in the credentials, select the location, and click Next.
  • In Save Mode window, select the appropriate saving mode and click Save.
  • On the Save Complete window, click on OK.

Conclusion

In case of MDF file corruption, backup helps in restoring the data. But if the backup is not available, then you can use the DBCC CHECKDB command to repair the MDF file in the SQL Server. Above, we’ve discussed the stepwise instructions to use the DBCC CHECKDB command. If the DBCC CHECKDB command fails to provide the desired results, then use Stellar Repair for MS SQL software. It is a professional repair software to repair corrupt SQL database (both MDF and NDF) files without any data loss.



Was this article helpful?
FAQs
This is simply a precautionary measure. Running repair process on a database copy helps mitigate the risks of any accidental damage to the file. The software by itself is equipped to prevent any modifications to your database. However, if something goes wrong (power spike, system glitch), you may run the risk of damaging the database structure or the contents of your database file.
Such a situation is very rare and can only happen in case the file is severely corrupt and is beyond recovery by any application. In such a case, you need to send it to us so that our professionals can analyze and repair the file. You can reach us via email at support@stellarinfo.com
It seems that your SQL database is damaged. You need to take quick action before the database becomes totally corrupt. Stop the database immediately and look for a reliable database repair software like Stellar Repair for MS SQL. Use the software to fix issues within the database's MDF file and save all your critical data.
Indeed it can! This mdf file repair software is capable of fixing a number of SQL Server database corruption errors such as 5171, 8942, 3414, not a primary database file, header file corruption, database in suspect mode, clustered or non-clustered index corruption, consistency error, schema corruption and much more.
Stellar Repair for MS SQL performs deleted records recovery while performing MDF file repair function. With this tool you can restore all your deleted records without any modifications in the original hierarchy and save them in a newly created table.
Yes, Stellar Repair for MS SQL can repair MDF file and all NDF files associated with it. Keep all the associated NDF files at the location of corrupt MDF file.
About The Author
author image
Charanjeet Kaur linkdin Icon

Technical writer with over 7 years of experience

Table of Contents

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