0% found this document useful (0 votes)
6 views

7.0 Overview of SQL Server

The document provides an overview of SQL Server database files, detailing the three types: primary data files (.mdf), secondary data files (.ndf), and log files (.ldf). It explains the structure and purpose of these files, along with the concept of filegroups and system databases like master, msdb, model, and tempdb. Additionally, it highlights the importance of the transaction log for recovery processes and the management of database changes.

Uploaded by

Akinahom Yoseph
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

7.0 Overview of SQL Server

The document provides an overview of SQL Server database files, detailing the three types: primary data files (.mdf), secondary data files (.ndf), and log files (.ldf). It explains the structure and purpose of these files, along with the concept of filegroups and system databases like master, msdb, model, and tempdb. Additionally, it highlights the importance of the transaction log for recovery processes and the management of database changes.

Uploaded by

Akinahom Yoseph
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

SQL Server

Overview

Database Administration
Database Files

Database Programming
Database Files
 SQL Server maps a database over a set of
operating-system files
 Data and log information are never mixed in the
same file
 individual files are used only by one database
 SQL Server databases have three types of files:
 Primary data files
 Secondary data files
 Log files

3
Primary data files
 The primary data file is the starting point of the
database and points to the other files in the
database.
 Every database has one primary data file.
 The file name extension for primary data files is
.mdf.

4
Secondary data files
 Secondary data files make up all the data files,
other than the primary data file.
 Some databases may not have any secondary
data files, while others have several secondary
data files.
 The file name extension for secondary data files
is .ndf.

5
Log files
 Log files hold all the log information that is used
to recover the database.
 There must be at least one log file for each
database, although there can be more than one.
 The file name extension for log files is .ldf.

6
Logical / Physical File Names
 SQL Server files have two names:
 logical_file_name
 the name used to refer to the physical file in all
Transact-SQL statements.
 must comply with the rules for SQL Server identifiers
and must be unique among logical file names in the
database.
 os_file_name
 thename of the physical file including the path.
 must follow the rules for the operating system file
names.
7
File Size
 Files are created by specifying their initial size
 If
size is missing, the size of the primary file in the
model database will be used
 File growth increment can also be specified
 Every time the file is filled, it increases its size by the
growth increment.
 Each file can also have a maximum size
specified.
 Ifa maximum size is not specified, the file can
continue to grow until it has used all available space
on the disk.

8
Database Filegroups
 Database objects and files can be grouped
together in filegroups for allocation and
administration purposes.
 Log files cannot be placed in a filegroup.
 No file can be a member of more than one filegroup.
 Tables, indexes, and large object data can be
associated with a specified filegroup.

9
The Default filegroup
 One filegroup in each database is designated
the default filegroup.
 When a table or index is created without
specifying a filegroup, they will be created in the
default filegroup.
 Only one filegroup at a time can be the default
filegroup.
 If no default filegroup is specified, the primary
filegroup is the default filegroup.

10
Types of filegroups
 There are two types of filegroups:
 Primary filegroup
 The primary filegroup contains
 the primary data file and
 any other files not specifically assigned to another filegroup.
 Allpages for the system tables are allocated in the
primary filegroup.
 User-defined filegroup
 areany filegroups that are specified by using the
FILEGROUP keyword in a CREATE DATABASE or
ALTER DATABASE statement.

11
Create Database [Syntax]
CREATE DATABASE database_name
[ ON
[ PRIMARY ]
[ <filespec> [ ,...n ]

[ , <filegroup> [ ,...n ] ]

[ LOG ON { <filespec> [ ,...n ] } ]


]
]

12
Example 1
USE master;
GO
CREATE DATABASE MyDB
ON PRIMARY
( NAME='MyDB_Primary',
FILENAME= 'c:\TestDB\data\MyDB_Prm.mdf',
SIZE=4MB,
MAXSIZE=10MB,
FILEGROWTH=1MB)

