You are on page 1of 22

Module 2:

Managing Databases
and Files
Overview

Planning Databases
Creating Databases
Managing Databases
Lesson 1: Planning Databases

How Is Data Stored?


How the Transaction Log Works
Considerations for File Placement
What Are Filegroups?
Considerations for Creating Filegroups
Considerations for Capacity Planning
How Is Data Stored?

Data file:
.mdf or .ndf
Log
file: .ldf

Extent: 8 contiguous
8KB pages

Page: 8KB
How the Transaction Log Works

1 Data modification is
sent by application

3 Modification is recorded
Buffer Cache in transaction log on disk

Disk

Data pages are located in, Disk


2 or read into, buffer cache
and modified Checkpoint writes
committed
4 transactions
to database
Considerations for File Placement

Types of files
 Primary
 Secondary
 Transaction logs
File placement
 Place files on different disks for performance and
recovery
What Are Filegroups?

AdventureWorks Database
sys... ...
sys... SalesOrderHeader
sysusers OrdHistYear2
Customer
sysobjects Product OrdHistYear1

C:\ D:\ E:\


OrdHist1.ndf
AdventureWorks_
OrdHist2.ndf AdventureWorks_
Data.mdf
Log.Idf
Primary Filegroup OrderHistoryGroup
Considerations for Creating Filegroups

Creating user-defined filegroups


 Performance
 Manageability
Specifying the default filegroup
Using read-only filegroups
Considerations for Capacity Planning

Estimating the size of a database


Size of model database
Predicted growth
Indexes
Size of transaction log
Lesson 2: Creating Databases

Considerations for Creating Databases


Syntax for Creating Databases
How to Resize Database Files
What Are Schemas?
Syntax for Creating Schemas
How Object Name Resolution Works
Practice: Creating Databases
Considerations for Creating Databases

model database

Primary Log
Secondary

Database files include primary, secondary, and log files


Considerations for database size
 Initial size of data files
 Initial size of log files
 Potential growth of physical data storage
Syntax for Creating Databases

CREATE DATABASE database_name


[ ON
[ PRIMARY ] [ filespec [, n] ]
[ FILEGROUP filegroup_name [DEFAULT]
filespec [, n] ] [, n] ]
[ LOG ON
[filespec [, n] ]
[ COLLATE collation_name ] ]
How to Resize Database Files

Enlarging a database file

ALTER DATABASE database_name


MODIFY FILE
( NAME = logical_file_name,
SIZE = size [ KB | MB | GB | TB ] )
Shrinking a database file

DBCC SHRINKFILE
( { 'file_name' | file_id }
{ [ ,EMPTYFILE ]
| [ [ , target_size ] [ ,
{ NOTRUNCATE | TRUNCATEONLY } ] ]
} )
What Are Schemas?

Namespaces for database objects

Person

Contact
(Server1.AdventureWorks.Person.Contact)

Sales

Customer
(Server1.AdventureWorks.Sales.Customer)

dbo

ErrorLog AdventureWorks
(Server1.AdventureWorks.dbo.ErrorLog)
Syntax for Creating Schemas

CREATE SCHEMA
schema_name | AUTHORIZATION owner_name |
schema_name AUTHORIZATION owner_name
[ table_definition | view_definition |
grant_statement | revoke_statement |
deny_statement ]
How Object Name Resolution Works

Person

SELECT * FROM Contact

Lance Contact
(Default schema = Person)

SELECT * FROM Person.Contact Sales

SELECT * FROM ErrorLog

SELECT * FROM Contact


Anders
(Default schema = Sales)
dbo

ErrorLog
Practice: Creating Databases

In this practice, you will:


Create a database by using SQL Server
Management Studio
Create a database by using Transact-SQL
Lesson 3: Managing Databases

Database Options
Sources of Database Metadata
Demonstration: Retrieving Database Metadata
Database Options

Set database options by using:


SQL Server Management Studio
ALTER DATABASE statement
Option Category Purpose
Controls automatic behaviors such as statistics,
Automatic database closing, and shrinking
Controls whether database is online, who can
Availability
connect to it, and whether database is read-only
Cursor Controls cursor behavior and scope

Recovery Controls the recovery model for the database


Controls ANSI compliance options such as ANSI
SQL
nulls and recursive triggers
Sources of Database Metadata

Information Source Description

Visual tool that generates reports


SQL Server Management
displaying server and database
Studio
metadata

Provide metadata about database


Catalog views
objects returning rows of information

Return a single value of metadata


Metadata functions
information per function

Retrieve metadata using stored


System stored procedures
procedures
Demonstration: Retrieving Database Metadata

In this demonstration, you will see how to:


View server reports in SQL Server
Management Studio
View database reports in SQL Server
Management Studio
Use system stored procedures to retrieve
database metadata
Query catalog views to retrieve database
metadata
Lab: Managing Databases and Files

Exercise 1: Creating a Database


Exercise 2: Monitoring and Managing
Filegroup Usage
Exercise 3: Viewing Database Metadata

You might also like