Вы находитесь на странице: 1из 43

Database

Administration

Topics
Database Files and Logs
Database Administration Basics
Creating Databases
Altering Databases and Their Options
Managing Database and Log Size
Manipulating Databases

MS SQL Server
Database - a collection of data and the
objects that represent and interact with that
data
Tables, views, stored procedures, triggers,
and constraints are typical database objects
A single database server instance can have up
to 32,767 databases, and each database can
have more than 2 billion objects.

Database Files and


Logs

Transaction Logs
history of modifications to the database, and
SQL Server uses it to ensure that a database
has integrity
All changes to the database are first written
to the transaction log and then applied to the
database
If the database update is successful, the
transaction is completed and recorded as
twophase
successful
commit
If the database update fails, SQL Server
uses the transaction log to restore the
database to its original state.
enables SQL Server to restore a database
automatically in case of power failure, server
outage, or other problems that might occur when
you enter a transaction.

Primary data files


Every database has one primary data file. A primary data
file stores data and maintains records of other files used in
a database. By default, primary data files end with the
.mdf extension.

Secondary data files


These files store additional data for a database. Although
not all databases have secondary data files, a single
database can have multiple secondary data files. By
default, these files end with the .ndf extension.

Transaction log files


Every database has at least one transaction log file. This
file contains information necessary to restore the
database. By default, log files end with the .ldf extension.

Database Files

You can use SQL Server Management


Studio to carry out tasks such as the
following:
Viewing database information
Checking user and system databases
Examining database objects

Database
Administration Basics

SQL Server organizes


information using a top-down
hierarchy that starts at the
highest level with server groups
and then moves down to
servers, databases, schemas,
and objects

Viewing Database
Information

General
Provides general database information, such as status, owner,date created, size,
and space available. This page also details the last backup date and collation
setting.
Files
Provides details on the data and log files associated with the database. If the
database has been configured for full-text search, the Use Full-Text Indexing
check box is selected. Catalog files associated with the database are not listed.
Filegroups
Lists the filegroups associated with the database and allows you to add or remove
filegroups.
Options
Provides controls for viewing and managing standard database options and
settings.
Change Tracking
Provides controls for viewing and managing change-tracking settings.

Database Properties

Permissions
Lists users or roles that have specific permissions allowed or denied in
the database. Also allows you to set database permissions for users or
roles.
Extended Properties
Provides controls for viewing and managing extended database
properties.
Mirroring
Provides controls for viewing and managing database mirroring settings.
Transaction Log Shipping
Details the current log shipping configuration (if any) and allows you to
manage log shipping.

T-SQL is an enhanced version of the standard structured query


language that SQL Server uses
In SQL Server Management Studio, access the Query view by
right-clicking the name of a server you have connected to
already in the Object Explorer view and selecting New Query.
sp_helpdb <dbname>
go

Viewing Database
Information Using T-SQL

* System databases are critical to the proper

operation of SQL Server, and an important


aspect of the administration process is backing
up and maintaining these databases.

Checking the System


Databases

Certificates
Constraints
Defaults
Indexes
Keys
Stored procedures and extended stored procedures
Tables

Examining Database
Objects

Database Diagrams Contains visual diagrams of a database and the


information it contains. Use Database Designer to create and manage
diagrams.
Tables Contains system and user tables. System tables are used for many
purposes, including database mail, database maintenance plans,
replication, backup and restore, and log shipping. System tables should not
be modified directly.
Views Contains system and user views. Standard views combine data from
one or more tables to make working with the data easier. Indexed views
have unique clustered indexes to improve query performance. Partitioned
views join horizontally partitioned data from tables on one or more servers.
Synonyms Contains synonyms, which are alternate names for schemascoped objects. Applications can use synonyms to refer to objects in the
database abstractly. You can then change the underlying name of the
object without having to modify the application programming.

Programmability Contains nodes used to represent most programmable


object types and subtypes, including stored procedures, functions,
triggers, assemblies, data types, rules, and defaults. This node also
contains plan guides.
Service Broker Contains Service Brokerrelated objects, including
message types, contracts, queues, services, routes, and remote service
bindings. This node also contains broker priorities.
Storage Contains storage-related objects, including full-text catalogs,
partition schemes, and partition functions. This node also contains fulltext stoplists.
Security Contains security-related objects, including users, roles,
schemas, keys, and certificates. It also contains database audit
specifications.

Broker Priorities
Broker priorities define a priority level and
the set of criteria for determining which
Service Broker conversations to assign the
priority level
Servicer Broker assigns the priority level to
any conversation endpoint that uses the same
combination of contracts and services that
are specified in the conversation priority.

Plan Guides, Broker


Priorities & Full text
stoplists

* You can create plan guides to help optimize

queries that are not performing as expected.


* you can specify the T-SQL statement that you
want to be optimized and either an OPTION
clause that contains the query hints you want
to use or a specific query plan you want to use
to optimize the query

Plan Guides

3 Types of Plan Guides

Full-text stoplists

Creating Databases

Creating Databases

Creating Databases

* Windows collation names have two

components: a collation designator and a


