๐ What is an MDF File?
An MDF file is the primary data file of a Microsoft SQL Server database. It is the main file where SQL Server keeps the real contents of a database, and it is often read as the Master Database File or Main Database File.
Every SQL Server database has at least one MDF file. It stores your tables and their rows, plus indexes, views, stored procedures and the startup information that points to the other files the database uses. The file is binary and page-structured, so you cannot read it in a text editor. To open it you attach it in SQL Server or use a dedicated MDF viewer.
Database Objects
- Tables, rows and columns
- Indexes and keys
- Views and stored procedures
- Functions and triggers
System Information
- File header and boot page
- Allocation maps for free space
- Pointers to secondary and log files
- Internal database version number
โก Quick Facts
| File Extension | .mdf |
| Full Name | Primary Data File (commonly called Master Database File) |
| Category | Database File |
| Developed By | Microsoft Corporation |
| Used By | Microsoft SQL Server |
| File Type | Binary (proprietary, page-based) |
| Page Size | 8 KB (8,192 bytes) |
| Extent Size | 64 KB (8 pages) |
| Companion Files | .ndf (secondary data), .ldf (transaction log) |
| Opens With | SQL Server (SSMS); MDF viewers for read-only access |
| Max Data File Size | 16 TB per file (10 GB per database on Express) |
๐ฆ What an MDF File Stores
An MDF file is not a plain container of records. It stores everything SQL Server needs to run the database, organised into fixed-size pages. Here is what lives inside.
All table rows and column values
Structures that speed up queries
Stored procedures, functions, triggers
Saved queries and object grouping
Track which pages are used or free
File header and database version
โ๏ธ MDF vs NDF vs LDF
A SQL Server database is made of up to three kinds of files. Knowing the difference helps you move, back up and open a database correctly.
.mdf | .ndf | .ldf | |
|---|---|---|---|
| Name | Primary data file | Secondary data file | Transaction log file |
| Required | Yes, exactly one | No, optional | Yes, at least one |
| Holds | Data plus startup info | Extra data only | A record of every change |
| Uses 8 KB pages | Yes | Yes | No, uses log records |
| Purpose | Main storage | Spread data across files or drives | Recovery and consistency |
๐งฉ MDF File Structure
SQL Server stores everything in an MDF file on pages. A page is the smallest unit the engine reads or writes. Understanding pages explains how the file is laid out.
Page
The basic storage unit, numbered from 0 to n
Byte header
Page ID, type, record count and page pointers
Extent
A group of 8 physically neighbouring pages
Each 8 KB page begins with a 96-byte header, which leaves about 8,060 bytes for row data. Pages are grouped into extents of 8 pages. The first pages of the file hold system information such as the file header and allocation maps, then the data and index pages follow.
Special system pages keep the file organised. The file header page (page 0) describes the file itself. Allocation maps such as PFS, GAM and SGAM track which pages and extents are free or in use. The transaction log (.ldf) works differently and does not use pages at all, it stores a stream of log records instead.
๐ Versions & Compatibility
Every MDF file stores an internal database version number that ties it to the SQL Server release that created or last upgraded it. This drives one important rule.
The modern 8 KB page format has been used since SQL Server 7.0 (1998). Earlier versions used 2 KB pages. Each release since then keeps the page format but raises the internal version and adds features. Databases also carry a compatibility level that controls query behaviour.
| SQL Server Release | Year | Compatibility Level |
|---|---|---|
| SQL Server 2008 / 2008 R2 | 2008 / 2010 | 100 |
| SQL Server 2012 | 2012 | 110 |
| SQL Server 2014 | 2014 | 120 |
| SQL Server 2016 | 2016 | 130 |
| SQL Server 2017 | 2017 | 140 |
| SQL Server 2019 | 2019 | 150 |
| SQL Server 2022 | 2022 | 160 |
๐ Size Limits
Maximum data file size
16 TB
A single .mdf or .ndf data file can grow up to 16 terabytes.
SQL Server Express limit
10 GB
The free Express edition caps each database at 10 GB.
๐ How to Open an MDF File
There are two paths. Use SQL Server when you want to run the database, or an MDF viewer when you only need to read the data.
Attach in SQL Server Management Studio
- Open SQL Server Management Studio and connect to your instance.
- Right-click the Databases node and choose Attach.
- Click Add, then select your
.mdffile. Check that the matching.ldfis listed. - Click OK. The database appears under Databases, ready to query.
Attach with T-SQL
Open Without SQL Server
If you do not have SQL Server, or the file is corrupt or orphaned, a dedicated tool can read it directly:
๐ Default MDF File Location
By default, SQL Server saves each database's .mdf and .ldf files in the DATA folder of its instance. The folder name carries an internal version number, so it changes with each SQL Server release. For a default instance the base path is C:\Program Files\Microsoft SQL Server\.
| SQL Server Release | Default DATA Folder (default instance) |
|---|---|
| SQL Server 2008 | ...\MSSQL10.MSSQLSERVER\MSSQL\DATA\ |
| SQL Server 2008 R2 | ...\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\ |
| SQL Server 2012 | ...\MSSQL11.MSSQLSERVER\MSSQL\DATA\ |
| SQL Server 2014 | ...\MSSQL12.MSSQLSERVER\MSSQL\DATA\ |
| SQL Server 2016 | ...\MSSQL13.MSSQLSERVER\MSSQL\DATA\ |
| SQL Server 2017 | ...\MSSQL14.MSSQLSERVER\MSSQL\DATA\ |
| SQL Server 2019 | ...\MSSQL15.MSSQLSERVER\MSSQL\DATA\ |
| SQL Server 2022 | ...\MSSQL16.MSSQLSERVER\MSSQL\DATA\ |
MSSQLSERVER with the instance name, such as MSSQL15.SALES. To see the real location of every file, run SELECT name, physical_name FROM sys.master_files; in SQL Server.
๐ฏ Common Uses
๐๏ธ Application databases
Websites, ERP, CRM and line-of-business apps store their data in MDF files behind SQL Server.
๐พ Backup and migration
Teams detach or back up databases and move the .mdf set to another server or environment.
๐ Attach and detach
Detaching a database frees its files so they can be copied, then re-attached elsewhere.
๐ Recovery and forensics
Analysts read orphaned or corrupt MDF files to recover records when a server is unavailable.
๐ง Troubleshooting
You are attaching a file created on a newer SQL Server. Attach it on the same or a newer version. To use it on an older server, script out the objects and data on the newer server and import them there.
Run DBCC CHECKDB to check for consistency errors and restore from a good backup when possible. To repair the file and recover the data without a server, use SQL Database Recovery. To just read what is still accessible, an MDF viewer can open many damaged files.
If the database was shut down cleanly, SQL Server can rebuild the log with CREATE DATABASE ... FOR ATTACH_REBUILD_LOG. If it was not, you may need emergency mode with repair or a dedicated SQL Database Recovery tool that reads the MDF without the log.
Error 5120 means the SQL Server service account cannot reach the files. Give that account, or its service SID, read and write permission on the .mdf and .ldf files. Running SSMS as administrator on its own does not fix this. Also make sure the database is detached or offline before you copy the files, since SQL Server locks files that are in use.
โ Frequently Asked Questions
An MDF file is the primary data file of a Microsoft SQL Server database. It holds the real data of the database, including tables, rows, columns, indexes, views, stored procedures and the pointers to any other files the database uses. The .mdf extension is the recommended name for this primary data file.
MDF is the primary data file and every database has exactly one. NDF is an optional secondary data file used to spread a database across more files or drives. LDF is the transaction log file that records every change so the database can be recovered. SQL Server does not force these extensions, but they are the standard convention.
The normal way is to attach it in Microsoft SQL Server. In SQL Server Management Studio, right-click Databases, choose Attach and add the .mdf file. You can also run CREATE DATABASE ... FOR ATTACH in T-SQL. If you do not have SQL Server, a dedicated MDF viewer can open and read the file without a server.
Yes. A third-party MDF viewer can read the tables and objects inside an .mdf file without a running SQL Server instance, which is useful for a quick look or for corrupt and orphaned files. To attach the database and run queries you still need SQL Server, or you can convert the data to SQL scripts or CSV first.
No. Each MDF file stores an internal database version number. A database can be attached or restored to the same version or a newer version of SQL Server, but never to an older one. To move data to an older server you must script out the objects and data on the newer server and import them, rather than attaching the file.
Usually yes. The transaction log (.ldf) is part of the database and SQL Server expects it when you attach. If the log is missing, SQL Server can sometimes rebuild it using CREATE DATABASE ... FOR ATTACH_REBUILD_LOG, but this only works when the database was shut down cleanly. Keep the matching .ldf with the .mdf whenever you move a database.
A single SQL Server data file can grow up to 16 TB, so an .mdf file can be very large. The SQL Server Express edition is different: it limits each database to 10 GB. For performance, many teams keep the .mdf and .ldf on separate drives and set sensible autogrowth values.
No. The .mdf extension is used by two unrelated formats. This guide covers the Microsoft SQL Server primary database file. The other .mdf is a Media Descriptor File created by disc tools like Alcohol 120% and DAEMON Tools to store a CD or DVD image, usually paired with an .mds file. They share an extension but are completely different files.
๐ ๏ธ Related Tools
SQL Database Recovery
Repair corrupt or suspect MDF files and recover deleted records and dropped tables.
Recover Data โ๐ Summary: Key Points About MDF Files
- MDF is the primary data file of a SQL Server database
- Stores tables, indexes, views and procedures
- Built from 8 KB pages grouped into 64 KB extents
- Pairs with NDF (secondary) and LDF (log) files
- A single data file can reach 16 TB
- Open by attaching it in SQL Server, or read it with a viewer
- Attach only to the same or a newer SQL Server version
- Keep the matching LDF with the MDF
- Express edition limits a database to 10 GB
- The disc-image .mdf is a different, unrelated format