7.0 Overview of SQL Server
7.0 Overview of 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 ] ]
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
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
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
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
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.
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