SlideShare una empresa de Scribd logo
1 de 16
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

Más contenido relacionado

La actualidad más candente

MySQL Enterprise Edition
MySQL Enterprise EditionMySQL Enterprise Edition
MySQL Enterprise EditionMySQL Brasil
 
Oracle Architecture
Oracle ArchitectureOracle Architecture
Oracle ArchitectureNeeraj Singh
 
Database backup & recovery
Database backup & recoveryDatabase backup & recovery
Database backup & recoveryMustafa Khan
 
MySQL Architecture and Engine
MySQL Architecture and EngineMySQL Architecture and Engine
MySQL Architecture and EngineAbdul Manaf
 
End-to-end Troubleshooting Checklist for Microsoft SQL Server
End-to-end Troubleshooting Checklist for Microsoft SQL ServerEnd-to-end Troubleshooting Checklist for Microsoft SQL Server
End-to-end Troubleshooting Checklist for Microsoft SQL ServerKevin Kline
 
Sql 2012 always on
Sql 2012 always onSql 2012 always on
Sql 2012 always ondilip nayak
 
Less05 storage
Less05 storageLess05 storage
Less05 storageImran Ali
 
Microsoft SQL Server Database Administration.pptx
Microsoft SQL Server Database Administration.pptxMicrosoft SQL Server Database Administration.pptx
Microsoft SQL Server Database Administration.pptxsamtakke1
 
Training AWS: Module 8 - RDS, Aurora, ElastiCache
Training AWS: Module 8 - RDS, Aurora, ElastiCacheTraining AWS: Module 8 - RDS, Aurora, ElastiCache
Training AWS: Module 8 - RDS, Aurora, ElastiCacheBùi Quang Lâm
 
Active directory interview questions
Active directory interview  questionsActive directory interview  questions
Active directory interview questionsAnand Dhouni
 
Oracle architecture with details-yogiji creations
Oracle architecture with details-yogiji creationsOracle architecture with details-yogiji creations
Oracle architecture with details-yogiji creationsYogiji Creations
 
DB2 and storage management
DB2 and storage managementDB2 and storage management
DB2 and storage managementCraig Mullins
 
Solving the DB2 LUW Administration Dilemma
Solving the DB2 LUW Administration DilemmaSolving the DB2 LUW Administration Dilemma
Solving the DB2 LUW Administration DilemmaRandy Goering
 
Boost Performance With My S Q L 51 Partitions
Boost Performance With  My S Q L 51 PartitionsBoost Performance With  My S Q L 51 Partitions
Boost Performance With My S Q L 51 PartitionsPerconaPerformance
 

La actualidad más candente (20)

MySQL Enterprise Edition
MySQL Enterprise EditionMySQL Enterprise Edition
MySQL Enterprise Edition
 
Oracle DBA
Oracle DBAOracle DBA
Oracle DBA
 
Oracle Architecture
Oracle ArchitectureOracle Architecture
Oracle Architecture
 
Database backup & recovery
Database backup & recoveryDatabase backup & recovery
Database backup & recovery
 
MySQL Architecture and Engine
MySQL Architecture and EngineMySQL Architecture and Engine
MySQL Architecture and Engine
 
End-to-end Troubleshooting Checklist for Microsoft SQL Server
End-to-end Troubleshooting Checklist for Microsoft SQL ServerEnd-to-end Troubleshooting Checklist for Microsoft SQL Server
End-to-end Troubleshooting Checklist for Microsoft SQL Server
 
Sql 2012 always on
Sql 2012 always onSql 2012 always on
Sql 2012 always on
 
Less05 storage
Less05 storageLess05 storage
Less05 storage
 
Oracle archi ppt
Oracle archi pptOracle archi ppt
Oracle archi ppt
 
Microsoft SQL Server Database Administration.pptx
Microsoft SQL Server Database Administration.pptxMicrosoft SQL Server Database Administration.pptx
Microsoft SQL Server Database Administration.pptx
 
SKILLWISE-DB2 DBA
SKILLWISE-DB2 DBASKILLWISE-DB2 DBA
SKILLWISE-DB2 DBA
 
AlwaysON Basics
AlwaysON BasicsAlwaysON Basics
AlwaysON Basics
 
Training AWS: Module 8 - RDS, Aurora, ElastiCache
Training AWS: Module 8 - RDS, Aurora, ElastiCacheTraining AWS: Module 8 - RDS, Aurora, ElastiCache
Training AWS: Module 8 - RDS, Aurora, ElastiCache
 
Active directory interview questions
Active directory interview  questionsActive directory interview  questions
Active directory interview questions
 
Oracle architecture with details-yogiji creations
Oracle architecture with details-yogiji creationsOracle architecture with details-yogiji creations
Oracle architecture with details-yogiji creations
 
DB2 and storage management
DB2 and storage managementDB2 and storage management
DB2 and storage management
 
ASM
ASMASM
ASM
 
Solving the DB2 LUW Administration Dilemma
Solving the DB2 LUW Administration DilemmaSolving the DB2 LUW Administration Dilemma
Solving the DB2 LUW Administration Dilemma
 
AD & LDAP
AD & LDAPAD & LDAP
AD & LDAP
 
Boost Performance With My S Q L 51 Partitions
Boost Performance With  My S Q L 51 PartitionsBoost Performance With  My S Q L 51 Partitions
Boost Performance With My S Q L 51 Partitions
 

Similar a MDF and LDF in SQL Server

Db2 migration -_tips,_tricks,_and_pitfalls
Db2 migration -_tips,_tricks,_and_pitfallsDb2 migration -_tips,_tricks,_and_pitfalls
Db2 migration -_tips,_tricks,_and_pitfallssam2sung2
 
