Nielsen ftoc.tex V4 - 07/21/2009 3:36pm Page xxx Contents Level 2 — Read Committed 1392 Level 3 — Repeatable Read 1392 Level 4 — Serializable 1395 Snapshot isolations 1399 Using locking hints 1402 Application Locks 1403 Application Locking Design 1405 Implementing optimistic locking 1405 Lost updates 1405 Transaction-Log Architecture 1408 Transaction log sequence 1408 Transaction log recovery 1412 Transaction Performance Strategies 1413 Evaluating database concurrency performance 1413 Summary 1414 Chapter 67: Data Compression 1415 Understanding Data Compression 1415 Data compression pros and cons 1416 Row compression 1417 Page compression 1417 Compression sequence 1420 Applying Data Compression 1421 Determining the current compression setting 1421 Estimating data compression 1422 Enabling data compression 1422 Data compression strategies 1424 Summary 1425 Chapter 68: Partitioning 1427 Partitioning Strategies 1428 Partitioned Views 1429 Local-partition views 1430 Distributed-partition views 1438 Partitioned Tables and Indexes 1440 Creating the partition function 1441 Creating partition schemes 1443 Creating the partition table 1444 Querying partition tables 1445 Altering partition tables 1446 Switching tables 1447 Rolling partitions 1449 Indexing partitioned tables 1449 Removing partitioning 1450 Data-Driven Partitioning 1450 Summary 1451 Chapter 69: Resource Governor 1453 Configuring Resource Governor 1454 Resource pools 1454 Workload groups 1456 Classifier functions 1457 Monitoring Resource Governor 1457 Summary 1458 Part X Business Intelligence Chapter 70: BI Design 1461 Data Warehousing 1462 Star schema 1462 Snowflake schema 1463 Surrogate keys 1464 Consistency 1464 Loading data 1465 Changing data in dimensions 1467 Summary 1468 xxx www.getcoolebook.com Nielsen ftoc.tex V4 - 07/21/2009 3:36pm Page xxxi Contents Chapter 71: Building Multidimensional Cubes with Analysis Services 1469 Analysis Services Quick Start 1469 Analysis Services Architecture 1470 Unified Dimensional Model 1471 Server 1471 Client 1472 Building a Database 1472 Business Intelligence Development Studio 1473 Data sources 1473 Data source view 1474 Creating a cube 1478 Dimensions 1479 Dimension Designer 1479 Beyond regular dimensions 1487 Dimension refinements 1490 Cubes 1491 Cube structure 1492 Dimension usage 1494 Calculations 1496 KPIs 1497 Actions 1497 Partitions 1497 Aggregation design 1499 Perspectives 1501 Data Storage 1501 Proactive caching 1502 SQL Server notifications 1503 Client-initiated notifications 1503 Scheduled polling notifications 1503 Data Integrity 1504 Null processing 1504 Unknown member 1505 Error Configuration 1505 Summary 1507 Chapter 72: Programming MDX Queries 1509 Basic Select Query 1510 Cube addressing 1510 Dimension structure 1511 Basic SELECT statement 1512 Advanced Select Query 1517 Subcubes 1518 WITH clause 1518 Dimension considerations 1522 MDX Scripting 1523 Calculated members and named sets 1523 Adding Business Intelligence 1525 Summary 1526 Chapter 73: Authoring Reports with Reporting Services 1527 Anatomy of a Report 1527 Report Definition Language (RDL) 1528 Data Sources 1528 Reporting Services datasets 1529 Query parameters and report parameters 1530 Report content and layout 1531 The Report Authoring Process 1533 Creating a Reporting Services project 1533 Creating a report 1533 Using the Report Wizard to create reports 1534 Authoring a report from scratch 1534 Working with Data 1537 Working with SQL in the Query Designer 1537 Using query parameters to select and filter data 1538 Adding calculated fields to a dataset 1542 Working with XML data sources 1542 Working with expressions 1544 xxxi www.getcoolebook.com Nielsen ftoc.tex V4 - 07/21/2009 3:36pm Page xxxii Contents Designing the Report Layout 1547 Design basics 1547 Using the Tablix property pages 1551 Grouping and sorting data in a Tablix 1551 Illustrating data with charts and gauges 1556 Summary 1558 Chapter 74: Administering Reporting Services 1559 Deploying Reporting Services Reports 1561 Deploying reports using BIDS 1561 Deploying reports using the Report Manager 1563 Deploying reports programmatically using the Reporting Services Web Service 1563 Configuring Reporting Services Using Management Studio 1564 Configuring Reporting Services server properties 1564 Security: managing roles 1566 Configuring Reporting Services Using Report Manager 1567 Managing security 1567 Working with linked reports 1569 Creating linked reports 1570 Leveraging the power of subscriptions 1570 Creating a data-driven subscription 1571 Summary 1576 Chapter 75: Analyzing Data with Excel 1577 Data Connections 1578 Data Connection Wizard 1580 Microsoft Query 1580 Connection file types 1581 Basic Data Analysis 1581 Data tables 1582 PivotTables 1582 PivotCharts 1584 Advanced Data Analysis 1585 Installing the data mining add-ins 1586 Exploring and preparing data 1586 Table analysis tools 1588 Data mining client 1591 Summary 1591 Chapter 76: Data Mining with Analysis Services 1593 The Data Mining Process 1594 Modeling with Analysis Services 1595 Data Mining Wizard 1595 Mining Models view 1597 Model evaluation 1598 Algorithms 1604 Decision tree 1604 Linear regression 1606 Clustering 1606 Sequence clustering 1607 Neural Network 1608 Logistic regression 1609 Naive Bayes 1609 Association rules 1609 Time series 1610 Cube Integration 1611 Summary 1612 Appendix A: SQL Server 2008 Specifications 1613 Appendix B: Using the Sample Databases 1619 Index 1625 xxxii www.getcoolebook.com Nielsen flast.tex V4 - 07/23/2009 6:17pm Page xxxiii C an one book really cover everything you need to know about SQL Server 2008? As more and more books are covering fewer and fewer features of this huge product, before taking a close look at Paul’s SQL Server 2008 Bible, I would have said no. And of course, the answer depends on how much you actually need to know about my favorite database system. For some, ‘‘information needed’’ could cover a lot of ground, but Paul’s book comes closer to covering everything than any book I have ever seen. Paul Nielsen brings his passion for SQL Server and his many years of experience with this product into every page of the SQL Server 2008 Bible. Every detail and every example is tested out by Paul personally, and I know for a fact that he had fun doing all this amazing writing and testing. Of course, no book can go into great depth on every single area, but Paul takes you deeply enough into each topic that you, the reader, can decide whether that feature will be valuable to you. How can you know whether PowerShell or Spatial Data is something you want to dive deeply into unless you know something about its value? How can you know if you should look more deeply into Analysis Services or partitioning if you don’t even know what those features are? How do you know which Transact-SQL language features will help you solve your data access problems if you don’t know what features are available, and what features are new in SQL Server 2008? How can you know which high-availability technology or monitoring tool will work best in your environment if you don’t know how they differ? You can decide whether you want to use what Paul has presented as either a great breadth of SQL Server knowledge or a starting point for acquiring greater depth in areas of your own choosing. As someone who writes about a very advanced, but limited, area within SQL Server, I am frequently asked by my readers what they can read to prepare them for reading my books. Now I have an answer not just for my readers, but for myself as well. Just as no one book can cover every aspect of SQL Server in great depth, no one person can know everything about this product. When I want to know how to get started with LINQ, Service Broker, or MDX, or any of dozens of other topics that my books don’t cover, Paul’s book is the place I’ll start my education. Kalen Delaney, SQL Server MVP and author of SQL Server 2008 Internals xxxiii www.getcoolebook.com Nielsen flast.tex V4 - 07/23/2009 6:17pm Page xxxiv www.getcoolebook.com Nielsen f06.tex V4 - 07/23/2009 6:18pm Page xxxv W elcome to the SQL Server 2008 Bible. SQL Server is an incredible database product. It offers an excellent mix of performance, reliability, ease of administration, and new architectural options, yet enables the developer or DBA to control minute details when desired. SQL Server is a dream system for a database developer. If there’s a theme to SQL Server 2008, it’s this: enterprise-level excellence. SQL Server 2008 opens sev- eral new possibilities for designing more scalable and powerful systems. The first goal of this book is to share with you the pleasure of working with SQL Server. Like all books in the Bible series, you can expect to find both hands-on tutorials and real-world practical applications, as well as reference and background information that provides a context for what you are learning. However, to cover every minute detail of every command of this very complex product would consume thousands of pages, so it is the second goal of this book to provide a concise yet comprehen- sive guide to SQL Server 2008 based on the information I have found most useful in my experience as a database developer, consultant, and instructor. By the time you have completed the SQL Server 2008 Bible, you will be well prepared to develop and manage your SQL Server 2008 database. Some of you are repeat readers of mine (thanks!) and are familiar with my approach from the previous SQL Server Bibles. Even though you might be familiar with this approach and my tone, you will find several new features in this edition, including the following: ■ A ‘‘what’s new’’ sidebar in most chapters presents a timeline of the features so you can envision the progression. ■ Several chapters are completely rewritten, especially my favorite topics. ■ I’ve added much of the material from my Smart Database Design into this book. A wise database developer once showed a box to an apprentice and asked, ‘‘How many sides do you see?’’ The apprentice replied, ‘‘There are six sides to the box.’’ The experienced database developer then said, ‘‘Users may see six sides, but database developers see only two sides: the inside and the outside. To the database developer, the cool code goes inside the box.’’ This book is about thinking inside the box. Who Should Read This Book I believe there are five distinct roles in the SQL Server space: ■ Data architect/data modeler ■ Database developer xxxv www.getcoolebook.com Nielsen f06.tex V4 - 07/23/2009 6:18pm Page xxxvi Introduction ■ Database administrator ■ BI (Business Intelligence) developer ■ PTO performance tuning and optimization expert This book has been carefully planned to address each of these roles. Whether you are a database developer or a database administrator, whether you are just starting out or have one year of experience or five, this book contains material that will be useful to you. While the book is targeted at intermediate-level database professionals, each chapter begins with the assumption that you’ve never seen the topic before, and then progresses through the subject, presenting the information that makes a difference. At the higher end of the spectrum, the book pushes the intermediate professional into certain advanced areas where it makes the most sense. For example, there’s very advanced material on T-SQL queries, index strategies, and data architecture. How This Book Is Organized SQL Server is a huge product with dozens of technologies and interrelated features. Seventy-six chapters! Just organizing a book of this scope is a daunting task. A book of this size and scope must also be approachable as both a cover-to-cover read and a reference book. The ten parts of this book are organized by job role, project flow, and skills progression: Part I: Laying the Foundation Part II: Manipulating Data with Select Part III: Beyond Relational Part IV: Developing with SQL Server Part V: Data Connectivity Part VI: Enterprise Data Management Part VII: Security Part VIII: Monitoring and Auditing Part IX: Performance Tuning and Optimization Part X: Business Intelligence xxxvi www.getcoolebook.com Nielsen f06.tex V4 - 07/23/2009 6:18pm Page xxxvii Introduction SQL Server Books Online This book is not a rehash of Books Online, and it doesn’t pretend to replace Books Online. I avoid listing the complete syntax of every command — there’s little value in reprinting Books Online. Instead, I’ve designed this book to show you what you need to know in order to get the most out of SQL Server, so that you can learn from my experience and the experience of the co-authors. In here you’ll find each feature explained as if we are friends — you got a new job that requires a spe- cific feature you’re unfamiliar with, and you asked me to get you up to speed with what matters most. The 76 chapters contain critical concepts, real-world examples, and best practices. Conventions and Features This book contains several different organizational and typographical features designed to help you get the most from the information. Tips, Notes, Cautions, and Cross-References Whenever the authors want to bring something important to your attention, the information will appear in a Tip, Note, or Caution. This information is important and is set off in a separate paragraph with a special icon. Cautions provide information about things to watch out for, whether simply inconvenient or potentially hazardous to your data or systems. Tips generally are used to provide information that can make your work simpler — special shortcuts or methods for doing something easier than the norm. You will often find the rel- evant .sys files listed in a tip. Notes provide additional, ancillary information that is helpful, but somewhat outside of the current presentation of information. Cross-references provide a roadmap to related content, be it on the Web, another chapter in this book, or another book. What’s New and Best Practice Sidebars Two sidebar features are specific to this book: the What’s New sidebars and the Best Practice sidebars. What’s New with SQL Server Feature W henever possible and practical, a sidebar will be included that highlights the relevant new features covered in the chapter. Often, these sidebars a lso alert you to which features have been eliminated and which are deprecated. Usually, these sidebars are placed near the beginning of the chapter. xxxvii www.getcoolebook.com Nielsen f06.tex V4 - 07/23/2009 6:18pm Page xxxviii Introduction Best Practice T his book is based on the real-life experiences of SQL Server developers and administrators. To enable you to benefit from all that experience, the best practices have been pulled out in sidebar form wherever and whenever they apply. www.SQLServerBible.com This book has an active companion website where you’ll find the following: ■ Sample code: Most chapters have their own SQL script or two. All the chapter code samples are in a single zip file on the book’s page. ■ Sample databases: The sample database specific to this book, OBXKites, CHA2, and others are in the Sampledb.zip file also on the book’s page. ■ Watch free screencasts based on the examples and content of this book. ■ Links to new downloads, and the best of the SQL Server community online. ■ Get a free Euro-style SQL Sticker for your notebook. ■ Get the latest versions of Paul’s SQL Server queries and utilities. ■ Paul’s presentation schedule and a schedule of SQL Server community events. ■ Link to BrainBench.com’s SQL Server 2008 Programming Certification, the test that Paul designed. ■ Sign up for the SQL Server 2008 Bible eNewsletter to stay current with new links, new queries, articles, updates, and announcements. WheretoGofromHere There’s a whole world of SQL Server. Dig in. Explore. Play with SQL Server. Try out new ideas, and e- mail me if you have questions or discover something cool. I designed the BrainBench.com SQL Server 2008 Programming Certification, so read the book and then take the test. Do sign up for the SQL Server Bible eNewsletter to keep up with updates and news. Come to a conference or user group where I’m speaking.I’dlovetomeetyouinpersonandsignyour book. You can learn where and when I’ll be speaking at SQLServerBible.com. With a topic as large as SQL Server and a community this strong, a lot of resources are available. But there’s a lot of hubris around SQL Server too, for recommended additional resources and SQL Server books, check the book’s website. Most important of all, e-mail me: pauln@SQLServerBible.com. I’d love to hear what you’re doing with SQL Server. xxxviii www.getcoolebook.com Nielsen p01.tex V4 - 07/21/2009 11:59am Page 1 Laying the Foundation IN THIS PART Chapter 1 The World of SQL Server Chapter 2 Data Architecture Chapter 3 Relational Database Design Chapter 4 Installing SQL Server 2008 Chapter 5 Client Connectivity Chapter 6 Using Management Studio Chapter 7 Scripting with PowerShell S QL Server is a vast product. If you’re new to SQL Server it can be difficult to know where to start. You need at least an idea of the scope of the components, the theory behind databases, and how to use the UI to even begin playing with SQL Server. That’s where this part fits and why it’s called ‘‘Laying the Foundation.’’ Chapter 1 presents an introduction to SQL Server’s many components and how they work together. Even if you’re an experienced DBA, this chapter is a quick way to catch up on what’s new. Database design and technology have both evolved faster since the millennium than at any other time since Dr. Edgar Codd introduced his revolutionary RDBMS concepts three decades earlier. Every year, the IT profession is getting closer to the vision of ubiquitous information. This is truly a time of change. Chapters 2 and 3 discuss database architecture and relational database design. Installing and connecting to SQL Server is of course required before you can have any fun with joins, and two chapters cover those details. Management Studio, one of my favorite features of SQL Server, and PowerShell, the new scripting tool, each deserve a chapter and round out the first part. If SQL Server is the box, and developing is thinking inside the box, the first part of this book is an introduction to the box. www.getcoolebook.com . 143 8 Partitioned Tables and Indexes 144 0 Creating the partition function 144 1 Creating partition schemes 144 3 Creating the partition table 144 4 Querying partition tables 144 5 Altering partition. 144 6 Switching tables 144 7 Rolling partitions 144 9 Indexing partitioned tables 144 9 Removing partitioning 145 0 Data-Driven Partitioning 145 0 Summary 145 1 Chapter 69: Resource Governor 145 3 Configuring. compression 142 2 Data compression strategies 142 4 Summary 142 5 Chapter 68: Partitioning 142 7 Partitioning Strategies 142 8 Partitioned Views 142 9 Local-partition views 143 0 Distributed-partition