About Paul Nielsen
Nielsen
flast.tex
V4 - 07/23/2009
6:17pm
Page xxxiv
Nielsen
ffirs.tex
Microsoft
V4 - 07/21/2009
®
SQL Server 2008
Bible
®
Paul Nielsen
with Mike White
and Uttam Parui
Wiley Publishing, Inc.
11:34am
Page 1
Nielsen
ffirs.tex
V4 - 07/21/2009
11:34am
Soli Deo Gloria
— Paul Nielsen
For my wife, Katy, who has been so patient during the long nights
and weekends of research and writing.
— Mike White
To my wife, Shyama, and my daughters, Noyonika and Niharika.
— Uttam Parui
Microsoft® SQL Server® 2008 Bible
Published by
Wiley Publishing, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256
www.wiley.com
Copyright © 2009 by Wiley Publishing, Inc., Indianapolis, Indiana
Published simultaneously in Canada
ISBN: 978-0-470-25704-3
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by
any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under
Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of
the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance
Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the
Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111
River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley
.com/go/permissions.
Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or
warranties with respect to the accuracy or completeness of the contents of this work and specifically disclaim
all warranties, including without limitation warranties of fitness for a particular purpose. No warranty may be
created or extended by sales or promotional materials. The advice and strategies contained herein may not
be suitable for every situation. This work is sold with the understanding that the publisher is not engaged in
rendering legal, accounting, or other professional services. If professional assistance is required, the services
of a competent professional person should be sought. Neither the publisher nor the author shall be liable for
damages arising herefrom. The fact that an organization or Web site is referred to in this work as a citation
and/or a potential source of further information does not mean that the author or the publisher endorses the
information the organization or Web site may provide or recommendations it may make. Further, readers
should be aware that Internet Web sites listed in this work may have changed or disappeared between when
this work was written and when it is read.
For general information on our other products and services please contact our Customer Care
Department within the United States at (877) 762-2974, outside the United States at (317) 572-3993 or
fax (317) 572-4002.
Library of Congress Control Number: 2009928744
Trademarks: Wiley and the Wiley logo are trademarks or registered trademarks of John Wiley & Sons,
Inc. and/or its affiliates, in the United States and other countries, and may not be used without written
permission. Microsoft and SQL Server are registered trademarks of Microsoft Corporation in the United States
and/or other countries. All other trademarks are the property of their respective owners. Wiley Publishing,
Inc. is not associated with any product or vendor mentioned in this book.
Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not
be available in electronic books.
Page 2
Nielsen
f02.tex
V4 - 07/21/2009
11:24am
About Paul Nielsen
Paul Nielsen, SQL Server MVP since 2004, focuses on database performance through excellent
design — always normalized, generalized, and data driven. Continuing to push the envelope of
database design, Paul experiments with Object/Relational designs, the open-source T-SQL solution for
transforming SQL Server into an object database.
As an entrepreneur, Paul has developed an application that helps nonprofit organizations that give hope
to children in developing counties.
As a consultant, he has developed small to very large SQL Server databases, and has helped several
third-party software vendors improve the quality and performance of their databases.
As a presenter, Paul has given talks at Microsoft TechEd, PASS Summits, DevLink (Nashville), SQL
Teach (Canada), Rocky Mountain Tech Tri-Fecta, ICCM, and numerous user groups. Paul recorded
SQL Server 2005 Development with Total Training. He has presented his Smart Database Design
Seminar in the U.S., Canada, U.K., and Denmark. Paul also provides private and public SQL Server
developer and data architecture courses.
As a family guy, Paul lives in Colorado Springs with his very sweet wife, Edie, and Dasha, their fiveyear-old daughter. He has two adult children: Lauren, who will soon graduate from the University of
Colorado at Boulder, to collect scores of apples as an elementary school teacher, and David, who has a
genius within him. Paul’s hobbies include photography, a little jazz guitar, great movies, and stunt kites.
Paul answers reader e-mails at [email protected]
For links to blogs, Twitter, eNewsletter, seminars and courses, free SQL utilities, links, screencasts, and
updates to this book, visit www.SQLServerBible.com.
About the Contributing Authors
Mary Chipman has been writing about databases and database development since SQL Server
version 6.0. She is best known for the Microsoft Access Developer’s Guide to SQL Server, which has
maintained a five-star ranking on Amazon.com since it was published in 2000. Mary is currently part
of the Business Platform Division at Microsoft, where she is dedicated to providing customers with
the information they need to get the most out of Microsoft’s data access technologies. Prior to joining
Microsoft, Mary was a founding partner of MCW Technologies, spoke at industry conferences, authored
several books, and created award-winning courseware and videos for Application Developers Training
Company (www.appdev.com/info.asp?page=experts mchipman). She was awarded MVP status
every year from 1995 until she joined Microsoft in 2004. Mary contributed Chapter 38.
Scott Klein is a .Net and SQL Server developer in South Florida and the author of Professional SQL
Server 2005 XML (Programmer to Programmer). Scott contributed chapters 32, 33, and 34.
Uttam Parui is currently a Senior Premier Field Engineer at Microsoft. He has worked with SQL Server
for over 11 years and joined Microsoft nine years ago with the SQL Server Developer Support team.
Additionally, Uttam has assisted with training and mentoring the SQL Customer Support Services (CSS)
and SQL Premier Field Engineering (PFE) teams, and was one of the first to train and assist in the
development of Microsoft’s SQL Server support teams in Canada and India. Uttam led the development
Page iii
Nielsen
f02.tex
V4 - 07/21/2009
11:24am
About the Authors
of and successfully completed Microsoft’s globally coordinated intellectual property for the ‘‘SQL Server
2005/2008: Failover Clustering’’ workshop. He received his master’s degree in computer science from
University of Florida at Gainesville and is a Microsoft Certified Trainer (MCT) and Microsoft Certified IT
Professional (MCITP): Database Administrator 2008. He can be reached at [email protected]
Uttam wrote all but one of the chapters in Part VI, ‘‘Enterprise Data Management,’’ Chapter 39, and
Chapters 41 through 48.
Jacob Sebastian, SQL Server MVP, is a SQL Server Consultant specializing in XML based on
Ahmedabad, India, and has been using SQL Server since version 6.5. Jacob compressed his vast
knowledge of SQL Server and XML into Chapter 18.
Allen White, SQL Server MVP (with a minor in PowerShell), is a SQL Server Trainer with Scalability
Experts. He has worked as a database administrator, architect and developer for over 30 years and blogs
on www.SQLBlog.com. Allen expressed his passion for PowerShell in Chapter 7.
Michael White has focused on database development and administration since 1992. Concentrating
on Microsoft’s Business Intelligence (BI) tools and applications since 2000, he has architected and
implemented large warehousing and Analysis Services applications, as well as nontraditional applications
of BI tools. After many years in corporate IT and consulting, Mike currently works as a data architect
for IntrinsiQ, LLC. He is a strong advocate for the underused BI toolset and a frequent speaker at SQL
Server user groups and events. Mike wrote Chapter 37 and all the chapters (70 through 76) in Part X,
‘‘Business Intelligence.’’
About the Technical Reviewers
John Paul Cook is a database consultant based in Houston. His primary focus is on the development
and tuning of custom SQL Server–based solutions for large enterprise customers. As a three-time
Microsoft MVP for Virtual Machines, his secondary focus is using virtualization to facilitate application
testing. You can read his blog at http://sqlblog.com/blogs/john paul cook.
Hilary Cotter has been a SQL Server MVP for eight years and specializes in replication, high availability, and full-text search. He lives in New Jersey and loves to play the piano and spend time with his
wife and four kids. He has answered over 17,000 newsgroup questions, some of them correctly.
Louis Davidson has over 15 years of experience as a corporate database developer and architect. He
has been the principal author on four editions of a book on database design, including Professional
SQL Server 2008 Relational Database Design and Implementation. You can get more information about his
books, blogs, and more at his web page, drsql.org.
Rob Farley lives in Adelaide, Australia, where the sun always shines and it hasn’t rained (hardly at all)
for a very long time. He runs the Adelaide SQL Server User Group, operates a SQL Server consultancy
called LobsterPot Solutions, acts as a mentor for SQLskills Australia, and somehow finds time to be married with three amazing children. He is originally from the U.K., and his passions include serving at his
church and supporting Arsenal Football Club. He blogs at http://msmvps.com/blogs/robfarley
and can be reached via e-mail to [email protected]
Hongfei Guo is a senior program manager in the SQL Server Manageability Team. Prior to Microsoft,
she spent six years in database research and earned her PhD from University of Wisconsin at Madison.
Hongfei’s dissertation was ‘‘Data Quality Aware Caching’’ and she implemented it in the SQL Server
Engine code base while interning at Microsoft Research. For the SQL Server 2008 release, Hongfei was a
critical contributor to the Policy-Based Management feature (PBM) and witnessed its journey from birth
iv
Page iv
Nielsen
f02.tex
V4 - 07/21/2009
11:24am
About the Technical Reviewers
to product. For SQL Server 11, Hongfei will continue her role as feature owner of PBM, and is dedicated
to producing the next version that customers desire.
Allan Hirt has been using SQL Server in various guises since 1992. For the past 10 years, he has been
consulting, training, developing content, speaking at events, as well as authoring books, white papers,
and articles related to SQL Server architecture, high availability, administration, and more. His latest
book is Pro SQL Server 2008 Failover Clustering (Apress, 2009). Before forming Megahirtz in 2007, he
most recently worked for both Microsoft and Avanade, and still continues to work closely with Microsoft
on various projects, including contributing to the recently published SQL Server 2008 Upgrade Technical
Reference Guide. He can be contacted at [email protected] or via his website at www.sqlha.com.
Brian Kelley is a SQL Server author, blogger, columnist, and Microsoft MVP focusing primarily on SQL
Server security. He is a contributing author for How to Cheat at Securing SQL Server 2005 (Syngress,
2007) and Professional SQL Server 2008 Administration (Wrox, 2008). Brian currently serves as a database
administrator/architect in order to concentrate on his passion: SQL Server. He can be reached at
[email protected]
Jonathan Kehayias is a SQL Server MVP and MCITP Database Administrator and Developer, who got
started in SQL Server in 2004 as a database developer and report writer in the natural gas industry.
He has experience in upgrading and consolidating SQL environments, and experience in running SQL
Server in large virtual environments. His primary passion is performance tuning, and he frequently
rewrites queries for better performance and performs in-depth analysis of index implementation and
usage. He can be reached through his blog at http://sqlblog.com/blogs/jonathan kehayias.
Hugo Kornelius, lead technical editor, is co-founder and R&D lead of perFact BV, a Dutch company that improves analysis methods and develops computer-aided tools to generate completely
functional applications from the analysis deliverable on the SQL Server platform. In his spare
time, Hugo likes to share and enhance his knowledge of SQL Server by frequenting newsgroups
and forums, reading and writing books and blogs, and attending and speaking at conferences. As
a result of these activities, he has been awarded MVP status since January 2006. Hugo blogs at
http://sqlblog.com/blogs/hugo_kornelis. He can be reached by e-mail at [email protected]
Marco Shaw, ITIL, RHCE, LCP, MCP, has been working in the IT industry for over 10 years. Marco
runs a Virtual PowerShell User Group, and is one of the community directors of the PowerShell Community site www.powershellcommunity.org. Marco recently received the Microsoft MVP Award
for the second year in a row (2008/2009) for contributions to the Windows PowerShell Community.
Included in his recent authoring activities is writing PowerShell content for various books published in
2008 and 2009. Marco’s blog is at http://marcoshaw.blogspot.com.
Simon Sabine is Database Architect for SQL Know How, a SQL Server Consultancy and Training
provider in the U.K. He has particular expertise in the world of search, distributed architectures, business intelligence, and application development. He has worked with SQL Server since
1998, always focused on high-performance, reliable systems. Simon received the MVP award in
2006. He founded the first free SQL Server conference in the U.K., SQLBits, in 2007, along with
other MVPs in the U.K. He is a regular speaker at SQL Server events and maintains a blog at
www.sqlblogcasts.com/blogs/simons. He is married with children and lives in the U.K. You can
contact him at [email protected]
Peter Ward is the Chief Technical Architect for WARDY IT Solutions (www.wardyit.com). Peter
is an active member in the Australian SQL Server community, and president of the Queensland SQL
Server User Group. Peter is a highly regarded speaker at SQL Server events throughout Australia and is
a sought-after SQL Server consultant and trainer, providing solutions for some of the largest SQL Server
sites in Australia. He has been awarded Microsoft Most Valuable Professional status for his technical
excellence and commitment to the SQL Server community.
v
Page v
Nielsen
f03.tex
V4 - 07/21/2009
11:31am
Credits
Executive Editor
Bob Elliott
Editorial Director
Robyn B. Siesky
Senior Project Editor
Ami Frank Sullivan
Editorial Manager
Mary Beth Wakefield
Development Editor
Lori Cerreto
Production Manager
Tim Tate
Lead Technical Editor
Hugo Kornelius
Vice President and Executive Group Publisher
Richard Swadley
Technical Editors
John Paul Cook
Hilary Cotter
Louis Davidson
Rob Farley
Hongfei Guo
Allan Hirt
Jonathan Kehayias
Brian Kelley
Hugo Kornelius
Simon Sabine
Marco Shaw
Peter Ward
Vice President and Executive Publisher
Barry Pruett
Production Editor
Dassi Zeidel
Cover Design
Michael E. Trent
Copy Editor
Luann Rouff
Cover Image
Joyce Haughey
Associate Publisher
Jim Minatel
Project Coordinator, Cover
Lynsey Stanford
Proofreader
Publication Services, Inc.
Indexer
Jack Lewis
Page vi
Nielsen
f04.tex
V4 - 07/21/2009
11:36am
From Paul Nielsen: Of course, any book this size requires the efforts of several people.
Perhaps the greatest effort was made by my family as I spent long days, pulled all-nighters, and worked
straight through weekends in the SQL Dungeon to finish the book. My first thank-you must go to my
beautiful wife, Edie, and my kids, Lauren, David, and Dasha for their patience and love.
I also want to thank the folks at European Adoption Consultants who helped us with our adoption
of Dasha from Russia in 2007. Every tiny detail was professional and we couldn’t be more pleased. I
encourage every healthy family to adopt an orphan. I know it’s a stretch, but it’s well worth it.
This was the second book that I did with the same team at Wiley: Bob Elliot, Ami Frank Sullivan, Mary
Beth Wakefield, and Luann Rouff. What an amazing team, and I’m sure there are others with whom I
didn’t have direct contact. Ami is a pleasure to work with and one of the best editors in the business.
I’m a lucky author to work with her.
I’m also lucky enough to be an MVP. By far the best benefit of being an MVP is the private
newsgroup — reading the questions and dialogue between the MVPs more brilliant than me and
the Microsoft development team. When Louis, Erland, Aaron, Hugo, Linchi, Alex, Simon, Greg, Denis,
Adam, and, of course, Kalen, and the many others ask a question or dig into an issue, I pay attention.
Whenever the MVPs meet I always feel like a fortunate guest to be among such a smart and insightful
group.
Kalen Delaney deserves a special acknowledgment. Kalen is a gracious lady with the highest integrity, a
deep knowledge of SQL Server, and always a kind word. Thank you, Kalen.
Louis Davidson has become a good friend. We co-present at many conferences and I hope that he’s
grown from our respectful and friendly debates as much as I have. And if you ever get a chance to see
us on stage, be sure to ask Louis about denormalization. He’ll like that.
To the other authors who contributed to this book, I thank you: Mike White, Uttam Parui, Allen White,
Scott Klein, and Jacob Sebastian. Without you the book might not have come out before SQL 11 ships.
For any errors and omissions I take full credit; for what’s right in the book you should credit the
tech editors. I think it would be interesting to publish a book with all the tech editor comments and
suggestions. Some authors don’t like it when a tech editor disagrees or nitpicks. Personally, I think
that’s what makes a great tech editor, which is why I picked my friend Hugo Kornelius as the lead
tech editor for this book. Hugo’s a great tech editor, and this book had an incredible team of technical
editors — all experts, and all went above and beyond in their quest for perfection: Louis Davidson
(who tech edited the chapter on relational database design about five times!), Jonathan Kehayias,
Simon Sabin, Hilary Cotter, Hongfei Guo, Peter Ward, Allan Hirt, John Paul Cook, Brian Kelley, Rob
Farley, and Marco Shaw. I’m also honored to acknowledge the many comments, recommendations,
and encouragement I’ve received from loyal readers. A few readers volunteered to help me polish this
edition by serving as a pre-editorial board, adding significant comments and feedback to early versions
vii
Page vii
Nielsen
f04.tex
V4 - 07/21/2009
11:36am
Acknowledgments
of various chapters for this book: JJ Bienn, Viktor Gurevich, Steve Miller, Greg Low, Aaron Bertrand,
Adam Greifer, Alan Horsman, Andrew Novick, Degmar Barbosa, Mesut Demir, Denis Gobo, Dominique
Verrière, Erin Welker, Henry S. Hayden, James Beidleman, Joe Webb, Ken Scott, Kevin Cox, Kevin
Lambert, Michael Shaya, Michael Wiles, Scott Stonehouse, and Scott Whigham. Thank you, all.
I really enjoy teaching and sharing SQL Server in the classroom, in seminars, at conferences, and on
the street corner. To everyone who’s joined me in these settings, thanks for your participation and
enthusiasm.
To the many who contributed efforts to the two previous editions, thank you. This work builds on your
foundation.
This was the second book that I did with the same team at Wiley: Bob Elliott, Ami Frank Sullivan, Mary
Beth Wakefield, Dassi Zeidel, and Luann Rouff.
Finally, a warm thank you goes out to the Microsoft SQL Server team in Redmond. Thanks for building
such a great database engine. Thanks for your close relationships with the MVPs. And thanks to those
team members who spent time with me individually: Buck Woody, Hongfei Guo, Ed Lehman.
For those of you who follow me on Twitter and read my daily tweets about writing, this book was powered by Dr. Pepper. And now, I have a stunt kite that has been in its case far too long — there’s a fair
wind outside somewhere and I’m going to find it.
From Uttam Parui: I would like to thank my parents for their endless love and support and for giving
me the best education they could provide, which has made me successful in life. I’d like to thank my
loving wife, Shyama, and my two doting daughters, Noyonika and Niharika, for all their encouragement
and understanding while I spent many nights and weekends working on the book. I would also like to
thank Paul Nielsen, the lead author, for giving me this great opportunity to co-author this book, and for
his support throughout the writing of it. Last but not least, I would like to thank everyone at Wiley for
their help with this book.
viii
Page viii
Nielsen
CAG.tex
V3 - 07/10/2009
2:37pm
Foreword ...................................................................................................................................................xxxiii
Introduction ............................................................................................................................................... xxxv
Part I Laying the Foundation
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
1:
2:
3:
4:
5:
6:
7:
The World of SQL Server ................................................................................................................................. 3
Data Architecture ............................................................................................................................................. 27
Relational Database Design ..............................................................................................................................43
Installing SQL Server 2008 ............................................................................................................................. 73
Client Connectivity .......................................................................................................................................... 93
Using Management Studio ............................................................................................................................ 101
Scripting with PowerShell ............................................................................................................................. 129
Part II Manipulating Data with Select
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
8: Introducing Basic Query Flow ......................................................................................................................167
9: Data Types, Expressions, and Scalar Functions ...........................................................................................197
10: Merging Data with Joins and Unions .........................................................................................................227
11: Including Data with Subqueries and CTEs ................................................................................................259
12: Aggregating Data ..........................................................................................................................................289
13: Windowing and Ranking ............................................................................................................................ 319
14: Projecting Data Through Views .................................................................................................................. 329
15: Modifying Data ............................................................................................................................................ 347
16: Modification Obstacles ................................................................................................................................ 377
Part III Beyond Relational
Chapter 17: Traversing Hierarchies ................................................................................................................................. 399
Chapter 18: Manipulating XML Data .............................................................................................................................. 435
Chapter 19: Using Integrated Full-Text Search .............................................................................................................. 491
Part IV Developing with SQL Server
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
Creating the Physical Database Schema ..................................................................................................... 513
Programming with T-SQL ...........................................................................................................................559
Kill the Cursor! ............................................................................................................................................579
T-SQL Error Handling ................................................................................................................................ 593
Developing Stored Procedures .................................................................................................................... 607
Building User-Defined Functions ................................................................................................................623
Creating DML Triggers ................................................................................................................................635
DDL Triggers ............................................................................................................................................... 657
Building Out the Data Abstraction Layer ...................................................................................................665
Dynamic SQL and Code Generation ..........................................................................................................673
Part V Data Connectivity
Chapter
Chapter
Chapter
Chapter
Chapter
30:
31:
32:
33:
34:
Bulk Operations ...........................................................................................................................................685
Executing Distributed Queries .................................................................................................................... 691
Programming with ADO.NET 3.5 .............................................................................................................. 715
Sync Framework .......................................................................................................................................... 751
LINQ ............................................................................................................................................................ 775
ix
Page ix
Nielsen
CAG.tex
V3 - 07/10/2009
2:37pm
Contents at a Glance
Chapter
Chapter
Chapter
Chapter
35:
36:
37:
38:
Asynchronous Messaging with Service Broker ...........................................................................................807
Replicating Data ...........................................................................................................................................813
Performing ETL with Integration Services ................................................................................................. 829
Access as a Front End to SQL Server ........................................................................................................867
Part VI Enterprise Data Management
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
Configuring SQL Server .............................................................................................................................. 883
Policy-Based Management ........................................................................................................................... 939
Recovery Planning ....................................................................................................................................... 953
Maintaining the Database ............................................................................................................................985
Automating Database Maintenance with SQL Server Agent ................................................................... 1011
Transferring Databases .............................................................................................................................. 1039
Database Snapshots ................................................................................................................................... 1059
Log Shipping ..............................................................................................................................................1069
Database Mirroring .................................................................................................................................... 1091
Clustering ...................................................................................................................................................1119
Part VII Security
Chapter
Chapter
Chapter
Chapter
49:
50:
51:
52:
Authenticating Principals ...........................................................................................................................1169
Authorizing Securables .............................................................................................................................. 1187
Data Cryptography .................................................................................................................................... 1197
Row-Level Security .................................................................................................................................... 1205
Part VIII Monitoring and Auditing
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
Data Audit Triggers ................................................................................................................................... 1223
Schema Audit Triggers .............................................................................................................................. 1233
Performance Monitor .................................................................................................................................1237
Tracing and Profiling .................................................................................................................................1243
Wait States ................................................................................................................................................. 1255
Extended Events ........................................................................................................................................ 1261
Change Tracking ........................................................................................................................................1267
Change Data Capture ................................................................................................................................ 1285
SQL Audit .................................................................................................................................................. 1297
Management Data Warehouse .................................................................................................................. 1305
Part IX Performance Tuning and Optimization
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
63:
64:
65:
66:
67:
68:
69:
Interpreting Query Execution Plans ......................................................................................................... 1313
Indexing Strategies .....................................................................................................................................1321
Query Plan Reuse ...................................................................................................................................... 1357
Managing Transactions, Locking, and Blocking ...................................................................................... 1363
Data Compression ..................................................................................................................................... 1415
Partitioning .................................................................................................................................................1427
Resource Governor .................................................................................................................................... 1453
Part X Business Intelligence
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
70:
71:
72:
73:
74:
75:
76:
BI Design ....................................................................................................................................................1461
Building Multidimensional Cubes with Analysis Services .......................................................................1469
Programming MDX Queries ......................................................................................................................1509
Authoring Reports with Reporting Services ............................................................................................. 1527
Administering Reporting Services ............................................................................................................. 1559
Analyzing Data with Excel ........................................................................................................................1577
Data Mining with Analysis Services ......................................................................................................... 1593
Appendix A: SQL Server 2008 Specifications ..........................................................................................1613
Appendix B: Using the Sample Databases ................................................................................................1619
Index ..........................................................................................................................................................1625
x
Page x
Nielsen
Foreword
ftoc.tex
V4 - 07/21/2009
3:36pm
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxiii
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxv
Introduction
Part I Laying the Foundation
Chapter 1: The World of SQL Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
A Great Choice ...................................................................................................................................................................................4
SQL Server Database Engine ............................................................................................................................................................. 6
Database Engine ....................................................................................................................................................................6
Transact-SQL ......................................................................................................................................................................... 8
Policy-Based Management .................................................................................................................................................. 10
.NET Common Language Runtime ................................................................................................................................... 10
Service Broker ..................................................................................................................................................................... 11
Replication services .............................................................................................................................................................11
Integrated Full-Text Search ................................................................................................................................................11
Server management objects ................................................................................................................................................12
Filestream ............................................................................................................................................................................ 12
SQL Server Services ......................................................................................................................................................................... 12
SQL Server Agent ............................................................................................................................................................... 12
Database Mail ......................................................................................................................................................................12
Distributed Transaction Coordinator (DTC) .....................................................................................................................12
Business Intelligence .........................................................................................................................................................................14
Business Intelligence Development Studio ........................................................................................................................14
Integration Services .............................................................................................................................................................14
Analysis Services ................................................................................................................................................................. 15
Reporting Services ...............................................................................................................................................................17
UI and Tools .................................................................................................................................................................................... 17
SQL Server Management Studio ........................................................................................................................................18
SQL Server Configuration Manager ...................................................................................................................................18
SQL Profiler/Trace ...............................................................................................................................................................19
Performance Monitor ..........................................................................................................................................................19
Command-line utilities ....................................................................................................................................................... 19
Books Online .......................................................................................................................................................................20
SQL Server Editions .........................................................................................................................................................................20
Exploring the Metadata ....................................................................................................................................................................22
System databases .................................................................................................................................................................22
Metadata views ....................................................................................................................................................................22
What’s New? .....................................................................................................................................................................................23
Summary ........................................................................................................................................................................................... 26
Chapter 2: Data Architecture
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Information Architecture Principle ..................................................................................................................................................28
Database Objectives ..........................................................................................................................................................................28
Usability ...............................................................................................................................................................................29
Extensibility ......................................................................................................................................................................... 29
Data integrity .......................................................................................................................................................................30
Performance/scalability ........................................................................................................................................................31
Availability ...........................................................................................................................................................................31
Security ................................................................................................................................................................................31
Smart Database Design .....................................................................................................................................................................33
Database system .................................................................................................................................................................. 34
Physical schema .................................................................................................................................................................. 36
xi
Page xi
Nielsen
ftoc.tex
V4 - 07/21/2009
3:36pm
Contents
Set-based queries ................................................................................................................................................................ 37
Indexing ...............................................................................................................................................................................37
Concurrency ........................................................................................................................................................................ 38
Advanced scalability ............................................................................................................................................................38
A performance framework ................................................................................................................................................. 39
Issues and objections ..........................................................................................................................................................40
Summary ........................................................................................................................................................................................... 40
Chapter 3: Relational Database Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Database Basics ................................................................................................................................................................................. 43
Benefits of a digital database .............................................................................................................................................44
Tables, rows, columns ........................................................................................................................................................45
Database design phases ...................................................................................................................................................... 46
Normalization ......................................................................................................................................................................47
The three ‘‘Rules of One’’ .................................................................................................................................................. 47
Identifying entities .............................................................................................................................................................. 48
Generalization ......................................................................................................................................................................49
Primary keys ....................................................................................................................................................................... 51
Foreign keys ........................................................................................................................................................................52
Cardinality ........................................................................................................................................................................... 52
Optionality ...........................................................................................................................................................................52
Data Design Patterns ........................................................................................................................................................................55
One-to-many pattern .......................................................................................................................................................... 55
One-to-one pattern ............................................................................................................................................................. 56
Many-to-many pattern ........................................................................................................................................................56
Supertype/subtype pattern ..................................................................................................................................................59
Domain integrity lookup pattern .......................................................................................................................................61
Recursive pattern ................................................................................................................................................................ 61
Database design layers ........................................................................................................................................................65
Normal Forms ...................................................................................................................................................................................65
First normal form (1NF) ....................................................................................................................................................66
Second normal form (2NF) ............................................................................................................................................... 67
Third normal form (3NF) ..................................................................................................................................................69
The Boyce-Codd normal form (BCNF) .............................................................................................................................70
Fourth normal form (4NF) ................................................................................................................................................70
Fifth normal form (5NF) ................................................................................................................................................... 71
Summary ........................................................................................................................................................................................... 71
Chapter 4: Installing SQL Server 2008 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Selecting Server Hardware ............................................................................................................................................................... 73
CPU planning ......................................................................................................................................................................73
Copious memory .................................................................................................................................................................74
Disk-drive subsystems ........................................................................................................................................................ 74
Network performance .........................................................................................................................................................76
Preparing the Server .........................................................................................................................................................................78
Dedicated server ..................................................................................................................................................................78
Operating system ................................................................................................................................................................ 79
Service accounts ..................................................................................................................................................................79
Server instances ...................................................................................................................................................................79
Performing the Installation .............................................................................................................................................................. 81
Attended installations ......................................................................................................................................................... 81
Unattended installations ..................................................................................................................................................... 85
Remote installations ............................................................................................................................................................86
Upgrading from Previous Versions ................................................................................................................................................. 86
Upgrading from SQL Server 2005 ....................................................................................................................................87
Migrating to SQL Server ..................................................................................................................................................................87
Migrating from Access ........................................................................................................................................................87
Migration Assistant ............................................................................................................................................................. 88
Removing SQL Server ...................................................................................................................................................................... 90
Summary ........................................................................................................................................................................................... 91
Chapter 5: Client Connectivity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
Enabling Server Connectivity ...........................................................................................................................................................93
Server Configuration Manager ........................................................................................................................................... 94
SQL Native Client Connectivity (SNAC) ..........................................................................................................................95
xii
Page xii
Nielsen
ftoc.tex
V4 - 07/21/2009
3:36pm
Contents
SQL Server Native Client Features ................................................................................................................................................. 96
Requirements .......................................................................................................................................................................97
Asynchronous operations ................................................................................................................................................... 97
Multiple Active Result Sets (MARS) ..................................................................................................................................97
XML data types ...................................................................................................................................................................98
User-defined types .............................................................................................................................................................. 98
Large value types ................................................................................................................................................................98
Handling expired passwords ..............................................................................................................................................98
Snapshot isolation ...............................................................................................................................................................99
Summary ........................................................................................................................................................................................... 99
Chapter 6: Using Management Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Organizing the Interface ................................................................................................................................................................ 104
Window placement ...........................................................................................................................................................104
The Context Menu ........................................................................................................................................................... 106
Registered Servers ...........................................................................................................................................................................107
Managing Servers ..............................................................................................................................................................107
Server Groups ................................................................................................................................................................... 109
Object Explorer .............................................................................................................................................................................. 110
Navigating the tree ........................................................................................................................................................... 110
Filtering Object Explorer ................................................................................................................................................. 113
Object Explorer Details ....................................................................................................................................................113
The Table Designer .......................................................................................................................................................... 114
Building database diagrams ..............................................................................................................................................115
The Query Designer ......................................................................................................................................................... 116
Object Explorer reports ....................................................................................................................................................118
Using the Query Editor .................................................................................................................................................................119
Opening a query connecting to a server ....................................................................................................................... 119
Opening a .sql file ........................................................................................................................................................... 119
Shortcuts and bookmarks ................................................................................................................................................ 121
Query options ................................................................................................................................................................... 122
Executing SQL batches .....................................................................................................................................................123
Results! .............................................................................................................................................................................. 123
Viewing query execution plans ....................................................................................................................................... 123
Using the Solution Explorer ..........................................................................................................................................................125
Jump-Starting Code with Templates .............................................................................................................................................126
Using templates .................................................................................................................................................................126
Managing templates .......................................................................................................................................................... 126
Summary ......................................................................................................................................................................................... 126
Chapter 7: Scripting with PowerShell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Why Use PowerShell? .................................................................................................................................................................... 130
Basic PowerShell .............................................................................................................................................................................130
Language features ..............................................................................................................................................................130
Creating scripts ................................................................................................................................................................. 137
Communicating with SQL Server ................................................................................................................................................. 142
SQL Server Management Objects ....................................................................................................................................142
ADO.NET .......................................................................................................................................................................... 147
Scripting SQL Server Tasks ...........................................................................................................................................................150
Administrative tasks ..........................................................................................................................................................150
Data-based tasks ............................................................................................................................................................... 159
SQL Server PowerShell Extensions ............................................................................................................................................... 161
SQLPS.exe ..........................................................................................................................................................................161
The SQL PSDrive — SQLSERVER: .................................................................................................................................162
SQL cmdlets ......................................................................................................................................................................162
Summary ......................................................................................................................................................................................... 164
Part II Manipulating Data with Select
Chapter 8: Introducing Basic Query Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
Understanding Query Flow ........................................................................................................................................................... 168
Syntactical flow of the query statement ......................................................................................................................... 168
A graphical view of the query statement .......................................................................................................................169
xiii
Page xiii
Nielsen
ftoc.tex
V4 - 07/21/2009
3:36pm
Contents
Logical flow of the query statement ...............................................................................................................................170
Physical flow of the query statement ............................................................................................................................. 171
From Clause Data Sources ............................................................................................................................................................ 171
Possible data sources ........................................................................................................................................................171
Table aliases ...................................................................................................................................................................... 173
[Table Name] .................................................................................................................................................................... 173
Fully qualified names .......................................................................................................................................................174
Where Conditions .......................................................................................................................................................................... 175
Using the between search condition ...............................................................................................................................176
Comparing with a list ......................................................................................................................................................176
Using the like search condition ...................................................................................................................................... 179
Multiple where conditions ............................................................................................................................................... 181
Select...where .....................................................................................................................................................................182
Columns, Stars, Aliases, and Expressions .................................................................................................................................... 183
The star ............................................................................................................................................................................. 183
Aliases ................................................................................................................................................................................183
Qualified columns .............................................................................................................................................................184
Ordering the Result Set .................................................................................................................................................................185
Specifying the order by using column names ...............................................................................................................185
Specifying the order by using expressions .....................................................................................................................187
Specifying the order by using column aliases ............................................................................................................... 188
Using the column ordinal position .................................................................................................................................188
Order by and collation .................................................................................................................................................... 189
Select Distinct ................................................................................................................................................................................. 191
Top () ..............................................................................................................................................................................................192
The with ties option ........................................................................................................................................................ 193
Selecting a random row ...................................................................................................................................................194
Summary ......................................................................................................................................................................................... 195
Chapter 9: Data Types, Expressions, and Scalar Functions . . . . . . . . . . . . . . . . . . . . . . . 197
Building Expressions ...................................................................................................................................................................... 197
Operators ...........................................................................................................................................................................198
Bitwise operators ...............................................................................................................................................................199
Case expressions ............................................................................................................................................................... 202
Working with nulls .......................................................................................................................................................... 204
Scalar Functions ............................................................................................................................................................................. 209
User information functions .............................................................................................................................................. 210
Date and time functions .................................................................................................................................................. 210
String Functions ............................................................................................................................................................................. 214
Soundex Functions .........................................................................................................................................................................218
Using the SOUNDEX() function ..................................................................................................................................... 220
Using the DIFFERENCE() Soundex function .................................................................................................................221
Data-Type Conversion Functions .................................................................................................................................................. 222
Server Environment Information ...................................................................................................................................................224
Summary ......................................................................................................................................................................................... 225
Chapter 10: Merging Data with Joins and Unions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
Using Joins ......................................................................................................................................................................................228
Inner Joins ...................................................................................................................................................................................... 230
Building inner joins with the Query Designer .............................................................................................................. 231
Creating inner joins within SQL code ........................................................................................................................... 231
Number of rows returned ................................................................................................................................................233
ANSI SQL 89 joins .......................................................................................................................................................... 234
Multiple data source joins ............................................................................................................................................... 235
Outer Joins ......................................................................................................................................................................................236
Using the Query Designer to create outer joins ........................................................................................................... 238
T-SQL code and outer joins ............................................................................................................................................238
Outer joins and optional foreign keys ........................................................................................................................... 240
Full outer joins .................................................................................................................................................................241
Red thing blue thing ........................................................................................................................................................241
Placing the conditions within outer joins ......................................................................................................................243
Multiple outer joins ..........................................................................................................................................................244
Self-Joins ..........................................................................................................................................................................................244
Cross (Unrestricted) Joins ..............................................................................................................................................................247
xiv
Page xiv
Nielsen
ftoc.tex
V4 - 07/21/2009
3:36pm
Contents
Exotic Joins .....................................................................................................................................................................................248
Multiple-condition joins ................................................................................................................................................... 248
(theta) joins ..................................................................................................................................................................249
Non-key joins ................................................................................................................................................................... 249
Set Difference Queries ....................................................................................................................................................................250
Left set difference query .................................................................................................................................................. 251
Full set difference queries ................................................................................................................................................252
Using Unions ..................................................................................................................................................................................254
Union [All] ........................................................................................................................................................................254
Intersection union .............................................................................................................................................................255
Difference union/except ....................................................................................................................................................256
Summary ......................................................................................................................................................................................... 257
Chapter 11: Including Data with Subqueries and CTEs . . . . . . . . . . . . . . . . . . . . . . . . . 259
Methods and Locations ..................................................................................................................................................................259
Simple Subqueries .......................................................................................................................................................................... 262
Common table expressions .............................................................................................................................................. 264
Using scalar subqueries ....................................................................................................................................................265
Using subqueries as lists ..................................................................................................................................................267
Using subqueries as tables ...............................................................................................................................................271
Row constructors .............................................................................................................................................................. 273
All, some, and any ...........................................................................................................................................................274
Correlated Subqueries .................................................................................................................................................................... 276
Correlating in the where clause ......................................................................................................................................276
Correlating a derived table using apply .........................................................................................................................280
Relational Division ..........................................................................................................................................................................281
Relational division with a remainder ..............................................................................................................................282
Exact relational division ...................................................................................................................................................285
Composable SQL ............................................................................................................................................................................286
Summary ......................................................................................................................................................................................... 288
Chapter 12: Aggregating Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
Simple Aggregations ....................................................................................................................................................................... 289
Basic aggregations ............................................................................................................................................................. 290
Aggregates, averages, and nulls ....................................................................................................................................... 293
Using aggregate functions within the Query Designer ..................................................................................................293
Beginning statistics ............................................................................................................................................................294
Grouping within a Result Set ....................................................................................................................................................... 295
Simple groupings .............................................................................................................................................................. 296
Grouping sets ....................................................................................................................................................................297
Filtering grouped results ..................................................................................................................................................298
Aggravating Queries ....................................................................................................................................................................... 299
Including group by descriptions ..................................................................................................................................... 299
Including all group by values ......................................................................................................................................... 301
Nesting aggregations .........................................................................................................................................................303
Including detail descriptions ............................................................................................................................................304
OLAP in the Park .......................................................................................................................................................................... 306
Rollup subtotals ................................................................................................................................................................ 306
Cube queries ..................................................................................................................................................................... 308
Building Crosstab Queries ............................................................................................................................................................. 309
Pivot method .....................................................................................................................................................................309
Case expression method ...................................................................................................................................................311
Dynamic crosstab queries ................................................................................................................................................ 312
Unpivot ..............................................................................................................................................................................313
Cumulative Totals (Running Sums) ..............................................................................................................................................314
Correlated subquery solution ...........................................................................................................................................315
T-SQL cursor solution ......................................................................................................................................................315
Multiple assignment variable solution .............................................................................................................................316
Summary ......................................................................................................................................................................................... 317
Chapter 13: Windowing and Ranking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
Windowing ......................................................................................................................................................................................319
The Over() clause .............................................................................................................................................................320
Partitioning within the window ...................................................................................................................................... 321
xv
Page xv
Nielsen
ftoc.tex
V4 - 07/21/2009
3:36pm
Contents
Ranking Functions ..........................................................................................................................................................................322
Row number() function ................................................................................................................................................... 322
Rank() and dense_rank() functions .................................................................................................................................324
Ntile() function ................................................................................................................................................................. 325
Aggregate Functions ......................................................................................................................................................... 326
Summary ......................................................................................................................................................................................... 327
Chapter 14: Projecting Data Through Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
Why Use Views? ............................................................................................................................................................................ 329
The Basic View ...............................................................................................................................................................................331
Creating views using the Query Designer ......................................................................................................................332
Creating views with DDL code .......................................................................................................................................334
Executing views ................................................................................................................................................................ 334
Altering and dropping a view .........................................................................................................................................336
A Broader Point of View ............................................................................................................................................................... 336
Column aliases ..................................................................................................................................................................337
Order by and views ......................................................................................................................................................... 337
View restrictions ............................................................................................................................................................... 338
Nesting views ....................................................................................................................................................................338
Updating through views ...................................................................................................................................................340
Views and performance ....................................................................................................................................................341
Locking Down the View ................................................................................................................................................................341
Unchecked data ................................................................................................................................................................ 342
Protecting the data ........................................................................................................................................................... 342
Protecting the view ...........................................................................................................................................................343
Encrypting the view’s select statement ........................................................................................................................... 344
Application metadata ........................................................................................................................................................345
Using Synonyms .............................................................................................................................................................................345
Summary ......................................................................................................................................................................................... 346
Chapter 15: Modifying Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
Inserting Data ................................................................................................................................................................................. 348
Inserting simple rows of values ...................................................................................................................................... 349
Inserting a result set from select .................................................................................................................................... 352
Inserting the result set from a stored procedure .......................................................................................................... 353
Creating a default row .....................................................................................................................................................355
Creating a table while inserting data ..............................................................................................................................355
Updating Data .................................................................................................................................................................................358
Updating a single table ....................................................................................................................................................359
Performing global search and replace .............................................................................................................................360
Referencing multiple tables while updating data ...........................................................................................................360
Deleting Data .................................................................................................................................................................................. 365
Referencing multiple data sources while deleting ..........................................................................................................366
Cascading deletes ..............................................................................................................................................................367
Alternatives to physically deleting data .......................................................................................................................... 368
Merging Data .................................................................................................................................................................................. 369
Returning Modified Data ............................................................................................................................................................... 373
Returning data from an insert .........................................................................................................................................373
Returning data from an update .......................................................................................................................................374
Returning data from a delete .......................................................................................................................................... 374
Returning data from a merge ..........................................................................................................................................374
Returning data into a table ............................................................................................................................................. 375
Summary ......................................................................................................................................................................................... 376
Chapter 16: Modification Obstacles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
Data Type/Length ........................................................................................................................................................................... 377
Primary Key Constraint and Unique Constraint ......................................................................................................................... 378
Identity columns ...............................................................................................................................................................379
Globally unique identifiers (GUIDs) ............................................................................................................................... 381
Deleting Duplicate Rows ................................................................................................................................................................383
Deleting duplicate rows using windowing ..................................................................................................................... 384
Deleting duplicate rows using a surrogate key ..............................................................................................................385
Deleting duplicate rows using select distant into ..........................................................................................................386
Foreign Key Constraints ................................................................................................................................................................ 387
Null and Default Constraints ........................................................................................................................................................ 389
Check Constraints .......................................................................................................................................................................... 389
xvi
Page xvi
Nielsen
ftoc.tex
V4 - 07/21/2009
3:36pm
Contents
Instead of Triggers ......................................................................................................................................................................... 390
After Triggers ..................................................................................................................................................................................391
Non-Updateable Views ...................................................................................................................................................................392
Views With Check Option ............................................................................................................................................................393
Calculated Columns ....................................................................................................................................................................... 394
Security Constraints ........................................................................................................................................................................395
Summary ......................................................................................................................................................................................... 396
Part III Beyond Relational
Chapter 17: Traversing Hierarchies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
Adjacency List Pattern ....................................................................................................................................................................401
Single-level queries ........................................................................................................................................................... 404
Subtree queries ................................................................................................................................................................. 407
Is the node an ancestor? ................................................................................................................................................. 415
Determining the node’s level ...........................................................................................................................................415
Reparenting the adjacency list .........................................................................................................................................415
Indexing an adjacency list ............................................................................................................................................... 416
Cyclic errors ......................................................................................................................................................................416
Adjacency list variations ...................................................................................................................................................417
Adjacency list pros and cons .......................................................................................................................................... 419
The Materialized-Path Pattern ........................................................................................................................................................419
Subtree queries ................................................................................................................................................................. 422
Is the node in the subtree? .............................................................................................................................................424
Determining the node level ............................................................................................................................................. 425
Single-level queries ........................................................................................................................................................... 426
Reparenting the materialized path ...................................................................................................................................427
Indexing the materialized path ........................................................................................................................................427
Materialized path pros and cons .....................................................................................................................................427
Using the New HierarchyID ..........................................................................................................................................................428
Selecting a single node .................................................................................................................................................... 430
Scanning for ancestors ..................................................................................................................................................... 430
Performing a subtree search ............................................................................................................................................ 431
Single-level searches ......................................................................................................................................................... 431
Inserting new nodes ......................................................................................................................................................... 432
Performance .......................................................................................................................................................................432
HierarchyID pros and cons ............................................................................................................................................. 433
Summary ......................................................................................................................................................................................... 433
Chapter 18: Manipulating XML Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
XML Processing in SQL Server 2008 ...........................................................................................................................................436
Generating XML documents ............................................................................................................................................ 437
Querying XML documents ...............................................................................................................................................437
Validating XML documents ..............................................................................................................................................438
Sample Tables and Data ................................................................................................................................................................440
XML Data Type ..............................................................................................................................................................................441
Typed and untyped XML ................................................................................................................................................ 442
Creating and using XML columns .................................................................................................................................. 443
Declaring and using XML variables ................................................................................................................................ 444
Using XML parameters and return values ......................................................................................................................445
Loading/querying XML documents from disk files ........................................................................................................446
Limitations of the XML data type ...................................................................................................................................447
Understanding XML Data Type Methods .....................................................................................................................................448
XPath ................................................................................................................................................................................. 449
value() ................................................................................................................................................................................449
nodes() ...............................................................................................................................................................................449
exist() .................................................................................................................................................................................450
query() ............................................................................................................................................................................... 451
modify() .............................................................................................................................................................................451
Joining XML nodes with relational tables ......................................................................................................................451
Using variables and filters in XQuery expressions ........................................................................................................452
Accessing the parent node ...............................................................................................................................................454
Generating XML Output Using FOR XML .................................................................................................................................. 455
FOR XML AUTO ..............................................................................................................................................................455
FOR XML RAW ................................................................................................................................................................457
xvii
Page xvii
Nielsen
ftoc.tex
V4 - 07/21/2009
3:36pm
Contents
FOR XML EXPLICIT ........................................................................................................................................................458
FOR XML PATH ...............................................................................................................................................................464
TYPE directive ...................................................................................................................................................................468
XSINIL Directive ...............................................................................................................................................................469
Generating XML Schema information .............................................................................................................................470
Generating XML namespaces ...........................................................................................................................................471
Understanding XQuery and FLWOR operations ......................................................................................................................... 472
Simple queries ...................................................................................................................................................................472
FLWOR operation .............................................................................................................................................................473
What’s new for XQuery in SQL Server 2008 ............................................................................................................... 475
Understanding XQuery Functions .................................................................................................................................................476
String functions .................................................................................................................................................................476
Numeric and aggregate functions ....................................................................................................................................476
Other functions .................................................................................................................................................................477
Performing XML Data Modification ..............................................................................................................................................477
Insert operation .................................................................................................................................................................478
Update operation .............................................................................................................................................................. 478
Delete operation ................................................................................................................................................................479
What’s new for XML DML operations in SQL Server 2008 ........................................................................................479
Handling Namespaces .................................................................................................................................................................... 480
Shredding XML Using OPENXML() ............................................................................................................................................. 481
XSD and XML Schema Collections .............................................................................................................................................. 483
Creating an XML Schema collection ...............................................................................................................................483
Creating typed XML columns and variables ..................................................................................................................484
Performing validation ....................................................................................................................................................... 484
XML DOCUMENT and CONTENT ................................................................................................................................485
Altering XML Schema collections ....................................................................................................................................485
What’s in the ‘‘collection’’? .............................................................................................................................................. 485
What’s new in SQL Server 2008 for XSD .....................................................................................................................487
Understanding XML Indexes ......................................................................................................................................................... 487
XML Best Practices .........................................................................................................................................................................488
Summary ......................................................................................................................................................................................... 489
Chapter 19: Using Integrated Full-Text Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491
Configuring Full-Text Search Catalogs ......................................................................................................................................... 494
Creating a catalog with the wizard .................................................................................................................................494
Creating a catalog with T-SQL code .............................................................................................................................. 496
Pushing data to the full-text index .................................................................................................................................496
Maintaining a catalog with Management Studio ............................................................................................................497
Maintaining a catalog in T-SQL code .............................................................................................................................498
Word Searches ................................................................................................................................................................................498
The Contains function ..................................................................................................................................................... 498
The ContainsTable function .............................................................................................................................................499
Advanced Search Options ..............................................................................................................................................................501
Multiple-word searches .....................................................................................................................................................501
Searches with wildcards ...................................................................................................................................................502
Phrase searches ................................................................................................................................................................. 502
Word-proximity searches ................................................................................................................................................. 502
Word-inflection searches ..................................................................................................................................................504
Thesaurus searches ........................................................................................................................................................... 504
Variable-word-weight searches .........................................................................................................................................505
Fuzzy Searches ................................................................................................................................................................................507
Freetext ..............................................................................................................................................................................507
FreetextTable ..................................................................................................................................................................... 507
Performance .....................................................................................................................................................................................508
Summary ......................................................................................................................................................................................... 508
Part IV Developing with SQL Server
Chapter 20: Creating the Physical Database Schema . . . . . . . . . . . . . . . . . . . . . . . . . . 513
Designing the Physical Database Schema .....................................................................................................................................514
Logical to physical options ..............................................................................................................................................515
Refining the data patterns ................................................................................................................................................515
Designing for performance ...............................................................................................................................................515
xviii
Page xviii
Was this manual useful for you? yes no
Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Download PDF

advertisement