How to Restore MDF and LDF Files

Written by jim campbell
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Restore MDF and LDF Files
Attach MDF and LDF files in SQL Server. (code image by Charles Taylor from

MDF files contain all SQL Server database information. The LDF file you see alongside the MDF file is an archive of the server logs. When you move your database to a new computer, the MDF and LDF files are "re-attached." "Attaching" a database means you restore and load the associated files on the database server. The SQL Server attaches MDF and LDF files using the "sp_attach_db" stored procedure.

Skill level:

Other People Are Reading


  1. 1

    Click the Windows "Start" button and select "All Programs." Click your "SQL Server" program group and click the "SQL Server Management Studio" shortcut. This opens the Management Studio console application.

  2. 2

    Click the SQL Server name on the left side of your console. In the main toolbar, click "New Query." This opens a query window where you execute the "sp_attach_db" stored procedure.

  3. 3

    Type the following command into your query console:

    sp_attach_db 'Database_Name','c:\backupDB.mdf','C:\backupLogs.ldf'

    The first parameter is the "Database_Name." This is the name of the database that registers with SQL Server. This can be the original name of the backed up database, or you can give it a new name. The second parameter is the location and file name for the MDF file. The third is the location for the LDF file. You can restore from the local hard drive, an external drive or a shared network location.

  4. 4

    Press the "F5" key. This executes the SQL statement and restores the database along with the log file.

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.