Quick Answer
MDF, NDF and LDF files are the three file types of a SQL Server database. MDF is the primary data file and every database has exactly one. NDF files are optional secondary data files. LDF is the transaction log and every database needs at least one. Data files hold tables, indexes, stored procedures and views while the log holds the information for recovery.
Back up all of them together with BACKUP DATABASE and copy the files to a safe folder before any repair.
You open the data folder of a SQL Server instance and see files with endings you do not know. Which one holds your tables and which one can you leave alone?
Answers follow here with the place of every file so that you can plan space, moves and repairs with confidence.
What Are MDF, NDF and LDF Files in SQL Server?
SQL Server keeps every database in operating system files. MDF, NDF and LDF files are the three types you meet when you open the data folder of an instance. Tables, indexes, stored procedures and views sit in the data files. Recovery information for every transaction sits in the log file.
Every database has at least two files, one data file and one log file. Microsoft Learn lists .mdf, .ndf and .ldf as the recommended extensions for the primary data file, the secondary data files and the transaction log. Extensions are recommendations only. Check the physical file name when a file carries a different ending.
Knowing the role of MDF, NDF and LDF files helps in three cases. You move a database to another server. You attach a file that a colleague sent. You see an error that names one file and must decide where to look first.
Next sections cover what the three file types hold, how filegroups tie them together and how pages organize data inside. You will also see how to list the files of a database, how growth works and what to do when a file is damaged.
What Does Every File Type Hold?
Three file types do different work. Losing one of them can stop a database from opening. Treat all three as a set.
| File | Extension | What it holds | How many |
|---|---|---|---|
| Primary data file | .mdf | Startup information for the database and a pointer to the other files, plus data | Exactly one for every database |
| Secondary data file | .ndf | Optional user data that you can spread over other drives | Zero or more |
| Transaction log file | .ldf | Information that SQL Server needs to recover the database | At least one |
MDF is the starting point. SQL Server opens it first and learns from it where the other files of the database live. Databases with only one data file keep all their tables and indexes in the MDF file.
NDF files exist only when someone adds them. Admins add NDF files to place data on other disks or to split a large database across several drives. Most databases work well with one data file and one log file. Missing NDF files are normal and not a fault.
LDF records changes so that SQL Server can recover the database after a failure and keep transactions consistent. Tables do not live in it. Without its log file a database cannot open in the normal way. LDF files matter as much as the MDF file.
Every file has two names. Logical names are the names that Transact SQL uses to point at the file. Operating system file names are the physical names with the folder path. Logical names must follow the rules for SQL Server identifiers and stay unique inside the database. Physical names follow the rules of the operating system.
How Do Filegroups Tie the Files Together?
Filegroups are named sets of data files that you manage as one unit. They let you place objects on chosen disks and manage allocation and backup in parts. Log files stay outside this system.
- Primary filegroup. Contains the primary data file and every secondary file that you did not place elsewhere. System objects stay here.
- User defined filegroups. You create them to group files for administration, allocation and placement.
- Default filegroup. New objects without a named filegroup go to one default filegroup at a time. PRIMARY holds that role until you change it.
- Special types. Memory optimized data and Filestream filegroups serve those features and have their own rules.
Four rules keep the layout clear. Files belong to only one filegroup. Files and filegroups serve only one database. Transaction log files never join a filegroup. When a filegroup holds several files, SQL Server spreads writes across them in proportion to the free space in every file.
Microsoft Learn also gives a practical tip for databases with several data files. Create a second filegroup for the extra files and make it the default. Primary files then hold only system objects. Your MDF file stays small and focused that way.
How Do Pages and Extents Organize Data Inside the Files?
Inside a data file SQL Server divides storage into pages. Pages are 8 KB and form the basic unit of input and output. Every page starts with a 96 byte header. Eight pages form an extent. Objects can take an entire extent or share one with up to seven other objects. You can find a summary of this layout in the Wikipedia entry for Microsoft SQL Server.
Page numbers start at 0 in every file. Every file also has a unique file ID. SQL Server finds a page by file ID and page number together. Header pages and some system pages sit at the start of a file. Primary data files and first log files also hold a database boot page.
Damage to a single page can hurt only part of a table. Damage to a header or boot page can stop the entire database from opening. Admins speak of page level errors when they describe corruption in MDF and NDF files.
Log files have their own structure and do not use data pages in this way. Think of them as a sequential record of changes that SQL Server writes as work happens.
How to List the Files of a SQL Server Database?
Two ways show the files of a database. Both give the logical name, the physical path and the file type.
Use SQL Server Management Studio
- Connect to the instance in SQL Server Management Studio.
- Right click the database in Object Explorer and select Properties.
- Open the Files page and check the logical name, file type, filegroup, initial size, autogrowth and path of every file.
Use a query
Catalog view sys.master_files lists the files of all databases on the instance. View sys.database_files lists the files of the current database. Use the next query to list the files of every database.
SELECT DB_NAME(database_id) AS database_name, name AS logical_name, physical_name, type_desc, size * 8 / 1024 AS size_mb FROM sys.master_files ORDER BY database_id, file_id;
Column type_desc shows ROWS for data files and LOG for log files. Column size counts 8 KB pages, which is why the query multiplies it to get megabytes.
Write down the physical paths before you move or copy anything. Path lists save time when you attach a database on another server or tell a support engineer where the files sit.
How Do File Size, Growth and Disk Placement Work?
Files grow on their own from the original size by a set increment. You can also set a maximum size for a file. Files without a maximum grow until the disk is full. When a filegroup holds several files, none of them grows until all are full.
Disk placement matters for speed and safety. By default SQL Server puts data and log files on the same drive and path. For production work Microsoft Learn recommends separate disks and says that log files should not share a physical disk with other files and filegroups. Separate log disks also help recovery because one failed disk then does not take both sets of files.
File systems matter too. Data and log files can sit on FAT or NTFS volumes and Microsoft recommends NTFS on Windows. NTFS compressed volumes do not support data filegroups that accept writes or log files. Use data compression inside SQL Server when you want to save space.
- Watch free space on every drive that holds MDF, NDF and LDF files.
- Set growth in fixed sizes and not in tiny steps that fire again and again.
- Keep an eye on the log file because it can grow fast during busy periods.
What Happens When One of the Files Is Damaged or Missing?
Damaged or missing files show up as databases that will not attach, will not open or report errors on some tables. Your first move is to stop and protect what you still have. Do not overwrite files. Do not repeat repairs on your only copy. Do not delete the log file to force a start.
- Copy the MDF, NDF and LDF files to a separate folder before you try anything.
- Find your last good backup and check that it restores on a test server.
- Restore the backup when it is recent enough for your needs.
- Use a recovery tool on the copies when no usable backup exists or the backup misses recent data.
MDF Viewer Pro opens an MDF file and adds NDF files to the same session. Quick Scan and Advanced Scan modes come with the tool. Deleted records show in red and you can preview tables, views, triggers and stored procedures. Scanning and previewing need no SQL Server setup and only the export step does. Free demo exports stop at 25 records per table, which is enough to test your own file before you buy.


