SlideShare a Scribd company logo
BRIEF INSTRUCTION ABOUT MDF,LDF
By :
MD MASUM REZA
AGENDA OF THE DAY
 Introduction
 What is MDF , LDF and NDF
 Locating .mdf File .ldf file in server
 Role of MDF and LDF
 Restore data after truncating the
table(with example)
 Differential Backup
 Full Backup
 Conclusion
Introduction :-
The SQL Server provides the facility to restore your database
by attaching your .mdf file and .ldf file to the database.
We can directly Attach our existing .mdf and .ldf file to an SQL
instance by using SQL Server Management Studio or T-SQL.
We can restore our .mdf and .ldf file through execute some query.
What is MDF , LDF and NDF : -
MDF :–Stands for Master Database File. It contains all the main information of the
database that are part of the server. This extension also points to various other files. It
plays a crucial role in information storage. Overall it is very important for safe and secure
supervision of data. In case this file gets damaged, an MDF recovery procedure is
conducted to recover it. Doing so is important in order to save the data from going
missing.
NDF :-stand for Next Data File . NDF file is a user defined secondary database file of
Microsoft SQL Server with an extension .ndf, which store user data. Moreover, when
the size of the database file growing automatically from its specified size, you can use
.ndf file for extra storage and the .ndf file could be stored on a separate disk drive.
Every NDF file uses the same filename as its corresponding MDF file. We cannot open
an .ndf file in SQL Server Without attaching its associated .mdf file.
LDF :–Stand for LOG Database File .This file stores information related to transaction logs
for main data file. It basically keeps track of what all changes have been made in the
database. The information that this file stores ranges from date/time of change, details
of the changes made, as well as information related to whoever made the changes.
Information related to computer terminals where changes took place is also stored in the
logs.
Locating .mdf File .ldf file in server :-
Within each database, you will find two files namely MDF and LDF.
These two are basically file extensions used in Microsoft SQL. These files get automatically
created at the time of database creation. They also share the same storage location. The
reason why these files are so important is because they happen to be part of backup and
recovery process. In simpler words, in case something bad happens to the database, these are
the files the administrator will resort to for restoring and recovering the lost/damaged data.
Query can Execute:
 select * from sys.database_files
Role of MDF and LDF :-
 select * from fn_dblog (null , null)
Role of MDF and LDF
The primary purpose of an LDF file is to provide the ACID concept – Atomicity, Consistency,
Isolation, and Durability
Atomicity: if one part of the transaction fails, the entire transaction fails, and the database state is
left unchanged
Consistency: any transaction brings the database from one valid state to another
Isolation: the execution of concurrent transactions brings the database to a state as if the
transactions were executed serially, one by one
Durability: once committed, the transaction remain so, even in the case of errors, power loss, or
crashes
An LDF file stores enough information to replay or undo a change, or recover the database to a
specific point in time. Therefore, due to various auditing or recovery requirements, there is often a
need to open the LDF file and view its contents. But viewing LDF file content is not an easy task
Restore data after truncating the table :-
The below example will show how can retrieve data after truncate or delete if happen :
use master
1 .create database Test
go
USE Test
GO
CREATE TABLE Student
(
StudentID BIGINT IDENTITY PRIMARY KEY,
StudentName VARCHAR(128),
RollNo VARCHAR(10)
)
GO
INSERT INTO Student(StudentName , RollNo)
VALUES
('Reza','101')
,('Hari','102')
,('Sunil','103')
,('Naveen','104')
GO
Restore data after truncating the table :-
3. Take Database Backup :
BACKUP DATABASE Test
TO DISK = 'D:BackupNew folderMyTestDB.BAK'
GO
4. Truncate table student
5. select * from Student
2. select * from Student
Restore data after truncating the table :-
6. SELECT
[Current LSN],
Operation,
[Transaction ID],
[Begin Time],
[Transaction Name]
FROM
fn_dblog (NULL, NULL) where [Transaction Name] ='TRUNCATE TABLE‘
7. 00000021:0000005d:0001 take LSN (Log Sequence Numbers) from above code.
8. Convert LSN number from HEX to Decimal number. like below code
SELECT CAST (CONVERT (VARBINARY,'0x'+'00000021', 1) AS INT) as FirstPart, --33
CAST (CONVERT (VARBINARY,'0x'+'0000005d', 1) AS INT) as SecondPart, --93
CAST (CONVERT (VARBINARY,'0x'+'0001', 1) AS INT)as ThirdPart --1
GO
Restore data after truncating the table :-
9. Add preceding zeros for 93and 1.
 Note : no need for 33
 93=0000000093 (8 zeroes) total should be 10 digits
 1 =00001 (4 zeroes) total should be 5 digits.
 => 33000000009300001
 Now do following steps one by one .
