What are the files that make up a SQL Server Database?
This post is part of our series of SQL Server DBA fundamentals posts. These are intended for people who are new to database administration or find themselves in a position of accidental DBA where managing SQL Server is not their primary responsibility but something they do along side their other job. From my experience, IT Managers with smaller teams seem to pick up this task.
So if this describes, you might be wondering of the anatomy of your SQL Server databases, and this post will attempt to help you understand the physical structure of your database
SQL Server database files are the physical files that store data and metadata for a SQL Server database. There are two main types of database files in SQL Server:
- Data files: Data files contain the actual data for the database. Each database can have one or more data files and are organised into file groups. The first or primary data file traditionally has theย “.mdf” file extension and gets this by default. Secondary data files traditionally get the .ndf extension.
- Transaction Log files: The Transaction Log files contain information about all changes/transactions made to the database and are used to recover the database in case of failure. Each database has one log file with a “.ldf” file extension by default.
What other file types will my SQL Server database work with
There are several other files that you might encounter when working with a SQL Server database such as
- Backup files. We will cover database backups in more detail in a later post. These will either back up your database or your database’s transaction log. Just consider them database backups for now
- Database snapshots allow you to maintain a read-only snapshot of your databases at a given time.
There will be others I have missed, but these are the main ones, and we will focus on the DATA files and the Transaction Log Files.
What is the SQL Server Data Files
In SQL Server, a data file is a physical file that contains the user data and objects for a specific database. When a new database is created, SQL Server automatically creates a primary data file that has a “.mdf” file extension by default. Additional data files can be added to the database to increase storage capacity and split the workload across different disks and mount points.
A data file is organised into a series of 8 KB pages, the basic data storage unit in SQL Server. Pages are allocated to database objects such as tables, indexes, and stored procedures, and each page can contain a certain amount of data depending on the object it belongs to.
Data files can be created on different types of storage devices, including direct-attached storage (DAS), network-attached storage (NAS), and storage area networks (SAN). The choice of storage device can affect the performance of the database, so it’s important to choose the appropriate type of storage based on the workload and performance requirements.
SQL Server manages data files using the buffer pool, a memory area that caches frequently accessed data pages. When a page is requested by a user or application, SQL Server first checks if the page is already in the buffer pool. If the page is not in the buffer pool, SQL Server reads it from disk into the buffer pool, which can be accessed much more quickly in memory.
To manage data files in SQL Server, you can use SQL Server Management Studio or Transact-SQL commands to perform tasks such as creating new data files, changing the size of data files, or moving data files to a different location. Monitoring the size and usage of data files regularly is important to ensure the database has enough storage capacity and prevent performance issues.
What is the SQL Server Transaction Log
In SQL Server, the transaction log is a critical component that records all transactions made to a database. Every transaction that modifies data in the database is written to the transaction log, including inserts, updates, and deletes. The transaction log ensures data consistency and supports database recovery in case of failure.
The transaction log is stored in a separate physical file with a “.ldf” file extension and is organised into a series of virtual log files (VLFs) each 512 KB in size by default. The transaction log is circular in nature, meaning that once the log reaches the end, SQL Server overwrites the oldest inactive VLF with new transactions.
The transaction log has two main functions:
- Recovery: The transaction log is used to recover a database to a consistent state after a failure. During recovery, SQL Server reads the transaction log and applies changes to the database that were not yet written to disk at the time of the failure.
- Rollback: The transaction log can be used to roll back a transaction that has not yet been committed. If a transaction is rolled back, SQL Server undoes all the changes made by the transaction by reading the transaction log.
The transaction log can also be used for other purposes, such as database replication, high availability, and backup and restore. For example, the transaction log can be used to maintain a copy of the database at a remote location for disaster recovery purposes. This sits under different technology names, but its the transaction log that allows this to happen.
To manage the transaction log in SQL Server, you can use SQL Server Management Studio or Transact-SQL commands to perform tasks such as backing up the transaction log and shrinking the log file (Which you will hopefully not need to do regularly). It’s important to monitor the transaction log regularly to ensure enough space for transactions and to prevent performance issues related to the transaction log becoming full.
Summary
In this post, we have tried to give you an idea of what files are used in a SQL Server Database and what they are used for. Hopefully, highlight the important role each plays in keeping your databases available for your applications and users to utilise. If you lack a full-time DBA and need help managing your SQL Server, please contact us.
Useful Links
you might find the following links useful
Introduction to the Microsoft Data Platform – Data Platform Roles

0 Comments