SQL Recovery for MDF and NDF repair serves the case where damage goes deeper. SQL Recovery repairs corrupt MDF and NDF files, previews the recovered tables and exports to SQL Server, scripts or CSV.
Work on copies of your files with either tool, which keeps the originals safe. Compare the preview with what you expect before you export anything.
Which File Should You Check First?
| Your problem | File to check | First step |
|---|---|---|
| Database will not attach | MDF and LDF together | Confirm both files belong to the same database and copy them to a safe folder |
| Disk is full | LDF and growing data files | Check log growth and the free space of the drive that holds the files |
| Errors on tables in one filegroup | NDF files of that filegroup | Find which files belong to the filegroup with the query from this guide |
| Corruption reported on pages | MDF and NDF files | Restore the latest good backup on a test server first |
| No backup and damaged data files | Copies of MDF and NDF files | Open the copies in a recovery tool and preview the tables |
Use this table as a quick map when an alert arrives. Rows point you to the file that deserves attention first.
Picture a team that sees an attach error after a server move. Admins in that team check the paths with the query and find that the LDF file stayed on the old server. They copy it over and the database attaches. Nobody needs a repair.
Author's Verdict
MDF holds the start of the database and your data. NDF files add optional data space. LDF protects recovery. Learn where all three sit, keep them together in backups and watch their growth. Copy the files before any repair and test a restore before you need it. When no backup helps, try MDF Viewer Pro on a copy and judge the preview before you pay.
FAQs on MDF, NDF and LDF Files
What Are MDF, NDF and LDF Files in SQL Server?
MDF is the primary data file with the startup information and your data. NDF files are optional secondary data files. LDF is the transaction log that holds the information for recovery.
Can a SQL Server Database Work Without an NDF File?
Yes. NDF files are optional. Most databases work well with one data file and one log file.
Can a Database Have More Than One LDF File?
Yes. Every database needs at least one log file and can have more. Microsoft documents an example with four log files.
Where Does SQL Server Keep These Files by Default?
Data and log files go on the same drive and path by default. Microsoft recommends separate disks for production work.
Can I Open an MDF File Without Installing SQL Server?
MDF Viewer Pro scans and previews an MDF file with no SQL Server setup. Only the export step needs SQL Server. Supported files come from SQL Server 2022 down to 2000.
Is It Safe to Copy an MDF File While the Database Is Online?
No. SQL Server keeps its files in use while the database is online, which makes a plain copy unreliable. Use BACKUP DATABASE or take the database offline first.
What Is the Difference Between a Logical Name and a File Name?
Logical names are what Transact SQL uses for the file and they must be unique in the database. File names are the physical names with the folder path in the operating system.