BACKUP LOG Test
TO DISK = 'D:BackupNew folderMyTestDB.TRN'
GO
RESTORE DATABASE MyTestDB_Copy
FROM DISK = 'D:BackupNew folderMyTestDB.bak'
WITH
MOVE 'Test' TO 'D:BackupNew folderMyTestDB.mdf',
MOVE 'Test_log' TO 'D:BackupNew folderMyTestDB_log.ldf',
NORECOVERY
GO
Restore data after truncating the table :-
RESTORE LOG MyTestDB_Copy
FROM
DISK = N'D:BackupNew folderMyTestDB.TRN'
WITH
STOPBEFOREMARK = 'LSN:33000000009300001’
10 . use MyTestDB_Copy
select * from Student
Differential Backup:-
A differential backup is created similarly to a full backup, but with one important difference –
the differential backup only contains the data that has changed since the last full backup (the
active portion of the transaction log). Differential backups are cumulative not incremental. This
means that a differential backup contains all changes made since the last full backup, in spite of
the fact that they have already been included in previous differential backups. Differential backups
are created the following way:
How to Make a Differential Backup
To make a differential database backup simply add “WITH DIFFERENTIAL” clause:
BACKUP DATABASE your_database TO DISK = 'diff.bak' WITH DIFFERENTIAL
Full Backup :-
The simplest kind of SQL Server backup is a full database backup. It provides a complete copy of
the database but allows restoring the database only to a point-in-time when the backup was
made.Even if you add the “WITH STOPAT=<time or log sequence number>” option to restore
command you will not get the expected result because this option applies only when you restore
the transaction log. Please see how a periodic full backup works in the picture below:
How to Make a Full Backup
To make a full backup you can use T-SQL command:
BACKUP DATABASE your_database TO DISK = 'full.bak'
Another way to backup a database is to use
SQL Server Management Studio (SSMS):
right click on the database you want to backup, select “Tasks”, then “Back up…”.
Choose “Full” backup type, add a backup destination and click “OK”.
Conclusion
In the above section, we learned how to restore LDF and MDF files to a database. It
is highly recommended that you have to detach the MDF file before attaching a new
one. This is a very helpful method to restore database using MDF and LDF file.
1. https://sqljumble.blogspot.in/2016/11/how-to-recover-data-after-truncate.html
2. https://sqlbak.com/blog/recover-deleted-data-in-sql-server/
3. https://sqlbak.com/academy
Thanking You

More Related Content

PPTX
Sql server basics
PDF
Chapter 5 Database Transaction Management
PPTX
Physical architecture of sql server
DOCX
What is difference between dbms and rdbms
PPT
Sql server basics
PPTX
Redis Introduction
PPTX
Oracle dba training
PDF
Introduction to Database Management System
Sql server basics
Chapter 5 Database Transaction Management
Physical architecture of sql server
What is difference between dbms and rdbms
Sql server basics
Redis Introduction
Oracle dba training
Introduction to Database Management System

What's hot (20)

PDF
SQL Complete Tutorial. All Topics Covered
PPTX
Mongo db basic installation
PPTX
Basic sql Commands
PPTX
Database Concepts and Terminologies
PPTX
PDF
chapter 4-Functional Dependency and Normilization.pdf
PPTX
Oracle DBA
PPTX
File system vs database
PPT
Lecture 08 distributed dbms
PPTX
An Introduction To Oracle Database
PPTX
FUNCTION DEPENDENCY AND TYPES & EXAMPLE
PPTX
12. oracle database architecture
PPTX
Sql Server Management Studio Tips and Tricks
PPTX
Functional dependency
PPT
Introduction to SQL
PDF
Mongodb
PPTX
SQL Server High Availability and Disaster Recovery
PPTX
Sql 2012 always on
PPTX
Structured Query Language (SQL)
PPTX
Introduction to triggers
SQL Complete Tutorial. All Topics Covered
Mongo db basic installation
Basic sql Commands
Database Concepts and Terminologies
chapter 4-Functional Dependency and Normilization.pdf
Oracle DBA
File system vs database
Lecture 08 distributed dbms
An Introduction To Oracle Database
FUNCTION DEPENDENCY AND TYPES & EXAMPLE
12. oracle database architecture
Sql Server Management Studio Tips and Tricks
Functional dependency
Introduction to SQL
Mongodb
SQL Server High Availability and Disaster Recovery
Sql 2012 always on
Structured Query Language (SQL)
Introduction to triggers
Ad

Similar to MDF and LDF in SQL Server (20)