13
Example 2
USE master;
GO
CREATE DATABASE MyDB
ON PRIMARY
( NAME='MyDB_Primary',
FILENAME= 'c:\TestDB\data\MyDB_Prm.mdf',
SIZE=4MB,
MAXSIZE=10MB,
FILEGROWTH=1MB)
FILEGROUP MyDB_FG1
( NAME = 'MyDB_FG1_Dat1',
FILENAME = 'c:\ TestDB\data\MyDB_FG1_1.ndf',
SIZE = 1MB,
MAXSIZE=10MB,
FILEGROWTH=1MB)
14
Example 3
CREATE TABLE [database_name.[owner].] table_name
(<column name> <data type>
[ [DEFAULT <constant expression>]
| [IDENTITY [(seed, increment)] ] ]
[NULL|NOT NULL]
[<column constraints>]
| [column_name AS computed_column_expression]

| [<table_constraint>]
[,...n]
)
[ON {<filegroup>|DEFAULT}]
[TEXTIMAGE_ON {<filegroup>|DEFAULT}]

15
Example 4
USE master;
GO

ALTER DATABASE MyDB


MODIFY FILEGROUP MyDB_FG1 DEFAULT;
GO

-- Create a table in the user-defined filegroup.


USE MyDB;
CREATE TABLE MyTable
( cola int PRIMARY KEY, colb char(8) )
ON MyDB_FG1;
GO

16
The
Transaction Log

Database Administration
Transaction Log
 When SQL Server needs to modify data:
 It reads the page that contains the database records into
memory
 It makes the required changes on the pages in memory

 Pages are NOT instantly written to the disk because:


 There may be some more changes to be made in the page
 Keeping the page in memory is faster

 Keeping changes on the data pages in memory has


some risk (?)

18
Transaction Log (cont.)
 SQL Server uses the Transaction Log to keep track of
the changes made in the memory pages
 Changes made on the memory pages are instantly written onto
the transaction Log files (on disk)
 This is what makes the transaction Log file fundamental to SQL
Server’s Recovery process
 This is why SQL Server’s Transaction Log is called
Write-Ahead-Log

19
Write Ahead Log

Write Ahead Log (video: duration less than 4 min)


https://youtu.be/rPfncf8W1MA

20
Write Ahead Log
 XXX

21
Page copied to memory

22
Modification made in memory & log

23
More pages moved to memory

24
More changes made in memory

25
Data written back to database

26
After system crash …..

27
System Databases

Database Administration
Database Administration
 By default, SQL Server provides you with four main
systems databases:
 master Database
 msdb Database
 model Database
 tempdb Database

29
master Database
 The master database stores all the system-level
information of an SQL Server instance.
 This information which includes:
 Server configuration settings
 Logon accounts
 Startup stored procedure
 File locations of user databases

 If the master database is unavailable, the SQL Server


cannot start

30
master Database (cont.)
 When you work with the master database, you should:
 Have a current backup of the master database.
 If the master database is corrupted, you can restore it from the
backup.
 Back up the master database as soon as possible whenever
some major operations.
 The master database should be backed up after the
following operations:
 Creating, modifying, and dropping any database
 Changing the server configurations
 Update the logon accounts including adding, removing, and
modifying

31
msdb Database
 The msdb is used by the SQL Server Agent for
scheduling jobs and alerts.
 It also stores the history of the SQL Agent jobs

32
model Database
 SQL Server uses the model database as the template for
creating other databases.
 When you create a new database, SQL Server copies
the contents of the model database including database
options to the new database.
 If you modify the model database, all databases that you
create afterward will take these changes.

33
tempdb Database
 The tempdb database stores
 temporary user objects that you explicitly create
 temporary tables and table variables.
 the internal objects that the database engine creates
 For example, the tempdb database stores the immediate sort results for
running the queries that include the ORDER BY clause.

 SQL Server recreates the tempdb database every time it


starts.
 Since the tempdb is non-permanent storage, you cannot
back it up or restore it.

34
Summary
 The master system database stores system-level
information of the SQL server instance
 The msdb database is used by SQL Server Agent for
jobs & alerts
 The model database is served as a template for creating
other databases
 The tempdb system database stores the temporary
objects

35

You might also like