Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Guide
SQL Server 2005:
Implementation and Maintenance
Table of Contents
Installing SQL Server 2005 ....................................................................................................................... 2
Configuring Log and Data Files ................................................................................................................. 9
Creating Partitions................................................................................................................................. 12
Configuring Database Mail..................................................................................................................... 13
Recovery Models ................................................................................................................................... 17
Configuring Server Security Principals.................................................................................................... 18
Configuring Database Securables........................................................................................................... 20
Configuring Linked Servers .................................................................................................................... 22
Creating Indexes.................................................................................................................................... 24
Working with T-SQL ............................................................................................................................... 28
Implementing Views .............................................................................................................................. 31
Working with XML Structures ................................................................................................................ 34
Creating Functions, Stored Procedures, and Triggers ............................................................................. 42
Working with Flat Files .......................................................................................................................... 47
Backing Up and Restoring Databases ..................................................................................................... 52
Moving Databases ................................................................................................................................. 61
Using Transact-SQL to Manage Databases ............................................................................................. 64
Automating Tasks with SQL Server Agent............................................................................................... 69
Monitoring and Troubleshooting SQL Server Performance .................................................................... 78
Implement Database Mirroring ............................................................................................................. 90
Implement Log Shipping ........................................................................................................................ 95
Managing Replication ............................................................................................................................ 99
Working with Service Broker ............................................................................................................... 125
Creating Full-Text Catalogs .................................................................................................................. 130
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 2
Once the prerequisites are installed, a System Configuration Check is performed. This is simply a process
that checks your system prior to SQL Server installation to prevent any potential installation problems.
After you click “Next”, you are prompted to enter the name and company fields (registration
information). Select “Next” to pick which components to install.
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 4
Tip: if you want to install the AdventureWorks database and Books Online: Choose Advanced, and elect
to download and install the SQL Server Books Online, Sample Databases, and Sample Code locally. It is
likely you will want all 3 choices anyway, so now is a good time to install them.
After selecting “Next”, you are required to name the database instance. Since you can install multiple
instances on the same physical server, every name must be unique. There can only be 1 default name
per server (which is your computer name), but
there can be multiple named instances.
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 5
There are two types of Authentication in SQL Server 2005: Windows Authentication Mode and Mixed
Mode.
The next step is self-explanatory, and can enable the submission of Error Reports and Feature Usage:
Collation settings relate to how SQL Server handles non-Unicode data in relation to sorting and
comparison. Generally, it is best to
leave the default settings as-is.
Exceptions to this rule are when the
primary language differs from the
computer SQL Server is being
installed and the SQL Server instance.
Another exception is when the
instance will replicate to other
instances with different languages.
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 7
Post-Installation
After installation, you should download the latest service packs/updates for SQL Server. This update is
especially important if running Vista, since there are compatibility issues with SQL Server unless running
with SP2 or higher.
http://technet.microsoft.com/en-us/sqlserver/bb426877.aspx
After updating SQL Server, you can also install optional databases. The two most popular databases,
referenced in most any SQL Server book, are the Northwind and Pubs databases. To download them,
use the following link: http://www.microsoft.com/downloads/details.aspx?FamilyId=06616212-0356-
46A0-8DA2-EEBC53A68034&displaylang=en.
Once you download and this .msi, you will see a set of scripts in
whichever drive you chose to use. After double-clicking these files, they
will load in SQL Server Management Console. To create the databases,
simply execute the scripts. To verify that the installation completed
successfully, right-click Databases in the Object Explorer and select
“Refresh”, then expand the databases item. You should see the two
databases listed:
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 8
You can upgrade from SQL Server 7.0 Service Pack 4 and SQL Server 2000 Service Pack 3 (direct
upgrade from SQL Server 6.5 is not available). You can either perform an in-place upgrade (immediately
overwrites the previous version of SQL Server) or side-by-side migration (install SQL Server 2005 while
retaining the old version).
In-Place upgrades should only be performed if the resources to host multiple database environments
are not available. Using side-by-side migration allows you to verify and test the new version of SQL
Server before moving the old database files over.
All of these methods are discussed in greater detail later in this study guide.
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 9
Data Files
Data Files can be further broken down into two separate types:
2. Secondary Data Files are optional and user-defined. These contain objects and user data. When
administering secondary data files, you can place secondary files on different physical disk drives
for a boost in performance. The “suggested” extension for secondary data files is “.ndf”. Each
database can have zero or more secondary data files.
Best Practice:
To reduce disk access contention, you should place all data and objects in secondary files and keep just the database catalog in the primary file.
Log Files
You must have at least a single transaction log for every database, but you are allowed multiple log files
per database to provide for faster recovery. The “suggested” extension for log files is “.ldf”.
Filegroups
A filegroup is simply a logical structure that allows the grouping of data files for management of multiple
files as a single logical entity. For a performance boost, you can divide database objects across several
filegroups, and place the filegroups on different disk subsystems.
Primary Filegroup: contains the primary data file and any secondary data files that aren’t
explicitly stored in another filegroup.
User-defined Filegroup: Created to group secondary files, assign database objects to groups.
To edit a filegroup:
To edit a filegroup, specify the database you
want to alter, issue the “ADD FILE”
command, specify parameters like you would
normally, and then specify to which filegroup
you want to add the datafile to.
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 11
Using RAID subsystems provides for improved performance, fault tolerance, and more storage capacity.
- RAID 0: Disk striping, best performance for read/write operations; no fault tolerance.
- RAID 1: Disk mirroring, provides fault tolerance; improves read performance, but can degrade
write performance.
- RAID 5: Disk striping with distributed parity. Improved performance over RAID 1, but more
expensive.
- RAID 10 (RAID 1+0): A mirror of a striped set (combines both speed and fault tolerance)
Creating Partitions
Partitioning allows you to split a table across multiple filegroups, depending on how you configure the
partitions. Partitions allow you to place a subset of a table/index on a filegroup. This creates more
efficient disk I/O, and provides for easier management of very large tables.
Difference in RANGE LEFT and RANGE RIGHT (using 1000 and 2000 as example values):
RANGE LEFT = -infinity to 1000, 1001 to 2000
RANGE RIGHT = -infinity to 999, 1000 to 1999
Querying Partitions
SQL Server allows you to query partitions so that you can determine the partition number that a
particular value would be placed in.
This would return the partition number that the (value) would be placed in.
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 13
As an alternative to enabling Database Mail, you can execute T-SQL code, or more specifically, a stored
procedure. Ensure you are logged in as a sysadmin, and run:
The second step in configuring Database Mail is to ensure that Service Broker is enabled in the msdb
database (enabled by default). To enable this, stop the SQL Server Agent and run the following:
The third step is necessary only if the SMTP server requires authentication. If this is the case, you need
to ensure that the SQL Server service (default) account credentials can access the server.
4. Logging and Auditing: Log information is stored in the host database (msdb). Query the
sysmail_event_log view to see the log.
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 15
To verify that Database Mail is setup correctly, go back to the Management Node and right-click
Database Mail. Select “Send Test Email…” Specify the recipient’s email address, and click “Send Test
Email”. You should receive an email similar to the following:
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 16
To view the Database Mail log, simply expand the Management Node, right-click Database Mail, and
select “View Database Mail log”:
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 17
Recovery Models
A recovery model is a configuration option that controls how the transaction log records actions, and
has both data recovery and performance implications. There are three recovery models for databases:
1. Full Recovery Model: All operations are logged, log is never truncated. Allows you to restore a
database to a point in failure.
2. Simple Recovery Model: Most operations are minimally logged; truncates log at each
checkpoint. Cannot back up or restore the transaction log. Not appropriate if you can’t afford
loss of recent changes.
3. Bulk-Logged Recovery Model: Minimally logs bulk operations (SELECT INTO, BULK INSERT);
intended to be used only during large bulk operations.
In SSMS, expand the database node, right-click the database you want to configure, select properties.
Then, select the Options page:
SQL Server authenticates all connections, and all connections must specify the mode of authentication
as well as the credentials.
Authentication Modes
Windows Authentication: Uses Active Directory accounts; recommended authentication mode.
Mixed Mode: Windows logins and SQL Server logins are used. Used when non-Windows users require
access.
Once you make a change to the authentication mode, you must click ok and restart the server. To
restart the server, right-click it and select “restart”.
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 19
To configure in SSMS:
Expand the security node, right-click logins, select New Login.
To configure in T-SQL:
Windows Login: CREATE LOGIN [domain\User] FROM WINDOWS
SQL Server Login: CREATE LOGIN [loginname] WITH PASSWORD= ‘password’
To change a password:
ALTER LOGIN [loginname] WITH PASSWORD= ‘password’
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 20
If a login name is not specified, SQL server will attempt to map a login name that is the same as the user
name.
To configure in SSMS: Expand the Databases Node, Expand Security, Expand Users. Right-click on Users,
select “New User…”
By default, if a login is not mapped to any database users, SQL Server looks for the GUEST account. All
databases have a GUEST user, but by default it is not allowed to connect to a database.
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 21
Orphaned Users
When database users exist, but there is no associated login mapped to that user, the database user is
called an orphaned user.
Database Roles
Much like fixed login roles, there are also predefined Database Roles, which eliminate the need to create
a new database user for every login you require.
T-SQL Syntax:
CREATE ROLE [roleName]
SSMS: Expand the Databases node, expand the Security node, right-click Roles, “New Role…”
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 22
3. Click OK, and close the window. Security configuration will be done shortly.
4. To validate that the connection is configured correctly, right-click the newly created linked
server, and select “Test Connection”.
You should see a similar popup
message as what is shown below:
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 23
When accessing a linked server, the external server often needs to authenticate the user that attempts
to access it. There are three security models that SQL Server has available:
1. Self-mapping: SQL Server attempts to connect to the external data source using the current
user’s login credentials. The same username/password must exist on the external system. This is
the default setting.
3. Remote Credentials: Allows you to map local logins to remote logins on the external data
source.
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 24
Creating Indexes
Indexes are useful in SQL Server if they can locate data quickly, regardless of the volume of data. A real-
world example is a large book that contains an index in the back, which lists topics and their
corresponding page numbers. Rather than scanning the book page-by-page, word for word, the use of
an index allows you to alphabetically search for a topic and the associated page number.
Databases are very similar to this analogy; rather than scanning an entire table for information, indexes
sort this information in some fashion to allow much faster data retrieval.
Index Structure
Indexes utilize what is known as a “B-Tree” structure (Balanced Tree).
As a general rule, all tables should have clustered indexes (and each table can only have 1 clustered
index). The columns used for that index are known as the clustering key. In a clustered index, the leaf
levels contain the actual data pages.
Creating an Index:
CREATE CLUSTERED INDEX index_name
ON dbo.TableName(ColumnName);
Disabling an Index:
ALTER INDEX index_name
ON dbo.TableName(ColumnName)
DISABLE
Rebuilding an Index:
ALTER INDEX index_name
ON dbo.TableName(ColumnName)
REBUILD
After creating clustered indexes, you can create multiple (up to 249) nonclustered indexes on the table,
since nonclustered indexes do not force a sort on the table. In a nonclustered index, the leaf nodes
contain pointers that reference the actual datapages.
It is important to balance the number of indexes with the amount of index maintenance. It is important
to understand that for every write to a table requires one write to the index, which could quickly get out
of hand if the table has many indexes.
In general, indexes improve query performance but degrade performance on all data-manipulation
operations.
Included Columns: In SQL Server 2005, columns become part of the index at the leaf level only (values
do not appear at the root/intermediate levels). This helps save space by not consuming any of the 900-
byte limit for an index.
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 28
The following will return any matches that contain the exact word “Help”:
SELECT *
FROM Person.Address
WHERE CONTAINS (AddressLine1, ‘Help’)
The following will return any matches that contain the word “Help”, and the * denotes that Help can be
a prefix to another word (such as “Helpme”):
SELECT *
FROM Person.Address
WHERE CONTAINS (AddressLine1, ‘”Help*”’)
The following will return any matches that contain the word “Help” in any context (beginning or end of a
word):
SELECT *
FROM Person.Address
WHERE FREETEXT (AddressLine1, ‘Help’)
You can also use table-valued versions of CONTAINS and FREETEXT, which include a RANK column that
lists matches by order of relevance (higher values = more relevant):
CONTAINSTABLE and FREETEXTTABLE
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 30
This command returns a random subset of datapages equaling the percentage you specify:
SELECT *
FROM TABLE
TABLESAMPLE (20 PERCENT)
Note: When using a user-defined function (UDF), UDFs must be scoped by the name of the schema the
UDF belongs to. Example: dbo.functionName(parameters)
To query CLR User-defined types (UDTs), you can use the ToString method to return the values as a
string. Example: SELECT ContactNumber.ToString() FROM ContactInfo
Implementing Views
A view is nothing more than a SELECT statement that is assigned a name and is stored as an object in
SQL Server. Views act as virtual tables and provide several benefits: they give developers a standard way
to execute queries, they provide an additional level of security, and they provide for performance
improvements for certain types of queries.
In general, you should only create views when there is a clear advantage of having one. By creating
unnecessary views, you simply take up space and administrative time managing the views.
To Create a View
There are three different options you can specify in the WITH clause:
1. ENCRYPTION specifies that SQL Server encrypt the definition of the view (not visible to anyone,
so keep the original source somewhere for later reference).
2. SCHEMABINDING prevents you from dropping any tables, views, or functions referenced by the
view without first dropping the view.
3. VIEW_METADATA returns metadata about a view to client-side data access libraries.
After a view is created, it can be used like any other table in a database. Keep in mind that a view does
not have any data in it (the query optimizer substitutes the reference with the definition of the view).
Ownership Chains
Since views can reference various objects, there is always a potential for permission conflicts. When an
object is accessed through a chain, SQL Server first compares
the owner of the object to the owner of the calling object. This
is the previous link in the chain. If both objects have the same
owner, permissions on the referenced object are not evaluated.
To restrict the changes that can be made via a view, use the WITH CHECK OPTION. This allows only
changes to data that are visible through the view. For example, suppose we have a table that allows any
value for a column called Salary, but the view only contains Salary equal to or greater than $40,000. A
new employee could be added with a salary of $35,000, but the view would not show that new
information. By using the WITH CHECK OPTION, that insert would not be allowed via a view.
Unlike typical views, indexed views are directly used by the query optimizer (data is returned directly
from the view, rather than substituted).
To see all of the views in a specific database: Expand the Database | Expand the Views Folder
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 33
XML Documents are XML structures that have a root, or parent, node.
XML Fragments are XML structures that don’t have a root, or parent, node.
Additionally, SQL Server can store XML data in two ways: as XML in a text column, and as an XML data
type column (new to SQL Server 2005).
Pros Cons/Limitations
Details such as comments and white space are Coding complexity + higher maintenance cost
preserved.
Reduces processing workload (processing of Manipulation, extraction, modification at the
XML occurs on a middle tier) node level is not possible
Best performance for document-level Searching XML data always requires reading
insertion/retrieval. the entire document (interpreted as text)
Does not depend on database capabilities XML validation, well-formedness, and type
checking must be performed at a middle tier
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 35
Pros Cons/Limitations
Same query processor/optimizer are used for Whitespace, comments, and other nondata
XML queries elements are not retained
Data stored/manipulated natively as XML Max storage is 2 GB
Document order/structure preserved Max allowed node depth is 128 levels
Performance improves for data-retrieval
This would return (once for EVERY row in the result set):
<XMLFragmentName>
<Column1>Value</Column1>
<Column2>Value</Column2>
<Column3>Value</Column3>
</XMLFragmentName>
Note that elements are ordered based on the table, NOT the columns (Column4 is listed last in the
SELECT statement, but comes before Column2/3 in the XML result format).
Note that since “@StartDate” does not specify where to locate the attribute, it is added to the context
node <ForEachRow>.
Parent must be the second column in the result set, indicates the node parent in the XMl structure.
SELECT 1 as Tag,
NULL as Parent,
Column1 as [EName!1!AName1],
Column2 as [EName!1!AName2]
NULL as [EName!1!AName3]
FROM Table1
UNION ALL
SELECT 2 as Tag,
1 as Parent, Column3
FROM Table2
FOR XML EXPLICIT, ROOT(‘RootName’)
If the FOR XML EXPLICIT is not specified, the Universal Table Structure is returned instead.
XML Methods
The XML data type has several methods used for querying data, extraction, modification, and shredding
into relational data. The methods are:
1. query () – Executes a query over XML and returns untyped XML result
2. value () – Extracts a value of SQL type from an XML instance
3. exist () – Determines whether a query returns a result
4. modify () – Used in updating/alter the content of an XML document
5. nodes () – Helps shred XML into relational data.
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 39
It is important to note that XML methods are considered subqueries and can not be used in the PRINT
statement or within a GROUP BY clause.
Returns:
<ElementName>Value1</ElementName>
<ElementName>Value2</ElementName>
Returns:
Column1 Value
1 Name1
2 Name2
Note that the [1] specifies the ordinal, so if there were more than 1 attribute listed for the element, only
the first one would be used ([2] would pick the seconds attribute, etc).
You also specify where to insert the data if using the insert statement (before, after, into as first, into as
last). This tells SQL where to make the insertion once it finds the element/attribute.
UPDATE Table
SET EmpName.modify (‘replace value of (/RootName/@LastName)[1] with
“Smith”’)
WHERE EmployeeID = ‘FE122’
Returns:
EmployeeID Name
1 Sue
2 Mary
3 Joe
Primary indexes are the first index placed on an XML column. When using a primary index, you avoid
needing to shred the XML at runtime.
Secondary indexes (PATH, VALUE, or PROPERTY) can be placed once a primary index is created. You
must specify one of these directives and associate the secondary index with a primary index.
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 41
VALUE is useful for queries that search for specific values without knowing the element/attribute they
would be located in.
PROPERTY is useful in queries that retrieve more than one value from individual XML instances.
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 42
Implementing Functions
Functions are included in SQL Server and provide you the ability to execute some common functionality
throughout the application. For example, the function GetDate() outputs the current data. In SQL Server
2005, you have the ability to create your own pre-defined functions (UDFs).
UDFS can be broken down into two separate categories: scalar functions and table-valued functions.
Scalar Functions accept 0 or more input parameters and return a single value (of the desired type);
table-valued functions return a result set (table data type). There are also Inline UDFs, which are simply
functions that consist of a single statement (can be either scalar of table-valued).
Benefits of using UDFs include reduction of network traffic, fast execution, modular programming
(update once and changes applied wherever the function is referenced), and UDFs can be used within
queries/select statements.
To view functions in SSMS: Expand Databases Node | Expand a database | Expand Functions node
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 43
Stored Procedures can contain almost any T-SQL command that would normally be used (with the
exception of USE, SET SHOWPLAN_TEXT ON, and SET SHOWPLAN_ALL ON), and can modify data, return
scalar values, or return result sets. Additionally, user permissions can be assigned to stored procedures
without needing to grant access to the actual data. Permissions to stored procedures are limited to the
operations the stored procedure(s) are defined to perform.
1. ENCRYPTION tells SQL Server to encrypt the content of the stored procedure.
2. RECOMPILE forces SQL Server to recompile the stored procedure every time it is executed.
3. EXECUTE AS provides a security context for the sproc (uses the permissions of an account).
To call Stored Procedures, you should always specify the EXECUTE command followed by the stored
procedure name and any necessary input parameters. The only exception to this rule is if the statement
is the first one in a batch.
Implementing Triggers
A trigger is a T-SQL or CLR batch of code that automatically runs in response to an event within the
database. The two triggering events are referred to as Data Manipulation Language (DML) and Data
Definition Language (DDL) events.
DML Triggers
DML triggers are not standalone objects, thus they cannot be executed directly. By attaching a DML
trigger to a specific table or view, you can automate certain tasks based on pre-defined events. These
events can include INSERT, UPDATE, and DELETE activity.
Exceptions on Triggers
Databases can’t be created, altered, dropped, backed up, or restored in triggers
Structural changes can’t occur on the table that caused the trigger to fire
Triggers only fire in response to logged operations
Triggers can’t run on system tables or DMVs
There are two tables that are associated with triggers: INSERTED and DELETED. These are viewed using
the OUTPUT clause.
INSERTED stores each row that was inserted into a table (no rows in DELETED).
DELETED stores each row that was deleted from the table (no rows in INSERTED).
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 45
During an UPDATE, INSERTED stores the “after” image, DELETED stores “before” image.
Syntax to Create a DML Trigger:
CREATE TRIGGER [TriggerName]
ON [Table | View]
WITH [Option]
{FOR | AFTER | INSTEAD OF}
[INSERT, UPDATE, or DELETE]
AS
The ON clause specifies the table or view that the trigger is created against. If the view or table is
dropped, the trigger is also dropped.
After the AS clause, you specify the code to be executed if the trigger is fired.
Recursive/Nested Triggers
Recursive Triggers are triggers that cause themselves to fire (the actual trigger itself sets off an action
that would fire the trigger, thus creating an infinite loop). This is disabled by default
(RECURSIVE_TRIGGERS database option is set to OFF).
Nested Triggers are triggers that cause indirect recursion. For example, an event fires a trigger, which
performs an action that cause another trigger to fire, and that trigger causes the original trigger to fire
again (and the loop continues). This is disabled via the NESTED TRIGGERS option set to 0.
DDL Triggers
The main purpose of DDL triggers are to audit and regulate actions performed on a database. For
example, when the trigger is fired, you can create a table and log the attempted action in a new table.
EventType can be a wide range of events; to view the entire list, search for “Event Groups for Use with
DDL Triggers” in BOL. Examples of events include CREATE TABLE, ALTER TABLE, and DROP TABLE.
Source File Location: The source of the data is important since it is a major factor in the
speed/complexity of a data import. It is important to remember that the process is only as fast as the
slowest component involved.
Import Mechanism: Mechanisms include bcp, BULK INSERT, OPENROWSET, or SSIS. Each has their own
strengths/weaknesses.
Data Destination: Includes which database server, the database, and the data structure. What level of
uptime is needed? Which recovery model is being used? Does the table have clustered/non-clustered
indexes, constraints, or triggers? Is the table a source for replication?
General Syntax
bcp databaseName..tableName in “FlatFileDirectoryLocation” -T
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 48
-T specifies that the import uses a trusted connection (use the permission of the Microsoft user
executing the command)
TABLOCK hint = bcp performs a table lock and allows the bulk-logged recovery model to use minimal
logging.
ORDER hint = specify the records in the data file to be ordered by certain columns (useful only if the sort
matches the destination’s clustered index)
Note: Minimal security permissions required are SELECT/INSERT. To suspend constraint checking, trigger
execution, etc, requires ALTER DATABASE.
BULK INSERT
BULK INSERT is the T-SQL “equivalent” to the bcp utility. BULK INSERT runs inside the SQL Server process
space and is executed from a query window/query batch. All of the caveats are the same, and BULK
INSERT uses almost all the same parameters (but is less cryptic).
Parameters
FIELDTERMINATOR – specifies the column delimiter (equivalent to –t)
FIRSTROW – specifies the first row in the flat file that will be inserted into the table (equivalent to –F)
Note: Minimal security permissions required are INSERT and ADMINISTER BULK OPERATION permission.
To suspend constraint checking, trigger execution, etc, requires ALTER TABLE permissions.
This bulk imports the contents of ‘C\Location\xml.txt’ file as a single_blob and inserts the blob into the
XmlCol of a single row in the documents table.
To start in SSMS:
Expand the Databases Node | Tasks | Import Data/Export Data
This screen allows you to choose the datasource, server name, and method of authentication to use for
the source of the import/export.
Next, you specify which tables and views are used for the import/export. This can be done by copying all
the data from the tables/views, or writing a query to specify and restrict only certain information.
The next screen (not shown) allows you to Save the package as well as immediately execute it. By saving
the package, you can use it as a base for other packages, use it to document data, and reuse.
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 52
Permissions
Using the principle of least privilege, the SQL Service account requires read/write permissions for any
directories/devices involved in the backup/restore process. Additionally, a user requires the
db_backupoperator role to backup a database, log, or checkpoint (no other access is required).
Backup Types
There are four main backup types:
1. Full Backups
2. Differential Backups
3. Transaction Log Backups
4. Filegroup Backups
Additionally, you can perform Mirrored Backups and Partial Backups (explained later).
Full Backups
These capture all of the data stored in the database. This can be used by itself to restore the entire
database up until the point that the full backup is completed. The internal process involved in a full
backup includes:
This process ensures that transactions aren’t left out during the backup process (which prevents data
inconsistency).
Differential Backups
These capture all of the changes to the database since the last full backup. Differential backups are used
in conjunction with full backups; if a full backup does not exist, you can not perform a differential
backup.
Filegroup Backups
Rather than backing up entire databases, you can backup a database over time by filegroup. This is
especially useful for very large databases that otherwise could not be backed up over the course of a
single night/maintenance window.
Filegroup backups do not work with the simple recovery model. Also, you can perform full or differential
backups against filegroups.
Full:
BACKUP DATABASE [DatabaseName] FILEGROUP = ‘FilegroupName’ TO DISK =
‘[DirectoryOrDeviceName]’
Differential:
BACKUP DATABASE [DatabaseName] FILEGROUP = ‘FilegroupName’ TO DISK =
‘[DirectoryOrDeviceName]’ WITH DIFFERENTIAL
Mirrored Backups
To specify additional locations to backup the database (mirror the copies), use the following syntax:
MIRROR TO [BackupDevice]
WITH FORMAT
You can do this with up to 3 mirrored locations (or 4 locations total). The WITH FORMAT is required
when performing a Mirror backup, which writes a new media header to each media set and overwrites
any previous header.
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 54
Partial Backups
To perform a backup that skips and read-only filegroups, use the following T-SQL:
BACKUP DATABASE [DBName] READ_WRITE_FILEGROUPS
TO DISK= 'Location\File.bak'
Destination specifies a backup device. To create a Backup device, refer to the instructions on the next
page.
Assign the device a name, and specify whether it is a tape drive or a file. Tape will be grayed out if there
are none present.
Going back to the backup dialog box, select “Add” in the Destination section to specify the backup
device to use for the current backup:
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 56
There are also a good number of options that you can choose while creating the backup:
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 57
Options include the ability to append to a media set or overwrite a media set; verify a backup when it is
complete; and unload/rewind a tape after backup is complete.
Validating a Backup
SQL Server 2005 provides the ability to check the status of a backup file. This should be done periodically
to ensure that the backup files are created correctly.
Restoring a Database
Full/Differential:
RESTORE DATABASE [DatabaseName] FROM DISK = '[Location]'
WITH REPLACE, NO RECOVERY
Log Restoration:
RESTORE LOG [LogName] FROM DISK = '[Location]' WITH RECOVERY
When restoring filegroups, you do not need to specify which filegroup is involved in the restore (SQL
Server does this automatically).
Partial Restore: Ability to partially restore a database while the rest of the database is open for
requests. It is NOT possible to partially restore to a certain point in time (must be the most current
data).
If corruption occurs on a data page, must either restore a backup or use the new feature PAGE_VERIFY
CHECKSUM.
This is off by default since there is a small amount of overhead for reads/writes. This tells SQL Server to
calculate a checksum for the page on every read/write and verifies that the page is not corrupt. If a page
is found to be corrupt, the transaction is rolled back and logged in msdb.
Media Errors
If restoring from damaged backup media, you can specify to skip any damaged sectors and continue
with the restore process. This places the database in an emergency state, but allows you to issue SELECT
statements and verify the integrity of the database. You can then change to state of the database if
deemed intact and operational.
To Restore a Database:
Right-Click the database | Tasks | Restore | Database
Specify the database you will be restoring, the source of the restore, and the backup sets to restore (the
example uses the full backup performed earlier in this demo).
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 59
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 60
One point of interest is the Recovery state; this is where you specify what mode the database is in after
the restore (WITH RECOVERY, WITH NO RECOVERY, or WITH STANDBY).
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 61
Moving Databases
To move or copy a database, you have the option of detaching/attaching an existing database from one
location to another (on the same or different server). Once a database is detached, it is no longer
accessible from SQL Server, but no loss of data occurs.
The process of moving a database involves detaching an existing database, copying or moving the
datafiles to a new location, and then reattaching the database to point to the new location.
From SSMS:
Right-click database | Tasks | Detach
You can now choose to drop all current connections, as well as Update Statistics. Click “OK”.
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 62
At this point, you would navigate to the directory that held the datafiles and then move/copy them to
the location you want the database to be stored in.
Then, in SSMS:
Right-click Databases | Attached
On the window that appears, select “Add” and then navigate to the new directory location. Select the
.mdf file and click “OK”:
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 63
All of the file types and locations are listed now. Select “OK” to begin attaching the database.
Index Fragmentation: Indexes lose their effectiveness over time due to fragmentation caused by
insertions/deletions of data in the index. The query optimizer may select indexes that have become
fragmented, which would decrease the query’s performance and defeat the purpose of using the
optimizer.
Internal Fragmentation is when pages are not as full as they could be (due to DELETE statements). This
decreases disk space efficiency and requires more pages for the same number of index rows.
Page Splits occur when SQL Server needs to place additional rows of data on an index page. The data is
logically ordered here, but not necessarily physically ordered. This is what is known as external
fragmentation.
External Fragmentation is always undesirable, whereas some level of internal fragmentation is desirable
for highly transactional databases.
To detect index fragmentation, utilize the sys.dm_db_index_physical_stats DMF. When the value is >
10, you have external fragmentation. In the avg_page_space_used column, if the value is < 75 you have
internal fragmentation.
You can either reorganize or rebuild your indexes. Reorganizing an index rearranges the physical leaf-
level pages to match the logical order of the leaf nodes. Rebuilding an index involves dropping the index
and then creating a new one.
Rebuilding an index also allows you to use the ONLINE option to make the table and associated indexes
available during the rebuild. Additionally, you can use the ALL option to rebuild all indexes on a table be
rebuilt with a single command.
Managing Statistics
Statistical information is generated by SQL Server regarding the distribution of values in a column. This
statistical data is used by the query optimizer to determine whether using an index would improve
query performance. The optimal query plan could be inaccurate if the statistics aren’t current.
Manually creating statistics can be done using the following T-SQL command:
You can also replace “CREATE” with UPDATE or DROP to perform the appropriate action.
Shrinking Files
Certain operations in SQL Server such as large deletions or data loads might leave database files larger
than they typically need to be. A DBA can shrink files to remove any unused pages, and in turn regain
disk space.
When shrinking the transaction log, the size of the virtual log files contained within the transaction log
determine the reduction in size that is possible. In other words, you can’t shrink the transaction log to a
size smaller than the virtual log file.
Note: Only use the repair options as a last resort; the recommended action to take is to perform a
restore from backup to correct any errors. Repair operations do not take into consideration any
constraints that are placed on the data.
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 69
Jobs: Series of actions that contain steps/actions to be performed within SQL Server (T-SQL command,
OS command, vbscript, etc). Ability exists to run jobs locally or on multiple remote servers.
Schedules: Individual objects that lay out when a job runs. In SQL Server 2005, you can create a single
schedule and then apply it to multiple (different) jobs, rather than creating a separate schedule for each
job.
Alerts: Automatic response to an event (SQL Server error, performance conditions, WMI events). Once
alerts are fired, messages can be sent to operators and/or a job can run.
Operators: A specified contact, which is used for notification within SQL Server, database mail, etc.
General options include configuring SQL Server to restart if it stops unexpectedly, to restart SQL Server
Agent if it stops unexpectedly, the directory which the error log is stored, and which user to send a “net
send” message if an error occurs.
*Note: Refer to the Database Mail tutorial for setting up Database Mail.
Once SQL Server Agent and Database Mail are enabled/configured, the next logical step is to create and
configure Operators. Operators will receive notifications if alerts are triggered.
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 71
Assign a name for the operator, specify the email address, the net send address (send a message across
the network to the specified machine and relies on the messenger service), pager email name, and the
pager schedule.
Creating Alerts
To create an alert, in SSMS:
Expand SQL Server Agent | Right-click Alerts | New Alert…
Give the alert a name, assign it a type (SQL Server event alert, performance alert, WMI event), assign it a
severity level, etc.
The Notify Operators checkbox allows you to specify which operator(s) to send a message to if this alert
is triggered. Creation/viewing of operators is also available from this menu.
On the Options page, you can elect to send the alert error text within the email, pager, and/or net send
message. You can also add additional text if you choose.
Creating Jobs
Jobs are the actual tasks that are performed within SQL Server Agent. To create a new job, in SSMS:
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 74
In the General page, you specify the name of the job, who the owner of the job is, the category (to easily
organize jobs; this is especially useful if you manage a large number of jobs), and a brief description of
what the job does. The Enabled
checkbox provides an easy way to
enable/disable jobs.
Here you can assign the step a name, the type of step (many options including vbscripts, OS commands,
replication commands, etc), which database to run the step in, and then the specific command
(generally T-SQL). Click on Parse to check the syntax of the Command.
In the Notifications page, you can specify which type of message is to be sent once the job completes.
The options are as listed below:
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 78
-Or in SSMS-
This dialog allows you to specify the name of the trace, which template to use, have the results saved to
a file or to a table, and a specific time to stop the trace. When saving to a file, you can adjust the
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 79
maximum size of the trace so that they are manageable sizes. This uses file rollover, where once one
trace file meets the max size, a new file is created.
If you click the Events Selection tab, you see the following:
This screen allows you to modify and fine-tune exactly what it is you will be performing a trace against.
The screenshot above is the default settings while using the Tuning template.
To create a filter, click the Column Filters button. Simply select the column you want to filter on and
enter the appropriate filter criteria. If a column is being filtered in any way, the blue filter icon will
appear to the left of the column name (shown below).
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 80
To run the trace, simply click the run button. Once a trace is running, you can control the trace by
pausing/stopping the trace. If pause is selected, the data is no longer gathered within Profiler until it is
resumed.
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 81
Once this is generated, you can now store the trace as a stored procedure that SQL Server Agent calls to
periodically run the trace.
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 82
In order to capture data and use it for later analysis, you need to create a counter log. To create a
counter log, launch System Monitor and select Counter Logs. If you right-click Counter Logs | New Log
Settings and enter a name for the log, you can define the counter log next. You can also add objects
(CPU, Memory, Disk, etc) and counters (% utilized, % free, etc).
DTA utilizes what is known as a workload file as an input and then tests that input using the query
analyzer to identify areas of improvement. This workload file can be in several different formats
including a trace file, a trace table, or a T-SQL script (the most common is the trace file).
To launch DTA:
In SSMS, select Tools |Database Engine Tuning Advisor
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 84
Once DTA is open, you must specify a workload to analyze, as well as the database to be used for the
analysis:
Now that a workload file is specified, select the Tuning Options tab to customize what you are tuning:
To limit the time DTA performs its tuning, select the Limit tuning time checkbox and specify a time limit.
*Note: DTA can have a performance impact on the system and take a very long time to complete.
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 85
If you select the Advanced Options button (not shown), you will see the following dialog box:
Here you can define the maximum size that the recommendations can be, and also specify whether or
not to use online recommendations.
As Analysis is in progress, you can view details on each step. You will notice while running analysis that
the longest step is the Performing Analysis action.
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 86
Once Analysis is complete, you can view one of several different reports to view any of the DTAs
recommendations. Once the analysis session is complete, you can save the recommendations for later
reference.
Additionally, you can apply the recommendations to a database immediately. This is not recommended
since changes are not integrated with your source code control system, and recommended changes
should be first tested in test/implementation servers.
Prefix Purpose
dm_db Provides general database stats
dm_exec Query stats
dm_io I/O stats
dm_os Hardware-level information
Database Statistics
db_index_usage_stats (DMV): Find the # of seeks, scans, lookups, or updates in an index
db_index_operational_stats (DMF): Determine how heavily a index is being used (modifications, scans,
and lookups)
db_index_physical_stats (DMF): Returns row size and fragmentation information
sys.dm_db_missing_index (DMV): Set of views that track the number of timesan index could have been
used, and helps identify areas that could benefit from indexes.
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 87
Query Statistics
These statistics are broken down into 2 categories: connections to the instance and queries running
from within the engine.
I/O Statistics
io_virtual_file_stats (DMF): breaks down the physical I/O written to each file by reads, writes, bytes
read, and bytes written. This ultimately helps identify if portioning/filegroups are necessary for
increased performance.
io_pending_io_requests (DMF): Contains a row for every request that is waiting for an I/O operation to
complete
Hardware Statistics
os_performance_counters (DMV): Provides all the counters that a SQL Server instance exposes in an
easily manipulated result set.
os_wait_stats (DMV): Displays how long processes have to wait before resources are allocated to them.
Correlating Performance and Monitoring Data
The true value in using all of the monitoring tools previously mentioned is when you use them in
conjunction with one another. These tools allow you to monitor and troubleshoot different layers of SQL
Server, so it is necessary to understand how to correlate that information effectively.
Examples:
To correlate System Monitor data with a Profiler trace, configure a trace that includes the “Start Time”
data column. Once the trace is done, select File | Import Performance Data in Profiler.
To correlate DMVs/DMFs with Profiler traces, you could identify queries that take several seconds and
then further break down their execution using DMVs/DMFs. For example, you could analyze
sys.dm_db_index_physical_stats to find that the optimizer is not using expected indexes because they
have become heavily fragmented.
Locking: Locks can be applied at a row, page, or table level. There are three types of locks:
1. Shared Locks: Allows shared access to the data; unlimited number of connections is allowed to
read the data. Data can’t be modified until the shared lock is released.
2. Exclusive Lock: Allows only a single connection to access the locked data. Used to ensure other
users can’t view the data until the modification is made.
3. Update Lock: Lock begins as a shared lock until the rows that need to be modified are located,
at which point turns into an Exclusive Lock. This “promotion” to an exclusive lock is the most
common cause of deadlocks.
Isolation Levels: In SQL Server 2005, there are five different levels which specify how transactions are
handled and the duration of the locks (described in the table below):
Isolation Level Description
READ UNCOMMITTED Read data that hasn’t been committed.
Blocking: When read operation place shared locks on rows, pages, or tables, and update operations
need to place exclusive locks on the same data. This is because an exclusive lock can’t be placed on a
shared lock. As a rule of thumb, any blocking that occurs for longer than 1 second creates contention,
lowers concurrency, and is typically a performance problem.
To terminate a process that may be causing severe blocking: member of the sysadmin fixed server role
must issue the command KILL [SPID], where SPID is the session ID that belongs to the blocking process.
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 89
Deadlocking is a combination of locks that can never be resolved. This requires at least 2 processes,
where each process is making a modification to the same data.
For example, process1 acquires a shared lock on data1, and needs an exclusive lock on data2. At the
same time, process2 acquires a shared lock on data2, and needs an exclusive lock on data1. Since both
processes are contending for the same data, a deadlock occurs.
SQL Server can detect a deadlock and in response applies an algorithm known as deadlock detection to
select one of the processes as the deadlock victim. This terminates the victim process, rolls back any
necessary transactions, releases the locks, and returns error 1205 to the application.
Database mirroring was initially available only for evaluation purposes, but then later became a
supported feature with no restrictions in Service Pack 1. To enable Database mirroring on the evaluation
version, the activation of trace flag 1400 was required.
Database mirroring works through the transaction log being copied to a mirrored server and then having
those log entries written to that server; this is known as “copy-on-write” technology. Since database
mirroring relies on the transaction log, on databases with the full recovery model can use database
mirroring.
3. Restore that instance on the mirroring database using the NORECOVERY option.
4. Copy other database objects from primary to mirror servers (typically logins, SQL Server Agent
jobs, linked servers, etc).
Two ways to set the recovery model: through SSMS Database Options, or via T-SQL:
To see how to backup and restore a database, refer to the tutorial on Backup and Restore.
Establishing Endpoints
Endpoints are the objects used in SQL Server that allows the capability to connect to a database
instance. There are two types of endpoints: TCP or HTTP.
TCP Endpoints are needed for Database Mirroring (specify a payload of DATABASE_MIRRORING). You
can only specify one type of endpoint per database instance. By specifying the type of endpoint, the
endpoint will refuse any other type of request.
You must also specify a port number for the endpoint (default is 5022), and then configure the Listener
IP for that endpoint. If the principal, mirror, or witness roles are instances on the same server, they must
use different port numbers. The Listener IP is simply a range/list of IP addresses that the endpoint will
accept connections/requests from (default is ALL).
Additionally, you must specify authentication, encryption (optional), and specify the state of the
endpoint as STARTED.
Example T-SQL Command:
The “ROLE” clause is an additional layer of security that specifies what the endpoint will allow:
PARTNER = Principal or Mirror
WITNESS
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 92
Operating Modes
Operating modes dictate how the transactions are transferred between the principal and the mirror, as
well as the type of failover that is available (automatic versus manual). There are three different modes
that Database Mirroring can operate in:
1. High Availability
2. High Performance
3. High Protection
High Availability Operating Mode: Synchronous transaction log transfer and automatic failover.
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 93
In High Availability mode, SQL Server writes the transaction to the transaction log, which triggers the
copy of these rows to the mirror database. The mirror then sends an acknowledgement back to the
principal, the principal proceeds with processing any additional transactions.
Synchronous transfer incurs performance overhead, which increases as distance between the servers
increases.
High availability mode requires a witness server. When a failure on the principal occurs, the witness
reverses the principal/mirror roles, as well as the transaction flow. For this to happen, both the mirror
and the witness must “agree” that the principal is unavailable via heartbeat monitoring (ping).
High Performance Operating Mode: Asynchronous transaction log transfer and manual failover.
Does not require a witness server; transactions are first applied to the principal and then they are
copied over to the mirror (handled by a separate process). This introduces latency but increases
performance.
High Protection Operating Mode: Synchronous transaction log transfer and manual failover.
Configured the same as High Availability Mode, but does not use a witness server. Therefore, you must
manually promote the mirror to the principal upon server failure.
Transparent Client Redirection: Application connections that automatically use the mirror server
connection if the principal goes down (by caching the mirror connection until it is needed).
Since the mirror will be in NO RECOVERY state, you can not initiate failover via SSMS (unless connecting
as admin).
In High Availability mode, you can still manually force a failover. This is typically done for general
maintenance of a server. The T-SQL command to do this from the principal is:
ALTER DATABASE SET PARTNER FAILOVER
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 94
From SSMS: right-click the database | Properties | Mirroring page | Stop Mirroring
T-SQL Command: ALTER DATABASE [DBName] SET PARTNER OFF
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 95
Figure 1 –
To enable the database as the primary database, simply check the initial checkbox. You can then
configure the Backup settings:
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 97
The Network path is simply directory that will store the backed up transaction log. The second box is
only used if you will store the transaction log locally (not recommended). Additional options allow you
to specify when to delete transaction log files, generate an alert if no backup occurs within a given
amount of time, etc.
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 98
Managing Replication
Replication is the technology in SQL Server that allows the distribution of data and database objects
from one database to one or more databases (and synchronizing those databases). Benefits of database
replication include increased availability and distribution of data-access workload.
Replication Architecture
Replication uses terminology similar to the publishing industry, as the components and processes are
the same for the most part.
A publication is a group of articles. This simplifies management as it provides a point for subscribing to a
group of articles rather than subscribing to articles individually.
The Publisher is the owner of the publication. Depending on the replication type, the publisher may be
the only role that can modify the data.
The Distributor manages the distribution database, which stores replication status data, metadata, and,
depending on the replication type, the actual data that is replicated. A Local Distributor is one that is
also a Publisher. In contrast, a Remote Distributor is one that is one a separate server than the Publisher
role.
The Subscriber is the server that receives the publication(s) and provides the data to end
users/applications. Depending on the type of replication, the subscriber would be read-only. In other
cases, the subscriber could update information and replicate those changes back to the Publisher.
Pull Subscriptions involve the Subscriber retrieving the data from the Distributor.
Push Subscriptions are good in scenarios with stable/permanent connections, and allow centralized
management of all replication agents. Pull Subscriptions are good for connections that are made on-
demand, and is also less resource-intensive on the Distributor if there are multiple Subscribers
(processing is on the Subscriber side).
Replication Types
There are three types of replication:
1. Snapshot Replication
2. Transactional Replication
3. Peer-to-Peer Replication
Snapshot Replication involves the server copying an entire set of data (the articles included in the
publication) to the Subscribers at scheduled times. This replication type should only be used with small
datasets that are somewhat static in nature.
Transactional Replication makes an initial complete copy of the data, and then all following transactions
include only data that has been modified (using the transaction log). This also allows data updates on
the Subscriber end, which introduces the idea of merge replication.
Peer-to-peer Replication allows multiple servers to subscribe to the same schema and data, and permits
simultaneous changes in multiple servers.
Replication Agents
Replication is performed through a collection of replication agents, which are agent jobs that run on the
different replication servers.
Snapshot Agent – Runs in Distributor; prepares schemas and initial data files.
Log Reader Agent – Runs in Publisher (for transactional replication); copies each transaction
that affects the publication to the distribution database.
Distribution Agent – Runs in Distributor (push) or Subscriber (pull); delivers the initial snapshot
and contents of distribution database to Subscribers.
Merge Agent – Runs in Distributor (push) or Subscriber (pull); handles conflict resolution.
Queue Reader Agent – agent that reads the queue and applies transactions to the publisher
(only if using queued update subscriptions)
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 101
Configuring Replication
The first step involved in configuring replication is configuring the Distributor. Remember that a
Publisher can only use a single Distributor, but a single Distributor can service multiple Publishers. By
default, the Distributor server serves its own data (local Distributor), but it can also serve the data of a
remote Publisher.
Before configuring the Distributor, ensure that the server that will be hosting this role has ample disk
space and CPU/Memory.
In SSMS:
Connect to the Database instance you want to be the Publisher/Distributor.
Right-click the Replication folder and choose Configure Distribution
Next you will see the screen where you select which instance/server will act as the Distributor. Keep the
default options and select “Next”.
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 102
On the Snapshot Folder page, enter the directory/UNC path that will contain the database snapshots.
You must use a UNC if you intend on using pull replication (in which case the Snapshot Agent needs
write permission and Merge/Distribution Agent needs read permission). Click Next.
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 103
The next screen is where you configure settings for the Distribution Database. You can specify the
database name and location of the data/log files. Click Next.
On the Publishers screen, you specify which servers will use this server as a Distributor. Note that you
can add either SQL Server Publishers or Oracle Publishers. Click Next.
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 104
This screen allows you to specify whether or not you want to configure distribution, as well as generate
a script file. This is useful for documentation and serves as backup in a recovery plan. Select the
checkbox to generate a script, and click Next.
For the Script File Properties, you can specify the File name/location of the script, whether or not to
overwrite a preexisting script, and the file format of the script. Click Next.
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 105
The progress of the Distribution configuration will be displayed. Click Close to finish the process.
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 106
Configuring a Publication
Connect to the database instance that hosts the data you want to Publish.
Expand the Replication folder | Right-Click Local Publications | New Publication.
Select the database that contains the data you want to replicate, and select Next.
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 108
On the Articles page, you specify which database objects you want to include in the publication. If you
select an object that depends on another object, those objects must also be included. Select Next.
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 109
You can now filter on specific rows that you want to retrieve:
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 110
The next screen configures the Snapshot Agent. You can specify whether to create a snapshot
immediately after configuration or at a later specified time. Select to Create the snapshot immediately,
and click Next.
This screen allows you to create the publication and generate a script. Leave the default settings and
click Next.
Now you just select a publication name, and review the publication settings. Click OK.
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 112
Subscribing to a Publication
In SSMS:
Connect to the Publisher database instance.
This screen allows you to select which publication/Publisher to use. This is automatically selected for
you if you create a subscription from the Publisher. If you create a subscription from the Subscriber, you
must fill this information in manually. Click Next.
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 114
The Distribution Agent Location screen allows you to specify push subscriptions or pull subscriptions.
Leave the settings as-is and click Next.
The Subscribers screen allows you to specify the subscriber to the publication. Click “Add Subscriber”,
and select “Add SQL Server Subscriber”. Now select another instance of SQL Server (if one exists), and
connect to that instance (shown below).
On this screen, you configure the security context for the Subscription agent and the connections to the
Distributor/Subscriber. Click “…” to open the security configuration screen.
On the Synchronization Schedule page, you choose the schedule that the agent will run. For Merge and
Snapshot replication, use “Run on Demand” or “Set a Schedule”. For transactional replication, choose
“Run continuously”. Click Next.
When initializing a subscription, the initial snapshot creates the schema and copies all data contained in
the publication. This could potentially be resource intensive, so you could specify to not initialize the
subscription immediately. For this example, keep the defaults and click Next.
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 117
This screen is similar to others you have seen for Distributor/Publisher configuration. Select “Generate a
script file…” and click Next. Retain the script defaults and click Next (screenshot not shown).
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 118
The Complete the Wizard screen is a summary of configuration options. Click Finish to complete the
configuration process.
To verify the subscription configuration, back in the Publisher instance of SQL Server, expand the
Replication node | Local Publications | Choose the publication that you subscribed to | Right-click and
choose Properties:
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 119
SQL Server 2005 also allows you to secure individual publications with publication access lists (PAL). This
is automatically created with the publication, and can be accessed through SSMS.
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 120
In SSMS:
Connect to the database instance that is the Publisher.
Expand Replication node | Local Publications Folder |Right-click Publication | Properties
Select the Publication Access List page
While creating articles for a publication that uses merge replication, you can specify these conflict
resolvers.
In SSMS:
Create a new publication | Choose Merge Publication | Select the Subscriber Types
Select the articles, and choose the Article Properties drop-down list
Select Set Properties of Highlighted Table Article
Select the Resolver tab | Use a custom resolver
The best way to become familiar with these resolvers is to refer to Books Online (BOL).
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 122
Monitoring Replication
In SQL Server 2005, you use SQL Server Replication Monitor (SSRM) to observe the replication activity on
your server(s).
In SSMS:
Connect to Distributor instance.
Right-click the user that will monitor
replication | Properties
Select the User Mapping page
Select the distribution database |
Select the Map checkbox | Select
replmonitor in the
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 123
Tracer Tokens: These allow you to measure the latency of the replication process (from Publisher to
Distributor and from Distributor to Subscriber). To access this, select a subscription, select the Tracer
Token tab, and click on Insert Tracer.
To configure alerts:
1. Open SSRM
2. Expand Replication Monitor | My Publishers | [PublisherName]
3. Select the publication you want to set the alert for
4. Click on Warnings and Alerts tab
5. Click Configure Alerts
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 125
Messaging Overview
Messages are simply data input that is sent to another process/server that requires some form of
processing/return value. Messages are only valuable if a) there is a means to process the message, and
b) capability of an application to accept message input.
Endpoints can be in the same database, in different databases on the same instance, or in databases on
different instances/servers.
Conversations are the exchanges of data/messages between endpoints; they manage the flow of
messages between initiator and target. Conversations can be:
Monolog: Conversation from one endpoint to any number of targets (not currently available in
SQL Server 2005).
Dialog: Conversation between exactly two endpoints.
Queues store messages in a table because messages are not always processed synchronously. When
apps send messages, an entry is added to the bottom of the table, while other applications read from
the top of the table and work their way down. Once a message is read, it is removed from the queue.
Services are an abstraction layer in Service Broker that allows queues to be placed between databases
and servers (separates applications from the physical storage).
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 126
Contracts are specified by services, and they specify what objects are allowed to be sent/received within
the messages. Furthermore, message format is constrained by a mechanism called the message type.
VALIDATION specifies whether or not messages are validated when they are submitted. The options for
validation include:
Creating a Contract
CREATE CONTRACT contract_name
AUTHORIZATION owner_name
([MessageTypeName] SENT BY INITIATOR,
[MessageTypeName SENT BY TARGET)
Options include:
STATUS = {ON | OFF} – Determines whether the queue is enabled.
RETENTION = {ON | OFF} – Determines whether messages are automatically removed from the
queue after they are processed.
ACTIVATION (STATUS = {ON | OFF},
PROCEDURE_NAME = <procedure>,
MAX_QUEUE_READERS = max_readers,
EXECUTE AS {SELF | ‘user_name’ | owner}
ACTIVATION specifies if a stored procedure is enabled to send notification when a new entry is
place in the queue (signifies that new work needs to be processed). If Service Broker determines
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 128
that messages are arriving faster than they can be processed by the existing procedures,
another copy of the sproc is launched (up to the max_readers value).
Creating a Service
Services define the endpoint (or queue) that a conversation will use as well as the types of conversations
(or contracts) that are allowed on a queue.
You can only specify a single queue, so for effective communication you should create a service for the
initiator and a service for the target. One or more contracts can be specified for the service.
Creating Conversations
Each message has a sequence number, which guarantees that messages are processed in order. Service
Broker has a mechanism that allows it to retry messages until they are successfully received.
Conversations implement this reliable ordered process for delivering messages via services.
CREATE ROUTE – adds a new route to the routing table for the current database; should specify three
components:
1. Name of the service for the route
2. Broker instance identifier of the DB to send the messages to
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 129
The first step in creating a full-text index is to create a full-text catalog that will hold the indexes.
It is best practice to place the catalog on a secondary filegroup that is used exclusively for full-text
catalogs. This allows filegroup backup/restore of full-text catalogs, independent from the rest of the
database.
IN PATH specifies the location the catalog will be stored (since it is saved outside of SQL Server).
AS DEFAULT specifies that the catalog will be the default catalog when any full-text index is created (and
no catalog is specified).
Helper Services are used in full-text index creation, and include word breakers and stemmers, language
filters, noise word files, filters, and protocol handlers.
Word breakers: routines that find the breaks between words, generate word list for every row
within a column that is being indexed
Stemmers: conjugate verbs
Language Files: use word breakers and stemmers to understand the words; handles multiple
languages without requiring translation
Noise words: Commonly used words, not useful for search routines (the, a, an)
Filters: Additional column used to mark a file as pdf, word, excel, etc. Loads up the binary
stream stored in the column, returns text within the document to the word-break routine.
Protocol handlers: Identifies how to handle formatting of different file formats.
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 131
KEY INDEX specifies the table’s unique column, and must be a single column in the table.
WITH CHANGE_TRACKING specifies how to propagate changes in the underlying columns to the full-text
index (a separate process is run to perform this update). If using the OFF option, you can also specify NO
POPULATION to prevent the index from being populated during index creation.
If using a column of type varbinary, varbinary(max), or image, you must add the clause TYPE COLUMN
columnName fileExtension.
Full populations cause all rows from the table to be read and processed; this is resource-intensive and is
generally only down when the index is created.
Incremental populations automatically populates the index for rows that were modified since the last
population. This requires a timestamp column, which is used to determine when the last changes were
made.
Update populations use that changes that SQL Server tracks to process any inserts, updates, and deletes
since the last time a change-tracked index was populated.
REBUILD deletes the full-text index and recreates it; this is generally only done if the
ACCENT_SENSITIVITY needs to be changed.
REORGANIZE causes all indexes in the catalog to have all changes merged. This frees up disk space and
memory resources.
FREETEXT: accepts one or more columns to search and a search argument. Searches using stemming
and for related words which the thesaurus identifies.
Example Query:
SELECT Title
FROM Production.Document
WHERE FREETEXT (Document, 'search term' );
FREETEXTTABLE: functions the same as FREETEXT, but returns a rowset that contains a rank column (1 –
1000, indicating how well the row matches the search criteria). Additionally, a KEY column is returned to
display the unique key used to identify the specific row.
CONTAINS: allows you to use precise or fuzzy matching algorithms (performing exact matches or prefix
searches). According to SQL Server BOL, CONTAINS can search for:
A word or phrase.
The prefix of a word or phrase.
A word near another word.
A word inflectionally generated from another (for example, the word drive is the inflectional
stem of drives, drove, driving, and driven).
A word that is a synonym of another word using thesaurus (for example, the word metal can
have synonyms such as aluminum and steel).
It is also possible to search using INFLECTIONAL and THESAURUS specifications. INFLECTIONAL searchs
for terms that stem from the search term; for example, the word “eat” would also find “ate”, “eaten”,
“eating”, etc. THESAURUS specifies to search for synonyms of the word as well.
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 133
Word proximity can also be specified in full-text searches. By specifying either the NEAR keyword or just
~, you specify that the rank is partly based on the proximity of words. For example,
CONTAINS(Column, 'mountain NEAR bike'); would return ranked results if “mountain” and
“bike” occurred near each other.
You can also specify the weight of search terms by using the ISABOUT clause. This manually assigns the
relevance for each search term used, based on the level of significance of a term.
CONTAINSTABLE: functions the same as the CONTAINS function, but returns a rowset that contains a
RANK and a KEY column; simply specify CONTAINSTABLE.
http://www.proprofs.com [ULTIMATE 70-431 STUDY GUIDE] Page | 134
References:
SQL Server 2005: Implementation and Maintenance Self-Paced Training Kit