DOCX
Db2 migration -_tips,_tricks,_and_pitfalls
PDF
The best ETL questions in a nut shell
PPT
database.ppt
PPTX
database backup and recovery
PDF
SQL Repair Tool.
TXT
oracle dba
PDF
Oracle tutorial
PPT
Ms sql server architecture
PPTX
hasbngclicvhhhhhjgzitxgkxhcjcjb. I u I u530.pptx
PDF
data stage-material
DOC
Steps for upgrading the database to 10g release 2
PPTX
Sql server lesson10
PPT
Recipe 14 - Build a Staging Area for an Oracle Data Warehouse (2)
PDF
Standby db creation commands
PPTX
Managing SQLserver for the reluctant DBA
PPTX
ELT Publishing Tool Overview V3_Jeff
PPTX
Sql server lesson3
DOCX
Db2 Important questions to read
TXT
Oracle11g notes
PDF
The best Teradata RDBMS introduction a quick refresher
Db2 migration -_tips,_tricks,_and_pitfalls
The best ETL questions in a nut shell
database.ppt
database backup and recovery
SQL Repair Tool.
oracle dba
Oracle tutorial
Ms sql server architecture
hasbngclicvhhhhhjgzitxgkxhcjcjb. I u I u530.pptx
data stage-material
Steps for upgrading the database to 10g release 2
Sql server lesson10
Recipe 14 - Build a Staging Area for an Oracle Data Warehouse (2)
Standby db creation commands
Managing SQLserver for the reluctant DBA
ELT Publishing Tool Overview V3_Jeff
Sql server lesson3
Db2 Important questions to read
Oracle11g notes
The best Teradata RDBMS introduction a quick refresher
Ad

Recently uploaded (20)

PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
medical staffing services at VALiNTRY
PDF
Digital Strategies for Manufacturing Companies
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PPTX
history of c programming in notes for students .pptx
PDF
System and Network Administration Chapter 2
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
System and Network Administraation Chapter 3
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
How Creative Agencies Leverage Project Management Software.pdf
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PPTX
Operating system designcfffgfgggggggvggggggggg
How to Migrate SBCGlobal Email to Yahoo Easily
medical staffing services at VALiNTRY
Digital Strategies for Manufacturing Companies
Odoo POS Development Services by CandidRoot Solutions
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
history of c programming in notes for students .pptx
System and Network Administration Chapter 2
Design an Analysis of Algorithms II-SECS-1021-03
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
System and Network Administraation Chapter 3
CHAPTER 2 - PM Management and IT Context
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
How to Choose the Right IT Partner for Your Business in Malaysia
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Internet Downloader Manager (IDM) Crack 6.42 Build 41
How Creative Agencies Leverage Project Management Software.pdf
Reimagine Home Health with the Power of Agentic AI​
2025 Textile ERP Trends: SAP, Odoo & Oracle
Design an Analysis of Algorithms I-SECS-1021-03
Operating system designcfffgfgggggggvggggggggg