comparison style
* collation designator specifies the alphabet or
language whose sorting rules are applied with
dictionary sorting and the code page to use
when storing non-Unicode character data

Creating Databases

Creating Database
using T-SQL

* USE MASTER
* GO
* CREATE DATABASE Sample
* ON
* PRIMARY
* ( NAME = Sample1,
* FILENAME = "c:\data\sampledat1.mdf",
* SIZE = 100MB,
* MAXSIZE = UNLIMITED,
* FILEGROWTH = 10%),
* ( NAME = Sample2,
* FILENAME = "c:\data\sampledat2.ndf",
* SIZE = 100MB,
* MAXSIZE = UNLIMITED,
* FILEGROWTH = 10%)
* LOG ON
* ( NAME = SampleLog1,
* FILENAME = "c:\data\samplelog1.ldf",
* SIZE = 3MB,
* MAXSIZE = UNLIMITED,
* FILEGROWTH = 5MB)
* GO

Altering Databases

Configuring Automatic
Options

Configuring Automatic
Options

Auto Shrink (auto_shrink)


When this option is set to TRUE, data
and log files are reduced in size and
compacted automatically. When
records are deleted or purged, SQL
Server automatically reduces the size
of data or log files or both file types.

Configuring Automatic
Options

Auto Update Statistics


(auto_update_statistics)
When this option is set to TRUE
(the default), existing statistics
are updated automatically if data
in the related tables changes.

Configuring Automatic
Options

Auto Update Statistics Asynchronously


(auto_update_statistics_async)
When this option is set to TRUE,
queries that initiate an automatic
update of out-of-date statistics do not
wait for the statistics to be updated
before compiling.

Configuring Automatic
Options

SQL Server can use parameters in T-SQL statements


to increase the ability of the relational engine to
match new SQL statements with existing, previously
compiled execution plans.

Parameterization Option
SIMPLE
SQL Server parameterizes very few classes of
queries and disables forced parameterization.
FORCED
any literal value that appears in a SELECT,
INSERT, UPDATE, or DELETE statement
submitted in any form is converted to a
parameter during query compilation.

Configuring
Parameterization

Cursors are used with stored


procedures, with triggers,
and in batch scripts to make
the contents of a result set
available to other statements

Configuring Cursor
Options

Close Cursor On Commit Enabled


(cursor_close_on_commit)
When this option is set to TRUE, open cursors are closed
automatically when a transaction is committed or rolled back.

Default Cursor (cursor_default)


LOCAL,
cursors are created with local scope unless otherwise specified.
As a result, the cursor name is valid only within this scope.
GLOBAL,
cursors not explicitly set to LOCAL are created with a global scope
and can be referenced in any stored procedure, batch, or trigger
that the connection executes.

Configuring Cursor
Options

In SQL Server Management


Studio, you can control the
general state of the database,
including whether the database
is read-only or read-write and
who has access to the
database.

Controlling User Access


and Database State

READ_ONLY,
you can read data but not modify it. You use this option to prevent users from
changing data and modifying database configuration settings.

READ_WRITE,
Normal Mode, which allows the database to be read and modified.

SINGLE_USER,
only the database owner can access the database. You use this option when
you are modifying a database and want to block access to it temporarily.

RESTRICTED_USER,
only members of the db_owner, dbcreator, or sysadmin roles can use the
database

MULTI_USER,
all users with the appropriate permissions to connect to the database are
permitted to use it.

Database State

In SQL Server Management Studio, you


manage recovery settings by using the
Recovery Model and Page Verify settings
on the Options page
In T-SQL, you manage these options by
using the ALTER DATABASE SET RECOVERY
and ALTER DATABASE SET PAGE_VERIFY
commands

Configuring Recovery, Logging, and Disk I/O Error


Checking Options

FULL When recovery is set to FULL, transactions are fully logged


and the database can be recovered to the point of failure or to a
specific point in time using the transaction logs
BULK_LOGGED When recovery is set to BULK_LOGGED, certain
SQL commands are not logged in the transaction log. These
commands include using SELECT INTO and BULK INSERT with a
permanent table, running fast bulk copy, using UPDATETEXT or
WRITETEXT without logging, and using a table load.
SIMPLE When recovery is set to SIMPLE (previously managed using
trunc.log on chkpt), the transaction log can be truncated
automatically. This setting allows the log to be cleared after
transactions have been committed.

Configuring Recovery, Logging, and Disk I/O Error


Checking Options

Disk I/O errors can cause


database corruption problems
and are usually the result of
power failures or disk hardware
failures that occur at the time
a page is written to disk.

Disk I/O Errors

three page verification options to help identify


incomplete I/O transactions caused by disk I/O
errors:
CHECKSUM are used to find incomplete I/O transactions
caused by disk I/O errors. The checksum is computed over
the contents of the entire page and stored in the page
header when a page is written to disk.
TORN_PAGE_DETECTION, a bit is reversed for each 512byte sector in an 8-KB database page when the page is
written to disk
NONE, future data page writes will not contain a
checksum or torn page bit, and pages will not be verified
at read time, even previously written pages that contain a
checksum or torn page bit.

Disk I/O Error

Вам также может понравиться