Knowledge is no fun, unless you share it!!! :)

Friday, 26 July 2013

MDF, NDF and LDF files in SQL-SERVER

If you have experience with SQL Server, you may have heard the terms MDF, NDF and LDF already. Those are the commonly used file name extensions in SQL Server for specify the Primary Data files, Secondary data files and Log files respectively.

In SQL Server, data and log information are never stored in the same file. Furthermore, those individual files (primary data, secondary data and log) are used by only one server. However, below is a brief description about the three file types in SQL Server.

Primary data files (MDF):
Primary data file is the starting point of the database. It points to the other files in the database. Therefore, every database has one primary data file. Also, all the data in the database objects (tables, stored procedures, views, triggers.. etc.) are stored in the primary data files. The recommended and the most common file name extension for primary data files is .mdf.

Secondary data files (NDF):
You can only have one primary data file for a database. Rest made up by secondary data files. But its not necessary to have a secondary data file. Therefore some databases may not have any secondary data file. But its also possible to have multiple secondary data files for a single database. .ndf is usually recommended to denote secondary data files. It’s also possible to store the secondary data file in a separate physical drive than the one which primary data file is stored.

Log files ( LDF ):
Log files in SQL Server databases hold all the log information. Those information can be later used to recover the database. Size of the log file is determined by the logging level you have set up on the database.  There must be at least one log file for each database. But it is also possible to have more than one log file for a single database. The recommended file name extension for log files is .ldf.

Locations of all the files in a database (primary, secondary and log files) are stored in the primary file of the database and in the master database. When the SQL Server Database Engine want to use the file location information of those  files, it retrieves the data from the master database.