MDF and LDF in SQL Server

  • 1. BRIEF INSTRUCTION ABOUT MDF,LDF By : MD MASUM REZA
  • 2. AGENDA OF THE DAY  Introduction  What is MDF , LDF and NDF  Locating .mdf File .ldf file in server  Role of MDF and LDF  Restore data after truncating the table(with example)  Differential Backup  Full Backup  Conclusion
  • 3. Introduction :- The SQL Server provides the facility to restore your database by attaching your .mdf file and .ldf file to the database. We can directly Attach our existing .mdf and .ldf file to an SQL instance by using SQL Server Management Studio or T-SQL. We can restore our .mdf and .ldf file through execute some query.
  • 4. What is MDF , LDF and NDF : - MDF :–Stands for Master Database File. It contains all the main information of the database that are part of the server. This extension also points to various other files. It plays a crucial role in information storage. Overall it is very important for safe and secure supervision of data. In case this file gets damaged, an MDF recovery procedure is conducted to recover it. Doing so is important in order to save the data from going missing. NDF :-stand for Next Data File . NDF file is a user defined secondary database file of Microsoft SQL Server with an extension .ndf, which store user data. Moreover, when the size of the database file growing automatically from its specified size, you can use .ndf file for extra storage and the .ndf file could be stored on a separate disk drive. Every NDF file uses the same filename as its corresponding MDF file. We cannot open an .ndf file in SQL Server Without attaching its associated .mdf file. LDF :–Stand for LOG Database File .This file stores information related to transaction logs for main data file. It basically keeps track of what all changes have been made in the database. The information that this file stores ranges from date/time of change, details of the changes made, as well as information related to whoever made the changes. Information related to computer terminals where changes took place is also stored in the logs.
  • 5. Locating .mdf File .ldf file in server :- Within each database, you will find two files namely MDF and LDF. These two are basically file extensions used in Microsoft SQL. These files get automatically created at the time of database creation. They also share the same storage location. The reason why these files are so important is because they happen to be part of backup and recovery process. In simpler words, in case something bad happens to the database, these are the files the administrator will resort to for restoring and recovering the lost/damaged data. Query can Execute:  select * from sys.database_files
  • 6. Role of MDF and LDF :-  select * from fn_dblog (null , null)
  • 7. Role of MDF and LDF The primary purpose of an LDF file is to provide the ACID concept – Atomicity, Consistency, Isolation, and Durability Atomicity: if one part of the transaction fails, the entire transaction fails, and the database state is left unchanged Consistency: any transaction brings the database from one valid state to another Isolation: the execution of concurrent transactions brings the database to a state as if the transactions were executed serially, one by one Durability: once committed, the transaction remain so, even in the case of errors, power loss, or crashes An LDF file stores enough information to replay or undo a change, or recover the database to a specific point in time. Therefore, due to various auditing or recovery requirements, there is often a need to open the LDF file and view its contents. But viewing LDF file content is not an easy task
  • 8. Restore data after truncating the table :- The below example will show how can retrieve data after truncate or delete if happen : use master 1 .create database Test go USE Test GO CREATE TABLE Student ( StudentID BIGINT IDENTITY PRIMARY KEY, StudentName VARCHAR(128), RollNo VARCHAR(10) ) GO INSERT INTO Student(StudentName , RollNo) VALUES ('Reza','101') ,('Hari','102') ,('Sunil','103') ,('Naveen','104') GO
  • 9. Restore data after truncating the table :- 3. Take Database Backup : BACKUP DATABASE Test TO DISK = 'D:BackupNew folderMyTestDB.BAK' GO 4. Truncate table student 5. select * from Student 2. select * from Student
  • 10. Restore data after truncating the table :- 6. SELECT [Current LSN], Operation, [Transaction ID], [Begin Time], [Transaction Name] FROM fn_dblog (NULL, NULL) where [Transaction Name] ='TRUNCATE TABLE‘ 7. 00000021:0000005d:0001 take LSN (Log Sequence Numbers) from above code. 8. Convert LSN number from HEX to Decimal number. like below code SELECT CAST (CONVERT (VARBINARY,'0x'+'00000021', 1) AS INT) as FirstPart, --33 CAST (CONVERT (VARBINARY,'0x'+'0000005d', 1) AS INT) as SecondPart, --93 CAST (CONVERT (VARBINARY,'0x'+'0001', 1) AS INT)as ThirdPart --1 GO
  • 11. Restore data after truncating the table :- 9. Add preceding zeros for 93and 1.  Note : no need for 33  93=0000000093 (8 zeroes) total should be 10 digits  1 =00001 (4 zeroes) total should be 5 digits.  => 33000000009300001  Now do following steps one by one . BACKUP LOG Test TO DISK = 'D:BackupNew folderMyTestDB.TRN' GO RESTORE DATABASE MyTestDB_Copy FROM DISK = 'D:BackupNew folderMyTestDB.bak' WITH MOVE 'Test' TO 'D:BackupNew folderMyTestDB.mdf', MOVE 'Test_log' TO 'D:BackupNew folderMyTestDB_log.ldf', NORECOVERY GO
  • 12. Restore data after truncating the table :- RESTORE LOG MyTestDB_Copy FROM DISK = N'D:BackupNew folderMyTestDB.TRN' WITH STOPBEFOREMARK = 'LSN:33000000009300001’ 10 . use MyTestDB_Copy select * from Student
  • 13. Differential Backup:- A differential backup is created similarly to a full backup, but with one important difference – the differential backup only contains the data that has changed since the last full backup (the active portion of the transaction log). Differential backups are cumulative not incremental. This means that a differential backup contains all changes made since the last full backup, in spite of the fact that they have already been included in previous differential backups. Differential backups are created the following way: How to Make a Differential Backup To make a differential database backup simply add “WITH DIFFERENTIAL” clause: BACKUP DATABASE your_database TO DISK = 'diff.bak' WITH DIFFERENTIAL
  • 14. Full Backup :- The simplest kind of SQL Server backup is a full database backup. It provides a complete copy of the database but allows restoring the database only to a point-in-time when the backup was made.Even if you add the “WITH STOPAT=<time or log sequence number>” option to restore command you will not get the expected result because this option applies only when you restore the transaction log. Please see how a periodic full backup works in the picture below: How to Make a Full Backup To make a full backup you can use T-SQL command: BACKUP DATABASE your_database TO DISK = 'full.bak' Another way to backup a database is to use SQL Server Management Studio (SSMS): right click on the database you want to backup, select “Tasks”, then “Back up…”. Choose “Full” backup type, add a backup destination and click “OK”.
  • 15. Conclusion In the above section, we learned how to restore LDF and MDF files to a database. It is highly recommended that you have to detach the MDF file before attaching a new one. This is a very helpful method to restore database using MDF and LDF file. 1. https://sqljumble.blogspot.in/2016/11/how-to-recover-data-after-truncate.html 2. https://sqlbak.com/blog/recover-deleted-data-in-sql-server/ 3. https://sqlbak.com/academy