The best ETL questions in a nut shell
The best ETL questions in a nut shellThe best ETL questions in a nut shell
The best ETL questions in a nut shellSrinimf-Slides
 
database backup and recovery
database backup and recoverydatabase backup and recovery
database backup and recoverysdrhr
 
SQL Repair Tool.
SQL Repair Tool.SQL Repair Tool.
SQL Repair Tool.SysCurve
 
data stage-material
data stage-materialdata stage-material
data stage-materialRajesh Kv
 
Steps for upgrading the database to 10g release 2
Steps for upgrading the database to 10g release 2Steps for upgrading the database to 10g release 2
Steps for upgrading the database to 10g release 2nesmaddy
 
Sql server lesson10
Sql server lesson10Sql server lesson10
Sql server lesson10Ala Qunaibi
 
Recipe 14 - Build a Staging Area for an Oracle Data Warehouse (2)
Recipe 14 - Build a Staging Area for an Oracle Data Warehouse (2)Recipe 14 - Build a Staging Area for an Oracle Data Warehouse (2)
Recipe 14 - Build a Staging Area for an Oracle Data Warehouse (2)Massimo Cenci
 
Standby db creation commands
Standby db creation commandsStandby db creation commands
Standby db creation commandsPiyush Kumar
 
Managing SQLserver for the reluctant DBA
Managing SQLserver for the reluctant DBAManaging SQLserver for the reluctant DBA
Managing SQLserver for the reluctant DBAConcentrated Technology
 
ELT Publishing Tool Overview V3_Jeff
ELT Publishing Tool Overview V3_JeffELT Publishing Tool Overview V3_Jeff
ELT Publishing Tool Overview V3_JeffJeff McQuigg
 
Sql server lesson3
Sql server lesson3Sql server lesson3
Sql server lesson3Ala Qunaibi
 
Db2 Important questions to read
Db2 Important questions to readDb2 Important questions to read
Db2 Important questions to readPrasanth Dusi
 
The best Teradata RDBMS introduction a quick refresher
The best Teradata RDBMS introduction a quick refresherThe best Teradata RDBMS introduction a quick refresher
The best Teradata RDBMS introduction a quick refresherSrinimf-Slides
 
Ibmtsm 100325083335-phpapp01
Ibmtsm 100325083335-phpapp01Ibmtsm 100325083335-phpapp01
Ibmtsm 100325083335-phpapp01Andrew Adam
 

Similar a MDF and LDF in SQL Server (20)

Db2 migration -_tips,_tricks,_and_pitfalls
Db2 migration -_tips,_tricks,_and_pitfallsDb2 migration -_tips,_tricks,_and_pitfalls
Db2 migration -_tips,_tricks,_and_pitfalls
 
The best ETL questions in a nut shell
The best ETL questions in a nut shellThe best ETL questions in a nut shell
The best ETL questions in a nut shell
 
database.ppt
database.pptdatabase.ppt
database.ppt
 
database backup and recovery
database backup and recoverydatabase backup and recovery
database backup and recovery
 
SQL Repair Tool.
SQL Repair Tool.SQL Repair Tool.
SQL Repair Tool.
 
oracle dba
oracle dbaoracle dba
oracle dba
 
Oracle tutorial
Oracle tutorialOracle tutorial
Oracle tutorial
 
data stage-material
data stage-materialdata stage-material
data stage-material
 
Steps for upgrading the database to 10g release 2
Steps for upgrading the database to 10g release 2Steps for upgrading the database to 10g release 2
Steps for upgrading the database to 10g release 2
 
Sql server lesson10
Sql server lesson10Sql server lesson10
Sql server lesson10
 
Recipe 14 - Build a Staging Area for an Oracle Data Warehouse (2)
Recipe 14 - Build a Staging Area for an Oracle Data Warehouse (2)Recipe 14 - Build a Staging Area for an Oracle Data Warehouse (2)
Recipe 14 - Build a Staging Area for an Oracle Data Warehouse (2)
 
Standby db creation commands
Standby db creation commandsStandby db creation commands
Standby db creation commands
 
Managing SQLserver for the reluctant DBA
Managing SQLserver for the reluctant DBAManaging SQLserver for the reluctant DBA
Managing SQLserver for the reluctant DBA
 
ELT Publishing Tool Overview V3_Jeff
ELT Publishing Tool Overview V3_JeffELT Publishing Tool Overview V3_Jeff
ELT Publishing Tool Overview V3_Jeff
 
Sql server lesson3
Sql server lesson3Sql server lesson3
Sql server lesson3
 
Db2 Important questions to read
Db2 Important questions to readDb2 Important questions to read
Db2 Important questions to read
 
Oracle11g notes
Oracle11g notesOracle11g notes
Oracle11g notes
 
The best Teradata RDBMS introduction a quick refresher
The best Teradata RDBMS introduction a quick refresherThe best Teradata RDBMS introduction a quick refresher
The best Teradata RDBMS introduction a quick refresher
 
Sqllite
SqlliteSqllite
Sqllite
 
Ibmtsm 100325083335-phpapp01
Ibmtsm 100325083335-phpapp01Ibmtsm 100325083335-phpapp01
Ibmtsm 100325083335-phpapp01
 

Último

The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfayushiqss
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...Nitya salvi
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfonteinmasabamasaba
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Hararemasabamasaba
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...masabamasaba
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...masabamasaba
 
%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durbanmasabamasaba
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...masabamasaba
 

Último (20)

The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 

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