3 Managing Database
3 Managing Database
3 Managing Database
Topic 3:
Managing Database
Learning Objectives
• user databases
– User databases are databases that users create and use.
– The database can be operate and manage by the users
– Can be accessed by the applications
SQL Server Databases
The following table describes each database:
SQL Server Databases
Database Objects
– A database is a collection of data, tables, views, clusters,
sequences, indexes and other objects.
– Any defined object that is used to store or reference data
– Database objects help you to structure data and define
data integrity mechanisms.
– The following table describes SQL Server database
objects.
SQL Server Databases
SQL Server Databases
Referring to SQL Server Objects
– Fully Qualified Names
• The complete name of a SQL Server object includes four
identifiers:
– server name
– database name
– owner name
– object name/id
in the following format: server.database.owner.object
• Each object that you create in SQL Server must have a unique,
fully qualified name.
• For example, you can have two tables named Orders in the
same database as long as they belong to different owners.
SQL Server Databases
– Partially Specified Names
• When referencing an object, you do not always have to specify the
server, database, and owner.
• Intermediate identifiers can be omitted as long as their positions are
indicated by periods.
• The following list contains valid formats for object names:
– server.database.owner.object
– database.owner.object
– database..object
– owner.object
– object
• When you create an object and do not specify the different parts of the
name, SQL Server uses the following defaults:
– Server defaults to the current instance on the local server.
– Database defaults to the current database.
– Owner defaults to the user name in the specified database associated with
the login ID of the current connection.
SQL Server Databases
Example
– The following example creates an OrderHistory table in the
Northwind database.
– Most object references use three-part names and default to the local server.
– Four-part names are generally used for distributed queries or remote stored
procedure calls.
SQL Server Databases
3. System Catalog
– Collection of system tables that stores metadata about the
entire system and all other databases.
– Found only in master database
– Most system tables begin with the sys prefix – resource database
– The following identifies several frequently used system tables and
views:
A Transaction
• Explicit Transaction
– SQL Server performs an explicit transaction when the beginning
and end of the transaction are explicitly defined.
– You can define the beginning and end of the transaction in
Transact-SQL by using BEGIN TRANSACTION and COMMIT
TRANSACTION statements.
• Transaction Log
– SQL Server records every transaction in a transaction log to
maintain database consistency and aid in recovery.
– The log is a storage area that automatically tracks changes to a
database.
– SQL Server records modifications in the log on disk as the
modifications are executed, before they are written in the
database.
A Transaction
• How the Transaction Log Works
– A data modification is sent by the application.
– The affected data pages are loaded from disk into
memory (buffer cache), provided that the pages are
not in the buffer cache.
– Each data modification statement is recorded in the
log as it is made and written to disk before that
change is made in the database. This called a write-
ahead log.
– On a recurring basis, the checkpoint process writes all
completed transactions to the database on the disk.
A Transaction
• If the system fails, the automatic recovery
process uses the transaction log to roll
forward all committed transactions and roll
back any incomplete transactions.
• Transaction markers in the log are used during
automatic recovery to determine the starting
and ending points of a transaction.
• A transaction is considered complete when
the BEGIN TRANSACTION marker has an
associated COMMIT TRANSACTION marker.
• Data pages are written to the disk when a
checkpoint occurs.
Creating Databases
• You can create a database by using:
– SQL Server Management Studio (SSMS)/GUI or
– the CREATE DATABASE Transact-SQL statement in
Query Editor Window
• When you create a database, SQL Server:
Creates a data file and a transaction log for that
database.
Requires that the owner and creator of the new
database have permission to use the master database.
Allows you to define the name of the database, the
properties of the database and the location of the
database files.
Creating Databases
SQL Server uses a copy of the model database
(MSDB) to initialize the database and its metadata.
Any options or settings in the model database are
copied into the new database.
SQL Server then fills the rest of the database with
empty pages, except for pages that have internal
data recording how the space is used in the
database.
Creating Databases
• When you create a database, you can specify the
following options:
Primary File
• The primary file consists of the initial data file in the primary filegroup.
• A filegroup is a named collection of data files.
• The primary filegroup contains all of the database system tables.
• It also contains all objects and data not assigned to user-defined
filegroups.
• The primary data file is the starting point of the database and points to
the rest of the files in the database.
• Every database has one primary data file and one primary filegroup.
• The recommended file name extension for primary data files is .mdf.
Creating Databases
Secondary Files
• Databases may have secondary data files.
• Some databases may be large enough to need
multiple secondary data files, or they may use
secondary files on separate disk drives to spread the
data across multiple disks.
• Secondary files may be either in the primary
filegroup or can be grouped into user-defined
filegroups.
• The recommended file name extension for secondary
data files is .ndf.
Creating Databases
Transaction Log
• Every database must have a transaction log.
• Unless specified otherwise, a transaction log file is
automatically created with a system-generated name.
• The recommended file name extension for transaction log files
is .ldf.
• Typically, the transaction log file is about 10 to 15 percent of
the database files size.
File Name and Location
• Every database file has a logical name and a physical location
for the file.
• Files generally should be spread across multiple disks for
performance and redundancy.
Creating Databases
Size
• You can specify sizes for each data and log file.
• The minimum size is 512 KB for both the data or log file.
• The size specified for the primary data file must be at
least as large as the primary file of the model database.
File Growth
• You can specify whether a file will grow in size if
necessary.
• This option is referred to as autogrowth.
• The default is to enable file growth.
Creating Databases
Maximum Size
• You can specify the maximum size to which a file can grow in
megabytes or as a percentage.
• The default growth value 1 MB for Primary data and 10 percent
for transaction log.
• It is recommended that you specify a maximum size to which the
file is permitted to grow.
• If you do not specify a size, and file growth is enabled, by default
the file will grow until the disk is full.
Collation
• This parameter specifies the default collation for the database.
• By default, a database inherits the collation of the instance of
SQL Server in which the database is created.
Creating Databases
• Changing Database Options After Database Creation
USE master ;
GO
CREATE DATABASE Sales
ON
( NAME = Sales_dat,
FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\saledat.mdf',
SIZE = 10,
MAXSIZE = 50,
FILEGROWTH = 5 )
LOG ON
( NAME = Sales_log,
FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\salelog.ldf',
SIZE = 5MB,
MAXSIZE = 25MB,
FILEGROWTH = 5MB ) ;
GO
Creating Databases
Viewing Database Properties
– To obtain database properties and information, you can
use:
• SSMS
• Query Editor
– system stored procedures
Managing Databases
Managing Data and Log File Growth
– Using SSMS
– the ALTER DATABASE statement (must be in the master
database)
DBCC SHRINKDATABASE
– Shrinks the size of the data files in the specified
database
– Examples - This example decreases the size of the
files in the UserDB user database to allow 10 percent
free space in the files of UserDB.
USE UserDB
GO
DBCC SHRINKFILE (DataFil1, 7)
GO
Managing Databases
Methods of Deleting a Database
• Delete databases using:
– SSMS or
– executing the DROP DATABASE statement.
• After delete a database, every login ID that used
that particular database as its default database
will not have a default database.
• Must be very careful, because no UNDO for
deleting database. But there are a solution.
Managing Databases
Examples
A. Drop/delete a single database
– This example removes all references for the publishing
database from the system tables.
DROP DATABASE publishing
Types of Filegroups
SQL Server has a primary filegroup and may also
have user-defined filegroups.
– The primary filegroup contains the primary
data file with the system tables.
– A user-defined filegroup consists of data files
that are grouped together for allocation and
administrative purposes.
Optimizing a Database Using Filegroups
Placing Files on Separate Disks
— Create user-defined filegroups to
separate files that are heavily
queried from those that are heavily
modified.
— In the illustration, the OrdHist1.ndf
and OrdHist2.ndf files are placed on
a separate disk from the Products,
Customers, and Orders tables
because they are queried for
decision support rather than updated
with current order information.
Optimizing a Database Using Filegroups
– Place the Ordhist1.ndf and
Ordhst2.ndf files on separate
disks if they are both heavily
queried.
USE master
ALTER DATABASE Test1
ADD FILEGROUP Test1FG1
GO
ALTER DATABASE Test1 ADD FILE
( NAME = test1dat3, FILENAME = 'c:\Program Files\Microsoft SQL Server\MSSQL\Data\t1dat3.ndf', SIZE = 5MB, MAXSIZE
= 100MB, FILEGROWTH = 5MB),
( NAME = test1dat4, FILENAME = 'c:\Program Files\Microsoft SQL Server\MSSQL\Data\t1dat4.ndf', SIZE = 5MB, MAXSIZE
= 100MB, FILEGROWTH = 5MB)
TO FILEGROUP Test1FG1
ALTER DATABASE Test1
MODIFY FILEGROUP Test1FG1 DEFAULT
GO
Optimizing a Database Using Filegroups
Choosing a Default Filegroup
– SQL Server designates one filegroup as the default
filegroup.
– The default filegroup is set to the primary filegroup
at the time of database creation, unless you specify
otherwise.
– The default filegroup contains the pages for all tables
and indexes that do not have a filegroup specified
when they are created.
Optimizing a Database Using Filegroups
Sizing the Primary Default Filegroup.
– The primary filegroup must be large enough to hold all
system tables and any tables and indexes not allocated
to a user-defined filegroup.
Viewing Filegroup Information
– You can view information about filegroups by using
SSMS or system stored procedures in Transact-SQL.
– Example using T-SQL:
Use AdventureWorks
GO
exec sp_helpfilegroup
Optimizing a Database Using Filegroups
Using Filegroups for performance
– Whenever a table is accessed sequentially, the
system creates a separate thread for each file in
parallel.
– When the system performs a table scan for a table in
a filegroup with four files, it uses four separate
threads to read the data in parallel.
– In general, using multiple files on separate disks
improves performance.
– However, too many files can cause too many parallel
threads and create bottlenecks.
Optimizing a Database Using Filegroups
The default location for all data files and transaction logs for SQL
Server 2014 is C:\Program Files\Microsoft SQL Server\
MSSQL12.MSSQLSERVER\MSSQL\DATA
Capacity Planning
96 Bytes
8060 Bytes
Estimate disk space (in Megabyte) required for STUDENTDB database if all
tables is estimated to have 500,000 rows. Assume each row in every table also
has 2 bytes overhead and index files consume 5 percent (%) of STUDENTDB
size. Present your calculation to estimate the disk spaces.
EXERCISE - STUDENTDB
Estimate disk space for STUDENTDB. Assume all tables have 500,000 rows.