Anda di halaman 1dari 31

Module 3:

Creating and Managing


Databases
Vidya Vrat Agarwal. | MCT, MCSD
SQL Server Database
A Database is a collection of Tables and Objects such as Views,
Triggers, Stored Procedures.
The Data and Objects in a database are stored as a set of Operating
System files.
The types of files that are used to store a database in SQL Server are:
• Primary
• Secondary
• Transaction Log
A database must consist of a Primary data file and one Transaction
log file.
The Primary data file can be used for database objects, and
Secondary data files can be used to store User data and objects.
Contd……..
SQL Server Database

Database need not have Secondary data file, if the Primary


Data file is Large Enough to hold the data in Database .
The Transaction log records all the modification that have
occurred in the database and which transaction performed
the modifications. E.g-Insert, Update, Delete.
The Transaction log files hold the log information used to
recover a database. There can be more than one
transaction log file.
The Primary data file has a .mdf extension
The Secondary data file has a .ndf extension
The Transaction log file has a .ldf extension
Creating Database
 Creating a Database Defines:
The name of the database
The size of the database
The files where the database will reside
CREATE
CREATE DATABASE
DATABASE Sample
Sample
ON
ON
PRIMARY
PRIMARY (( NAME
NAME == SampleData,
SampleData,
FILENAME
FILENAME ='c:\Program
='c:\Program Files\Data\Sample.mdf',
Files\Data\Sample.mdf',
SIZE = 10MB,
SIZE = 10MB,
MAXSIZE
MAXSIZE == 15MB,
15MB,
FILEGROWTH
FILEGROWTH == 20%
20% ))
LOG
LOG ON
ON
(( NAME
NAME == SampleLog,
SampleLog,
FILENAME
FILENAME =‘c:\Program Files\Data\Sample.ldf',
=‘c:\Program Files\Data\Sample.ldf',
SIZE = 3MB,
SIZE = 3MB,
MAXSIZE
MAXSIZE == 5MB,
5MB,
FILEGROWTH
FILEGROWTH == 1MB
1MB ))
Size
This parameter specifies the size of the data or log file. You
can specify sizes in megabytes (MB)—the default value—or
kilobytes (KB). The minimum size is 512 KB for both the
data and log file.

MaxSize
This parameter specifies the maximum size to which the file
can grow. You can specify sizes in megabytes—the default
value—or kilobytes.
If you do not specify a size, the file grows until the disk is
full.
FileGrowth
This parameter specifies the growth increment of the file.
The FILEGROWTH setting for a file cannot exceed the
MAXSIZE setting. A value of 0 indicates no growth.
The value can be specified in megabytes—the default—in
kilobytes, or as a percentage (%).
The default value if FILEGROWTH is not specified is 10
percent, and the minimum value is 64 KB (one extent).
The specified size is rounded to the nearest 64 KB. in three
ways: in megabytes, in kilobytes, or as a percentage. The
percentage only applies to file growth, not maximum size.
Retrieving Database Information

 Determine Database Properties by Using the


DATABASEPROPERTYEX Function
 Use System Stored Procedures to Display Information
About Databases and Database Parameters
 sp_helpdb
 sp_helpdb database_name
 sp_spaceused [objname]
FileGroups

A FileGroup is a Collection of files. The Files may or may not


be on the same Disk Drive.
Purpose
FileGroups improve the performance of the database .
For Better database performance, files and filegroups should
be created on different disks.
Filegroups

Northwind Database
sys...
sys... ...
...
sys...
sys... Orders
Orders
sysusers OrdHistYear2
OrdHistYear2
sysusers Customers
Customers
sysobjects
sysobjects Products OrdHistYear1
OrdHistYear1
Products

C:\ D:\ E:\

OrdHist1.ndf
OrdHist1.ndf
Northwind.mdf
Northwind.mdf OrdHist2.ndf
OrdHist2.ndf Northwind.Idf
Northwind.Idf

Default Filegroup OrderHistoryGroup


Managing Data and Log File Growth
 Using Automatic File Growth
 Expanding Database Files
 Adding Secondary Database Files
ALTER
ALTER DATABASE
DATABASE Sample
Sample
MODIFY
MODIFY FILE
FILE (( NAME
NAME == 'SampleLog',
'SampleLog',
SIZE
SIZE == 15MB)
15MB)
GO
GO

ALTER
ALTER DATABASE
DATABASE Sample
Sample
ADD
ADD FILE
FILE
(( NAME
NAME == SampleData2,
SampleData2,
FILENAME
FILENAME ='c:\Program
='c:\Program Files\..\..\
Files\..\..\
Data\Sample2.ndf',
Data\Sample2.ndf',
SIZE
SIZE == 15MB,
15MB,
MAXSIZE
MAXSIZE == 20MB
20MB ))
GO
GO
Renaming a Database

The database should not be in use when it is being renamed .


sp_renamedb ‘old_Dbname’, ‘new_Dbname’
Example – sp_renamedb ‘employee’,’emp’
Dropping a Database

Methods of Dropping a Database


 SQL Server Enterprise Manager
 DROP DATABASE statement

DROP
DROP DATABASE
DATABASE Northwind,
Northwind, pubs
pubs

Restrictions on Dropping a Database


 When a user is connected to it
 If it is a system database
How Data Is Stored

Database

Data (file) Log (file)


.mdf or .ndf .Idf

Tables, Extent
Indexes 1 2 3 5 6 7 8 (8 contiguous
8-KB pages)
Data 4 Page (8 KB)
 SQL Server stores, reads, and writes data in 8-KB
blocks of contiguous disk space called Pages.
 All pages are stored in extents. An extent is eight
contiguous pages, or 64 KB.
 This means that a database can store 128 pages per
megabyte.
1 MB = 1024 KiloBytes
1024 KB / 8 KB (Page) = 128 Pages per MB
1024 KB / 64 Kb (Extent) = 16 Extents per MB
Pages

Pages and extents are the primary data structures in the


SQL Server physical database.
Types of Pages
SQL Server uses several types of pages:
• some track space allocation, and
• some contain user and index data.
Extents

Types of Extents
SQL Server uses two types of extents:
Mixed Extents
Extents that contain pages from two or more objects are
called Mixed Extents.
Every table starts as a mixed extent. Database
use mixed extents primarily for pages that
track space and contain small objects.

Mixed
Extent
Extents

Uniform Extents
Extents that have all eight pages allocated to a single object
are called uniform extents. They are used when tables or
indexes need more than 64 KB of space.

Uniform
Extents
Check Your Understanding.
Q.1. What are the types of files to store
a Database in SQL Server. ?
Q.2. The Primary data file has a _______ extension. ?
Q.3. The Secondary data file has a __________ extension.
Q.4. The Transaction log file has a ________ extension.
Q.5. What is the Command to create a Database in
SQL Server. ?
Q.6. What are the the Parameters used to create
a Database in SQL Server. ?
Q.7. What is the difference between Name and
Filename. ?
Q.8. What is the difference between Size and
Maxsize. ?
Q.9. What is the SQL Statement to delete a
database. ?
Q.10. What is FileGroup and what is its Purpose. ?
Q.11. What is the Command to rename
a database?
Q.12. What is the difference between Mixed and
Uniform extents .
Winners don’t do
different things
they do things
differently.

Thank You.

Anda mungkin juga menyukai