Search
  • For Individuals
      « Back
    • Windows Data Recovery

      Recovers lost or deleted Office documents, emails, presentations & multimedia files.

      Free Standard Professional Premium

    • Mac Data Recovery

      Recovers deleted files, photos, videos etc. on Mac.

      Free Standard Professional Premium

    • Photo Recovery

      Recover photos, videos, & audio files from all cameras and storage on Windows or Mac.

      Free Standard Professional Premium

    • Video Repair
    • Photo Repair
    • iPhone Data Recovery
  • For Business
      « Back
    • Email Repair & Converter

      Repair for Exchange Converter for EDB Converter for OST Converter for NSF Converter for OST MBOX Repair for Outlook

    • Database & File Repair

      Repair for MS SQL Repair for Access Repair for QuickBooks Software Repair for Excel Extractor for Windows Backup Repair for MySQL

    • Data Recovery & Erasure

      Data Recovery Professional Data Recovery Technician Mac Recovery for Technician Virtual Machine Recovery File Erasure Software Mobile Erasure Drive Erasure File Eraser Software File Eraser Software for Mac

    • Toolkit

      Exchange Toolkit Outlook Toolkit File Repair Toolkit MS SQL Toolkit Data Recovery Toolkit

    • Forensic

      Email Forensic Exchange Auditor Log Analyzer for MySQL Log Analyzer for MS SQL

  • Store
  • Partners
  • Services
  • Offers
  • Support

 

  • For Individuals
    DIY software for anyone who works with data.

    Windows Data Recovery Recovers lost or deleted Office documents, emails, presentations & multimedia files

    Free Standard Professional Premium

    Mac Data Recovery Especially for Mac users to recover deleted documents and multimedia files from macOS

    Free Standard Professional Premium

    Video Repair Windows Mac Repair multiple corrupt videos in one go. Supports MP4, MOV & other formats.

    StandardPremium

    Photo Recovery Windows Mac Recover photos, videos, & audio files from all cameras and storage on Windows or Mac.

    Standard Professional Premium

    iPhone Data Recovery Windows Mac Recover deleted photos, videos, contacts, messages etc. directly from iPhone & iPad

    Recover Erase Toolkit

    Photo Repair Windows Mac Repair multiple corrupt photos in one go. Supports JPEG & other formats.

    Standard Professional Premium


  • For Business
    • Email Repair
    • Email Converter
    • File Repair
    • Data Recovery & Erasure
    • Toolkit
    • Forensic

    Exchange Repair Repair corrupt EDB file & export mailboxes to Live Exchange or Office 365

    Outlook PST Repair Repair corrupt PST & recover all mailbox items including deleted emails & contacts

    OLM Repair Repair Outlook for Mac (OLM) 2011 & 2016 backup files & recover all mailbox items

    Exchange Toolkit Repair EDB & Exchange backup file to restore mailboxes, convert OST to PST, & convert EDB to PST

    Active Directory Repair Repair corrupt Active Directory database (Ntds.dit file) & extract all objects in original form

    EDB to PST Convert online & offline EDB file & extract all mailbox items including Public Folders in PST

    OST to PST Convert inaccessible OST file & extract all mailbox items including deleted emails in PST

    NSF to PST Convert IBM Notes NSF file & export all mailbox items including emails & attachments to PST

    MBOX to PST Convert MBOX file of Thunderbird, Entourage & other clients, & export mailbox data to PST

    OLM to PST Convert Outlook for Mac Data File (OLM) & export all mailbox data to PST in original form

    GroupWise to PST Convert GroupWise mail & export all mailbox items - emails, attachments, etc. - to PST

    EML to PST Convert Windows Live Mail (EML) file & export mailbox data - emails, attachments, etc. - to PST

    Office 365 to PST Connect to Office 365 account & export mailbox data to PST and various other formats

    DBX to PST Convert Outlook Express (DBX) file & export all mailbox data - emails, attachments, etc. - to PST

    SQL Repair Repair corrupt .mdf & .ndf files and recover all database components in original form

    Access Repair Repair corrupt .ACCDB and .MDB files & recover all records & objects in a new database

    QuickBooks Repair Repair corrupt QuickBooks® data file & recover all file components in original form

    MySQL Repair Repair MyISAM & InnoDB tables and recover all objects - keys, views, tables, triggers, etc.

    Excel Repair Repair corrupt Excel (.XLS & .XLSX) files and recover tables, charts, chart sheet, etc.

    BKF Repair Repair corrupt backup (BKF, ZIP, VHDX and .FD) files and restore complete data

    Database Converter Interconvert MS SQL, MySQL, SQLite, and SQL Anywhere database files

    PowerPoint Repair Repair corrupt PPT files and restore tables, header, footer, & charts, etc. like new

    File Repair Toolkit Repair corrupt Excel, PowerPoint, Word & PDF files & restore data to original form

    Data Recovery Windows Mac Recover lost or deleted data from HDD, SSD, external USB drive, RAID & more.

    Technician Toolkit

    Tape Data Recovery Retrives data from all types and capacities of tape drives including LTO 1, LTO 2, LTO 3, & others.

    Virtual Machine Recovery Recover documents, multimedia files, and database files from any virtual machine

    File Erasure Permanently wipe files and folders, and erase traces of apps and Internet activity.

    Standard Corporate

    Mobile Erasure Certified and permanent data erasure software for iPhones, iPads, & Android devices

    Drive Erasure Certified and permanent data erasure software for HDD, SSD, & other storage media Windows Mac

    Exchange Toolkit 5-in-1 software toolkit to recover Exchange database, convert EDB to PST, convert OST to PST, restore Exchange backup, and reset Windows Server password.

    Outlook Toolkit Comprehensive software suite to repair PST files, merge PST files, eliminate duplicate emails, compact PST files, and recover lost or forgotten Outlook passwords.

    File Repair Toolkit Powerful file repair utility to fix corrupt Word, PowerPoint, and Excel documents created in Microsoft Office. It also repairs corrupt PDF files and recovers all objects.

    MS SQL Toolkit 5-in-1 software toolkit to repair corrupt SQL database, restore database from corrupt backup, reset database password, analyze SQL logs, & interconvert databases.

    Data Recovery Toolkit Software helps to recovers deleted data from Windows, Mac and Linux storage devices. Also supports recovery from RAIDs & Virtual Drives.

    Email Forensic Advanced email forensic solution for cyber experts to audit, analyze, or investigate emails & gather evidences.

    Exchange Auditor Exchange Server monitoring solution to automate audits, scans and generate reports ìn real-time.

    Log Analyzer for MySQL Analyze forensic details of MySQL server database log files such as Redo, General Query, and Binary Log.

    Log Analyzer for MS SQL Track & analyze MS SQL Server database transactions log files.

    Email Forensic
    STELLAR EMAIL FORENSIC

    Advanced email forensic tool to analyze and collect the mailbox data of email clients

    Learn More arrow


    All Products arrow

    All Products arrow

    All Products arrow

    All Products arrow

  • Our Partners
  • Lab Services
  • microphone-icon-android

    Trending Searches

    Data Recovery

    Photo Recovery

    Video Repair

    iPhone Data Recovery

    File Erasure Software

    Exchange Repair

    OST to PST

    PST Repair

    Raid Recovery

    MS SQL Repair

  • English Deutsch Français Nederlands Italiano Español 日本語 简体中文
  • Support
