PUBLISHED BY
Microsoft Press
A Division of Microsoft Corporation
One Microsoft Way
Redmond, Washington 98052-6399
Copyright © 2006 by Solid Quality Learning
All rights reserved. No part of the contents of this book may be reproduced or transmitted in any form or by
any means without the written permission of the publisher.
0-7356-2271-X
978-0-7356-2271-5
Library of Congress Control Number 2006924471
Printed and bound in the United States of America.
1 2 3 4 5 6 7 8 9 QWT 1 0 9 8 7 6
Distributed in Canada by H.B. Fenn and Company Ltd.
A CIP catalogue record for this book is available from the British Library.
Microsoft Press books are available through booksellers and distributors worldwide. For further information
about international editions, contact your local Microsoft Corporation office or contact Microsoft Press Inter-
national directly at fax (425) 936-7329. Visit our Web site at www.microsoft.com/mspress. Send comments
to
Microsoft, Active Directory, ActiveX, Excel, Internet Explorer, Microsoft Press, MSDN, Outlook, PivotTable,
PowerPoint, Visio, Visual Basic, Visual C#, Visual Studio, Windows, and Windows Server are either regis-
tered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries. Other
product and company names mentioned herein may be the trademarks of their respective owners.
The example companies, organizations, products, domain names, e-mail addresses, logos, people, places, and
events depicted herein are fictitious. No association with any real company, organization, product, domain
name, e-mail address, logo, person, place, or event is intended or should be inferred.
This book expresses the author’s views and opinions. The information contained in this book is provided with-
out any express, statutory, or implied warranties. Neither the authors, Microsoft Corporation, nor its resellers,
or distributors will be held liable for any damages caused or alleged to be caused either directly or indirectly
by this book.
Acquisitions Editor: Ken Jones
Project Editor: Laura Sackerman
Technical Editors: Rozanne Murphy Whalen and Dan Whalen
Copy Editor: Nancy Sixsmith
Indexer: Ginny Munroe
Body Part No. X12-35101
A02L62271X.fm Page ii Friday, April 29, 2005 7:23 PM
iii
Acknowledgments
There are always more people to thank than you ever have the space for. But the
authors would like to extend their sincere gratitude to the following for making this
book possible: A huge thanks to Ken Jones, Laura Sackerman, Jenny Moss Benson,
and everyone else at Microsoft Press, as well as Nancy Sixsmith, Dan Whalen, and
Rozanne Murphy Whalen—your Herculean efforts have not been missed. Also thanks
to Kathy Blomstrom, content development manager for Solid Quality Learning, for
her tireless work on the project and to Federico Bazo Alfaro for helping to keep the
wheels moving.
In addition, the authors would like to acknowledge the following special people:
Daren Bieniek I would like to thank my mom (MaryAnn), wife (Shara), and daughter
(Amelia) for tolerating all of those times that I said, “I can't right now. I'm working.”
Mike Hotek I’d like to thank the SQL Server Development Team for creating a prod-
uct that my life has revolved around for more than 15 years. Thanks to my sister Car-
rie, brother-in-law Dan, and nieces Tasha and Ashley for the support to keep going. I
would have never made it here without my grandparents, whom I’ve always thought
of as my other set of parents, for being a huge part of who I am today. Jen and Gabby,
you have provided more smiles than you can ever imagine. Thank you to the many
people at Sacred Heart in Eau Claire and Abbott Northwestern in Minneapolis for
sending my best friend back—three times. There aren’t words in any language to ade-
quately describe what you do every day. And thanks to my best friend—Dad.
Antonio Soto I need to thank my wife, Isabel, for her patience and understanding.
She is my raison d'être. Thanks to Fernando Guerrero for placing his trust in me.
Thanks to Marcelo Castelo for giving me the opportunity to work with him. Thanks
to Solid Quality Learning for its warm welcome. And thanks to Mosqui for making me
laugh in the bad moments.
Adolfo Wiernik I definitely must thank Fernando Guerrero, CEO of Solid Quality
Learning, for believing in me from the start when I joined this amazing company.
Thanks to Michael Rys, SQL Server Program Manager in charge of the XML features
in the product, for his mentoring and always answering my mails. Finally, I thank the
team at Magen, Microsoft Technology Center in Tel Aviv, Israel, which is where I
started working with the XML features in SQL Server 2000 and laid the foundation
for who I am.
A03A62271X.fm Page iii Friday, April 29, 2005 7:23 PM
A03A62271X.fm Page iv Friday, April 29, 2005 7:23 PM
v
About the Authors
MCTS Self-Paced Training Kit (Exam 70-431): Microsoft SQL Server 2005—Implementation
and Maintenance was written by the following mentors with Solid Quality Learning, a
trusted global provider of advanced education and solutions for the Microsoft SQL
Server platform:
Daren Bieniek is a mentor with Solid Quality Learning, has 20 years of professional
technical experience, and has served in nearly every IT role possible—from systems
administrator to developer to database administrator to architect to CEO. During the
past 10 years, Daren has focused on data-related roles and technologies, with an
emphasis on very large database (VLDB) architecture, design, and implementation.
Randy Dyess is the author of Transact-SQL Language Reference Guide and many maga-
zine and newsletter articles about SQL Server security and optimization issues, and
has spoken at various international and national conferences. Randy is a member of
the Board of Directors for the Professional Association for SQL Server (PASS) and is
the Director of Programs for the North Texas SQL Server Users Group. He is also the
founder and principle author of www.TransactSQL.com.
Mike Hotek has been working with SQL Server since before it was a Microsoft prod-
uct. He is known throughout the industry for his expertise in replication, high avail-
ability, disaster recovery, and performance tuning. Mike has delivered more than 200
sessions at a variety of conferences and has coauthored two other books about SQL
Server.
Javier Loria, a mentor with Solid Quality Learning, works primarily with Latin Amer-
ican companies to help them develop business intelligence projects. He is also a
trainer and a frequent speaker at Microsoft regional events and loves to write about
technology. When not working, Javier enjoys spending time with his wife, Maria, and
his five children: Lidia, Javier Jose, Maria Jose, Andrea, and Lucia.
Adam Machanic is an independent database software consultant, writer, and speaker
based in Boston, Massachusetts. He has implemented SQL Server solutions for a vari-
ety of high-availability online transaction processing (OLTP) and large-scale data
warehouse applications, and also specializes in Microsoft .NET data access layer per-
formance optimization. Adam is a Microsoft Most Valuable Professional (MVP) for
SQL Server and a Microsoft Certified IT Professional.
A04B62271X.fm Page v Friday, April 29, 2005 7:24 PM
vi
Antonio Soto, a database fanatic, has been working with databases since 1994. He
has worked with the IT training company Professional Training, in Spain, as a trainer
and consultant regarding SQL Server and other Microsoft technologies. Since 2002,
Antonio has also been a partner and director of Alcatraz Solutions, which provides
security solutions to the IT industry. He writes for various magazines and speaks at
many IT conferences in Spain. Antonio is a computer engineer, an MCDBA, an MCSE,
an MCAD, and an MCT.
Adolfo Wiernik is director of operations for Solid Quality Learning in Latin America.
He previously worked as lead architect at the Microsoft .NET Center in Central Amer-
ica and the Microsoft Technology Center in Tel Aviv, Israel. When not speaking at a
local or international event, Adolfo enjoys hiking in the mountains in his home coun-
try of Costa Rica.
A04B62271X.fm Page vi Friday, April 29, 2005 7:24 PM
vii
Contents at a Glance
1 Installing SQL Server 2005 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
2 Configuring SQL Server 2005. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
3 Creating Tables, Constraints, and User-Defined Types. . . . . . . . . . . . . . 103
4 Creating Indexes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
5 Working with Transact-SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
6 Creating Partitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
7 Implementing Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
8 Managing XML Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
9 Creating Functions, Stored Procedures, and Triggers. . . . . . . . . . . . . . . 349
10 Working with Flat Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
11 Backing Up, Restoring, and Moving a Database . . . . . . . . . . . . . . . . . . . 413
12 Using Transact-SQL to Manage Databases. . . . . . . . . . . . . . . . . . . . . . . . 447
13 Working with HTTP Endpoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
14 Working with SQL Server Agent Jobs. . . . . . . . . . . . . . . . . . . . . . . . . . . . 493
15 Monitoring and Troubleshooting SQL Server Performance . . . . . . . . . 529
16 Managing Database Snapshots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 601
17 Implementing Database Mirroring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 615
18 Implementing Log Shipping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 651
19 Managing Replication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 695
20 Working with Service Broker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 775
21 Creating Full-Text Catalogs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 813
A05C62271X.fm Page vii Friday, April 29, 2005 7:24 PM
A05C62271X.fm Page viii Friday, April 29, 2005 7:24 PM
ix
What do you think of this book?
We want to hear from you!
Microsoft is interested in hearing your feedback about this publication so we can
continually improve our books and learning resources for you. To participate in a brief
online survey, please visit: www.microsoft.com/learning/booksurvey/
Table of Contents
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxxiii
Hardware Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxiii
Software Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxiv
Using the CDs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxv
How to Install the Practice Tests. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxv
How to Use the Practice Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxvi
How to Uninstall the Practice Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxvii
Microsoft Certified Professional Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxvii
Technical Support. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxxviii
Evaluation Edition Software Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxxvix
1 Installing SQL Server 2005 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Before You Begin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2
Lesson 1: Selecting the Correct SQL Server 2005 Edition . . . . . . . . . . . . . . . . . . . . . . . .3
Understanding SQL Server 2005 Editions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3
Lesson Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8
Lesson 2: Determining Infrastructure Requirements for SQL Server 2005 . . . . . . . . .10
Identifying Minimum Hardware, OS, and Network Requirements. . . . . . . . . . .10
Lesson Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .15
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .16
Lesson 3: Using Default, Named, and Multiple Instances of SQL Server 2005. . . . . .17
Installing a Default, Named, or Multiple Instances of SQL Server 2005 . . . . . .17
Determining When to Use Multiple Instances of SQL Server 2005 . . . . . . . . . .18
Lesson Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .19
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .19
A06T62271X.fm Page ix Monday, May 1, 2006 11:01 AM
x Table of Contents
Lesson 4: Installing a New Instance of SQL Server 2005 . . . . . . . . . . . . . . . . . . . . . . . 21
Determining Service Accounts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Choosing an Authentication Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Determining Collation Setting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Lesson Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Lesson 5: Upgrading to a SQL Server 2005 Installation . . . . . . . . . . . . . . . . . . . . . . . . 34
Determining an Appropriate Upgrade Strategy . . . . . . . . . . . . . . . . . . . . . . . . . 34
Choosing an Upgrade Method. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Determining Testing and Success Criteria. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Establishing a Recovery Plan. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Tips for a Successful Upgrade . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Lesson Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Chapter Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Chapter Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Key Terms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Case Scenarios. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Case Scenario 1: Installing SQL Server 2005. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Case Scenario 2: Upgrading an Instance of SQL Server . . . . . . . . . . . . . . . . . . . 48
Suggested Practices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Selecting an Edition of SQL Server 2005 to Install. . . . . . . . . . . . . . . . . . . . . . . . 49
Determining When to Install Default, Named, or Multiple
Instances of SQL Server 2005 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Upgrading a SQL Server 2005 Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Take a Practice Test. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
2 Configuring SQL Server 2005 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Before You Begin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Lesson 1: Configuring Log and Data Files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Data Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Log Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Filegroups. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
How to Configure Data Files and Log Files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
A06T62271X.fm Page x Friday, April 29, 2005 7:24 PM
Table of Contents xi
Configuring Database Files with RAID Systems . . . . . . . . . . . . . . . . . . . . . . . . . . .57
Best Practices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .58
Lesson Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .61
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .61
Lesson 2: Configuring Database Mail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .63
Identifying Database Mail Prerequisites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .63
Understanding the Database Mail Architecture . . . . . . . . . . . . . . . . . . . . . . . . . .64
How to Configure Database Mail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .64
Lesson Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .68
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .68
Lesson 3: Specifying a Recovery Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .70
Recovery Models Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .70
How to Configure Recovery Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .71
Lesson Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .72
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .73
Lesson 4: Configuring Server Security Principals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .74
Choosing Between Authentication Modes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .74
How to Configure SQL Server Logins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .75
Managing Fixed Server Roles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .77
Lesson Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .79
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .79
Lesson 5: Configuring Database Securables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .81
Managing Database Users . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .81
Managing Database Roles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .82
Managing Schemas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .84
Lesson Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .86
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .86
Lesson 6: Configuring Encryption . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .88
Configuring the Encryption Hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .88
Configuring Symmetric and Asymmetric Keys . . . . . . . . . . . . . . . . . . . . . . . . . . .89
Configuring Certificates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .90
Lesson Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .92
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .92
A06T62271X.fm Page xi Friday, April 29, 2005 7:24 PM
xii Table of Contents
Lesson 7: Configuring Linked Servers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
How to Create a Linked Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Configuring the Security Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Lesson Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
Chapter Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Chapter Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Key Terms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Case Scenarios. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Case Scenario 1: Configuring Security. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Case Scenario 2: Configuring a Heterogeneous Environment. . . . . . . . . . . . . 102
Suggested Practices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Take a Practice Test. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
3 Creating Tables, Constraints, and User-Defined Types . . . . . . . . . . . . . 103
Before You Begin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
Lesson 1: Creating Tables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
Understanding Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
Nullability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Identity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
Computed Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
Creating a Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Assigning Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Lesson Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
Lesson 2: Implementing Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Check Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Default Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Unique Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Primary Key Constraints. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Foreign Key Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Lesson Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
A06T62271X.fm Page xii Friday, April 29, 2005 7:24 PM
Table of Contents xiii
Lesson 3: Creating User-Defined Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Transact-SQL UDTs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
CLR UDTs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Implement a Transact-SQL UDT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Lesson Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
Chapter Review. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
Case Scenario: Designing a Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
Suggested Practices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
Creating Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
Creating Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
Take a Practice Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
4 Creating Indexes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Before You Begin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
Lesson 1: Understanding Index Structure. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Exploring B-Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Inside Index Levels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Lesson Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
Lesson 2: Creating Clustered Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Implementing Clustered Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Disabling an Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
Lesson Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
Lesson 3: Creating Nonclustered Indexes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
Implementing a Nonclustered Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
Creating a Covering Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Balancing Index Maintenance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Using Included Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Lesson Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
A06T62271X.fm Page xiii Friday, April 29, 2005 7:24 PM
xiv Table of Contents
Chapter Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
Chapter Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
Key Terms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
Case Scenario: Indexing a Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
Suggested Practices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
Creating Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
Take a Practice Test. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
5 Working with Transact-SQL. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
Before You Begin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
Lesson 1: Querying Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
Determining Which Tables to Use in the Query. . . . . . . . . . . . . . . . . . . . . . . . . 171
Determining Which Join Types to Use. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Determining the Columns to Return . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
How to Create Subqueries. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
Creating Queries That Use Complex Criteria . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Creating Queries That Use Aggregate Functions. . . . . . . . . . . . . . . . . . . . . . . . 177
Creating Queries That Format Data by Using PIVOT and
UNPIVOT Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
Creating Queries That Use Full-Text Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Limiting Returned Results by Using the TABLESAMPLE Clause . . . . . . . . . . . . 181
Lesson Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
Lesson 2: Formatting Result Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
Using System Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
Using User-Defined Functions in Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
Querying CLR User-Defined Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
Creating Column Aliases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
Lesson Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
Lesson 3: Modifying Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
Understanding Cursors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
Creating Local and Global Temporary Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
Using the SELECT INTO Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
Lesson Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
A06T62271X.fm Page xiv Friday, April 29, 2005 7:24 PM
Table of Contents xv
Lesson 4: Working with Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
Beginning and Committing or Rolling Back Transactions . . . . . . . . . . . . . . . . 198
Programmatically Handle Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Lesson Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
Chapter Review. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
Case Scenarios . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
Case Scenario 1: Database-Backed Authoring Application . . . . . . . . . . . . . . . 203
Case Scenario 2: Banking Corporation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
Suggested Practices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
Writing Queries Against the Sales Schema of the
AdventureWorks Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
Take a Practice Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
6 Creating Partitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
Before You Begin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
Lesson 1: Creating a Partition Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
How to Create a Partition Function. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
Lesson Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
Lesson 2: Creating a Partition Scheme . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
How to Create a Partition Scheme . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Lesson Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
Lesson 3: Partitioning Tables and Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
Creating a Partitioned Table, Index, or Indexed View. . . . . . . . . . . . . . . . . . . . 218
Partitioned Indexes and Included Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Partitioning an Existing Table or Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
Lesson Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
Lesson 4: Querying Partitions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
How to Query Partitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
Lesson Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
A06T62271X.fm Page xv Friday, April 29, 2005 7:24 PM
xvi Table of Contents
Lesson 5: Managing Partitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
Split and Merge. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
SWITCH. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
Lesson Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
Chapter Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
Chapter Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
Key Terms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
Case Scenario: Archiving Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
Suggested Practice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
Partitioning Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
Take a Practice Test. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
7 Implementing Views. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
Before You Begin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
Lesson 1: Creating a View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
How to Create a View. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
Understanding Ownership Chains . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
Lesson Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
Lesson 2: Modifying Data Through Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
Creating Updateable Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
Lesson Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
Lesson 3: Creating an Indexed View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
Prerequisites for an Indexed View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
Query Substitution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
Lesson Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
Chapter Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
Chapter Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
Key Terms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
Case Scenario: Creating Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
A06T62271X.fm Page xvi Friday, April 29, 2005 7:24 PM
Table of Contents xvii
Suggested Practices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
Creating a View. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
Creating an Indexed View. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
Take a Practice Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
8 Managing XML Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
Before You Begin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
Lesson 1: Working with XML Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
Storage Options for XML data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
Storing XML in Text Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
Storing XML in XML Data Type Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
Typing and Validating XML Data with XML Schemas. . . . . . . . . . . . . . . . . . . . 262
Lesson Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
Lesson 2: Retrieving XML Data by Using SQL Server Server-Side Technologies . . . . 269
Converting Relational Data to XML. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
Retrieving XML Data from the XML Data Type . . . . . . . . . . . . . . . . . . . . . . . . . 285
Lesson Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
Lesson 3: Retrieving XML Data by Using SQL Server Middle-Tier Technologies. . . . 298
Using SQLXML-Annotated XSD Schemas. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
Querying Annotated XML Schemas and XML Views from .NET . . . . . . . . . . . 304
Lesson Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
Lesson 4: Modifying XML Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
Modifying XML Values and XML Structure. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
Using SQLXML Updategrams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
Lesson Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
Lesson 5: Converting Between XML Data and Relational Data. . . . . . . . . . . . . . . . . 320
Shredding XML Using OPENXML and XML Stored Procedures . . . . . . . . . . . 320
Shredding XML by Using the XML Data Type’s nodes() Method . . . . . . . . . . 325
Shredding XML by Using SQLXML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
Lesson Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
A06T62271X.fm Page xvii Friday, April 29, 2005 7:24 PM
xviii Table of Contents
Lesson 6: Creating XML Indexes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334
Indexing an XML Data Type Instance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334
Creating an XML Data Type Primary Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
Creating XML Data Type Secondary Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
Lesson Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
Chapter Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341
Chapter Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341
Key Terms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341
Case Scenarios. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
Case Scenario 1: Troubleshooting XML Performance by Choosing
the Correct Indexing Strategy. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
Case Scenario 2: Handling Data as XML or as Relational Representation . . . 343
Suggested Practices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344
Working with XML Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344
Retrieving XML Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
Modifying XML Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
Converting Between XML Data and Relational Data . . . . . . . . . . . . . . . . . . . . 346
Creating XML Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
Take a Practice Test. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
9 Creating Functions, Stored Procedures, and Triggers . . . . . . . . . . . . . . 349
Before You Begin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
Lesson 1: Implementing Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
Scalar Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
Table-Valued Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354
Deterministic vs. Nondeterministic Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 356
Lesson Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
Lesson 2: Implementing Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360
Creating a Stored Procedure. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360
Assign Permissions to a Role for a Stored Procedure . . . . . . . . . . . . . . . . . . . . 363
Lesson Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
A06T62271X.fm Page xviii Friday, April 29, 2005 7:24 PM
Table of Contents xix
Lesson 3: Implementing Triggers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
DML Triggers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
DDL Triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
Lesson Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374
Chapter Review. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375
Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375
Key Terms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375
Case Scenario: Creating Triggers, Functions, and Stored Procedures . . . . . . . . . . . 376
Suggested Practices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376
Creating Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376
Creating Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
Creating Triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
Take a Practice Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
10 Working with Flat Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
Before You Begin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380
Lesson 1: Preparing to Work with Flat Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
Source File Location. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
Import Mechanism. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
Data Destination. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
A Best-Case Scenario . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382
Lesson Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386
Lesson 2: Running the bcp Utility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
What Is bcp? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
bcp Command-Line Syntax. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388
bcp Hint Parameter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
bcp Permissions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
Lesson Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392
A06T62271X.fm Page xix Friday, April 29, 2005 7:24 PM
xx Table of Contents
Lesson 3: Performing a BULK INSERT Task . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
Differences Between BULK INSERT and bcp . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
BULK INSERT Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
Lesson Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
Lesson 4: Importing Bulk XML Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398
OPENROWSET Function. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398
Lesson Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
Lesson 5: Using the SSIS Import/Export Wizard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402
How to Start the SSIS Import/Export Wizard . . . . . . . . . . . . . . . . . . . . . . . . . . . 402
Walking Through the Import/Export Wizard . . . . . . . . . . . . . . . . . . . . . . . . . . . 403
Lesson Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408
Chapter Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
Chapter Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
Key Terms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
Case Scenario: Fixing a Bloated Transaction Log. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410
Suggested Practices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410
Using bcp and BULK INSERT to Load Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
Using SSIS to Load Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
Take a Practice Test. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
11 Backing Up, Restoring, and Moving a Database . . . . . . . . . . . . . . . . . . 413
Before You Begin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414
Lesson 1: Backing Up a Database. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416
Performing Full Backups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
Performing Differential Backups. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
Transaction Log Backups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419
Performing Filegroup Backups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420
Performing Mirrored Backups. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421
Partial Backups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422
Lesson Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425
A06T62271X.fm Page xx Friday, April 29, 2005 7:24 PM
Table of Contents xxi
Lesson 2: Restoring a Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
Restoring a Full Backup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
Restoring a Differential Backup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429
Restoring a Transaction Log Backup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 430
Performing a Partial Restore. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432
Restoring a Corrupt Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432
Restoring with Media Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433
Validating a Backup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 434
Lesson Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436
Lesson 3: Moving a Database. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437
Moving a Database by Using Detach/Attach. . . . . . . . . . . . . . . . . . . . . . . . . . . 437
Using the Copy Database Wizard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438
Lesson Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441
Chapter Review. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442
Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442
Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
Case Scenario: Designing a Backup Strategy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444
Suggested Practices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
Backing Up a Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
Restoring a Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
Moving a Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446
Take a Practice Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446
12 Using Transact-SQL to Manage Databases. . . . . . . . . . . . . . . . . . . . . . . . 447
Before You Begin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447
Lesson 1: Managing Index Fragmentation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
Understanding Index Fragmentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
Identifying Index Fragmentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
Managing Index Fragmentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452
Determining Which Statement to Execute . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453
Lesson Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456
A06T62271X.fm Page xxi Friday, April 29, 2005 7:24 PM
xxii Table of Contents
Lesson 2: Managing Statistics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457
Understanding Statistics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457
Automatic Statistics Generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457
Manual Statistics Generation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458
Viewing Column Statistics Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458
Lesson Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462
Lesson 3: Shrinking Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463
Shrinking Database Files Automatically . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463
Shrinking Database Files Manually. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464
Shrinking the Transaction Log . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464
Lesson Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 467
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468
Lesson 4: Using DBCC CHECKDB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469
DBCC CHECKDB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469
Lesson Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472
Chapter Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473
Chapter Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473
Key Terms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
Case Scenarios. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
Case Scenario 1: Defragmenting an Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
Case Scenario 2: Maintaining Database Integrity . . . . . . . . . . . . . . . . . . . . . . . 475
Suggested Practices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475
Managing Index Fragmentation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475
Managing Statistics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475
Shrinking Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476
Using DBCC CHECKDB to Perform Database Integrity Checks . . . . . . . . . . . . 476
Take a Practice Test. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476
A06T62271X.fm Page xxii Friday, April 29, 2005 7:24 PM
Table of Contents xxiii
13 Working with HTTP Endpoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
Before You Begin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
Lesson 1: Understanding HTTP Endpoint Security . . . . . . . . . . . . . . . . . . . . . . . . . . . 479
Seven Layers of HTTP Endpoint Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479
Lesson Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 482
Lesson 2: Creating a Secure HTTP Endpoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484
Creating an HTTP Endpoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484
Specifying Web Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485
Specifying WSDL Support, Schemas, and Namespaces . . . . . . . . . . . . . . . . . . 486
Additional SOAP Payload Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486
Lesson Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 488
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489
Chapter Review. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491
Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491
Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491
Case Scenario: Creating HTTP Endpoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491
Suggested Practices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492
Creating HTTP Endpoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492
Take a Practice Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492
14 Working with SQL Server Agent Jobs. . . . . . . . . . . . . . . . . . . . . . . . . . . . 493
Before You Begin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 494
Lesson 1: Creating a SQL Server Agent Job . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495
How to Create a SQL Server Agent Job . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495
How to Specify a Job Owner . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497
How to Create Job Steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497
How to Create Job Schedules. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 500
Lesson Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 501
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 502
A06T62271X.fm Page xxiii Friday, April 29, 2005 7:24 PM
xxiv Table of Contents
Lesson 2: Creating a Maintenance Plan . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504
How to Create a Maintenance Plan . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504
Lesson Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 514
Lesson 3: Configuring Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515
How to Configure an Operator. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515
Lesson Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517
Lesson 4: Configuring Alerts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519
How to Configure Alerts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519
Lesson Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523
Chapter Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 524
Chapter Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 524
Key Terms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 524
Case Scenario: Scheduling Administrative Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . 525
Suggested Practices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 526
Create a SQL Server Agent Job. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 526
Create a Maintenance Plan . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 526
Create an Alert . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 526
Take a Practice Test. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 527
15 Monitoring and Troubleshooting SQL Server Performance. . . . . . . . . 529
Before You Begin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 531
Lesson 1: Working with SQL Server Profiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 532
Defining a Trace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 532
Starting, Pausing, and Stopping a Trace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 538
Saving a Trace Log . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 539
Gathering Showplan Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 540
Creating a Replay Trace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543
Lesson Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 546
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 546
A06T62271X.fm Page xxiv Tuesday, May 2, 2006 9:08 AM
Table of Contents xxv
Lesson 2: Working with System Monitor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548
Creating a Counter Log. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 550
Lesson Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553
Lesson 3: Using the Database Engine Tuning Advisor . . . . . . . . . . . . . . . . . . . . . . . . 554
Building a Workload File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 556
Configuring DTA to Analyze a Workload . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 556
Saving Recommendations from DTA. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 562
Lesson Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 564
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 564
Lesson 4: Using Dynamic Management Views and Functions . . . . . . . . . . . . . . . . . 566
Key Performance and Monitoring DMVs and DMFs. . . . . . . . . . . . . . . . . . . . . 567
Lesson Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 574
Lesson 5: Correlating Performance and Monitoring Data . . . . . . . . . . . . . . . . . . . . . 575
Basic Query Processing Architecture. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575
Correlating System Monitor Data with SQL Server Profiler Traces . . . . . . . . . 577
Correlating DMVs/DMFs with SQL Server Profiler Traces . . . . . . . . . . . . . . . . 578
Correlating DMVs/DMFs with System Monitor Data . . . . . . . . . . . . . . . . . . . . 579
Correlating Multiple DMVs/DMFs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 579
Lesson Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 581
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 581
Lesson 6: Resolving Blocking and Deadlocking Issues . . . . . . . . . . . . . . . . . . . . . . . . 582
Understanding Locking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 582
Understanding Isolation Levels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 583
Understanding Blocking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 584
Terminating Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 585
Understanding Deadlocking. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 585
Lesson Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 591
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 592
Lesson 7: Resolving Database Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 593
Using the DAC. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 593
SQL Server and Windows Error Logs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 594
Lesson Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 595
Lesson Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 595
A06T62271X.fm Page xxv Friday, April 29, 2005 7:24 PM