Nielsen CAG.tex V3 - 07/10/2009 2:37pm Page x Contents at a Glance Chapter 35: Asynchronous Messaging with Service Broker 807 Chapter 36: Replicating Data 813 Chapter 37: Performing ETL with Integration Services 829 Chapter 38: Access as a Front End to SQL Server 867 Part VI Enterprise Data Management Chapter 39: Configuring SQL Server 883 Chapter 40: Policy-Based Management 939 Chapter 41: Recovery Planning 953 Chapter 42: Maintaining the Database 985 Chapter 43: Automating Database Maintenance with SQL Server Agent 1011 Chapter 44: Transferring Databases 1039 Chapter 45: Database Snapshots 1059 Chapter 46: Log Shipping 1069 Chapter 47: Database Mirroring 1091 Chapter 48: Clustering 1119 Part VII Security Chapter 49: Authenticating Principals 1169 Chapter 50: Authorizing Securables 1187 Chapter 51: Data Cryptography 1197 Chapter 52: Row-Level Security 1205 Part VIII Monitoring and Auditing Chapter 53: Data Audit Triggers 1223 Chapter 54: Schema Audit Triggers 1233 Chapter 55: Performance Monitor 1237 Chapter 56: Tracing and Profiling 1243 Chapter 57: Wait States 1255 Chapter 58: Extended Events 1261 Chapter 59: Change Tracking 1267 Chapter 60: Change Data Capture 1285 Chapter 61: SQL Audit 1297 Chapter 62: Management Data Warehouse 1305 Part IX Performance Tuning and Optimization Chapter 63: Interpreting Query Execution Plans 1313 Chapter 64: Indexing Strategies 1321 Chapter 65: Query Plan Reuse 1357 Chapter 66: Managing Transactions, Locking, and Blocking 1363 Chapter 67: Data Compression 1415 Chapter 68: Partitioning 1427 Chapter 69: Resource Governor 1453 Part X Business Intelligence Chapter 70: BI Design 1461 Chapter 71: Building Multidimensional Cubes with Analysis Services 1469 Chapter 72: Programming MDX Queries 1509 Chapter 73: Authoring Reports with Reporting Services 1527 Chapter 74: Administering Reporting Services 1559 Chapter 75: Analyzing Data with Excel 1577 Chapter 76: Data Mining with Analysis Services 1593 Appendix A: SQL Server 2008 Specifications 1613 Appendix B: Using the Sample Databases 1619 Index 1625 x www.getcoolebook.com Nielsen ftoc.tex V4 - 07/21/2009 3:36pm Page xi Foreword xxxiii Introduction xxxv 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 www.getcoolebook.com Nielsen ftoc.tex V4 - 07/21/2009 3:36pm Page xii 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 www.getcoolebook.com Nielsen ftoc.tex V4 - 07/21/2009 3:36pm Page xiii 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 UsingtheQueryEditor 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 TheSQLPSDrive—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 www.getcoolebook.com Nielsen ftoc.tex V4 - 07/21/2009 3:36pm Page xiv 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 www.getcoolebook.com Nielsen ftoc.tex V4 - 07/21/2009 3:36pm Page xv 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 www.getcoolebook.com Nielsen ftoc.tex V4 - 07/21/2009 3:36pm Page xvi 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 www.getcoolebook.com Nielsen ftoc.tex V4 - 07/21/2009 3:36pm Page xvii 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 www.getcoolebook.com Nielsen ftoc.tex V4 - 07/21/2009 3:36pm Page xviii 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 www.getcoolebook.com Nielsen ftoc.tex V4 - 07/21/2009 3:36pm Page xix Contents Responsible denormalization 516 Designing for extensibility 517 Creating the Database 517 The Create DDL command 517 Database-file concepts 519 Configuring file growth 520 Using multiple files 522 Planning multiple filegroups 525 Creating Tables 526 Designing tables using Management Studio 526 Working with SQL scripts 528 Schemas 529 Column names 530 Filegroups 531 Creating Keys 532 Primary keys 532 The surrogate debate: pros and cons 532 Database design layers 534 Creating foreign keys 537 Creating User-Data Columns 542 Column data types 542 Calculated columns 546 Sparse columns 546 Column constraints and defaults 547 Creating Indexes 550 Composite indexes 552 Primary keys 552 Filegroup location 552 Index options 553 Include columns 553 Filtered indexes 553 Summary 556 Chapter 21: Programming with T-SQL 559 Transact-SQL Fundamentals 560 T-SQL batches 560 T-SQL formatting 562 Variables 563 Variable default and scope 563 Using the set and select commands 564 Incrementing variables 565 Conditional select 566 Using variables within SQL queries 566 Multiple assignment variables 567 Procedural Flow 568 If 569 While 570 Goto 571 Examining SQL Server with Code 572 Dynamic Management Views 572 sp_help 572 System functions 573 Temporary Tables and Table Variables 574 Local temporary tables 574 Global temporary tables 575 Table variables 576 Summary 577 Chapter 22: Kill the Cursor! 579 Anatomy of a Cursor 580 The five steps to cursoring 580 Managing the cursor 581 Watching the cursor 583 Cursor options 583 Update cursor 584 xix www.getcoolebook.com . Soundex function 22 1 Data-Type Conversion Functions 22 2 Server Environment Information 22 4 Summary 22 5 Chapter 10: Merging Data with Joins and Unions 22 7 Using Joins 22 8 Inner Joins 23 0 Building. 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 . with SQL Server 1 42 SQL Server Management Objects 1 42 ADO.NET 147 Scripting SQL Server Tasks 150 Administrative tasks 150 Data-based tasks 159 SQL Server PowerShell Extensions 161 SQLPS.exe