Database Recovery 4 minute read

How to Restore Database from MDF File?

Updated on June 21st, 2021
Charanjeet Kaur
Written By
Charanjeet Kaur
Kuljeet Singh
Approved By
Kuljeet Singh

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.

How to Restore Database from MDF File?

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

Note: 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.

Contents

  • Methods to Restore Database from MDF File in SQL Server
  • Method 1 – Attach SQL Server Database using MDF File
  • Method 2 – Use SQL Repair Tool to Restore Database
  • Conclusion

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.

About The Author

Charanjeet

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.

Best Selling Products

Stellar Repair for MS SQL

Stellar Repair for MS SQL

Stellar Repair for MS SQL is an enterpri

Read More
Stellar Toolkit for MS SQL

Stellar Toolkit for MS SQL

3-in-1 software package, recommended by

Read More
Stellar Converter for Database

Stellar Converter for Database

Stellar Converter for Database is an eff

Read More
Stellar Repair for Access

Stellar Repair for Access

Powerful tool, widely trusted by users &

Read More

Leave a comment Cancel reply

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

14  +    =  16

Table of Contents    arrow

  1. Methods to Restore Database from MDF File in SQL Server
  2. Method 1 – Attach SQL Server Database using MDF File
  3. Method 2 – Use SQL Repair Tool to Restore Database
  4. Conclusion

Categories

Related Posts

related post
Database Recovery

How to Fix Error “The database cannot be recovered because the log was not restored”?

Stellar Author Charanjeet Kaur April 15, 2022 Read More
related post
Database Recovery

How to Fix System Database Corruption in SQL Server?

Stellar Author Charanjeet Kaur April 8, 2022 Read More
related post
Database Recovery

How to Get Data from a Corrupt SQL Table?

Stellar Author Charanjeet Kaur March 4, 2022 Read More

Stellar Official Website

Stellar Data Recovery Inc.
48 Bridge Street Metuchen,
New Jersey 08840,
United States

ALSO AVAILABLE AT

Partner Logo

About

  • About us
  • Career
  • ISMS Policy
  • Privacy Policy
  • Terms of Use
  • License Policy
  • Refund Policy
  • End User License Agreement

RESOURCES

  • Blog
  • Articles
  • Product Videos
  • Knowledge Base
  • Case Studies
  • Whitepapers
  • Software Catalog

NEWS & EVENTS

  • News
  • Events

PARTNERS

  • Affiliates
  • Resellers
  • Distributors

Useful Links

  • Contact Us
  • Support
  • Special Offers
  • Student Discounts
  • Awards & Reviews
  • Downloads
  • Store
  • Sitemap
Follow Us

tw in yt

Stellar & Stellar Data Recovery are Registered Trademarks of Stellar Information Technology Pvt. Ltd. © Copyright 2022 Stellar Information Technology Pvt. Ltd. All Trademarks Acknowledged.

Hippa Logo tuv footer partner logo DMCA.com Protection Status
We use cookies on this website. By using this site, you agree that we may store and access cookies on your device Read More Got it!