cyan yelloW MaGenTa Black panTone 123 c Books for professionals By professionals® The eXperT’s Voice® in sQl serVer Companion eBook Available Beginning SQL Server 2008 for Developers: From Novice to Professional Beginning Dear Reader, S Beginning Microsoft SQL Server 2008 is one of today’s market-leading, relational database management systems. Learn SQL Server 2008 well, and you can manage data in Q everything from enterprise data centers to handheld PDAs and mobile phones that run Windows Embedded CE. Knowing SQL Server is key to developing use- L Author of ful applications in a Microsoft environment. SQL Server 2008 Beginning SQL Server 2005 The aim of this book is for you to be confident when installing, setting up, for Developers: From Novice and working with SQL Server 2008. This book assumes little to no knowledge S to Professional about databases in general or about SQL Server 2008 in particular. By the time Beginning SQL Server 2005 you have worked through the examples, you will be able to create and manage e Express for Developers: your own database solutions. You will be well on your way to becoming a highly From Novice to Professional proficient SQL Server developer. Developers r Pro SQL Server 2005 As you read this book, you’ll find terminology and features explained every for Assemblies step of the way through instruction and example. I cover the basics of T-SQL— v Pro SQL Server 2005 SQL Server’s data-manipulation language—before introducing more advanced techniques. Whenever possible, I show you both the GUI- and text-based e Beginning C# Databases: From Novice to Professional approaches to completing a task. I show you how to build a database example from beginning to end, and I demonstrate and explain every stage of database r Beginning SQL Server 2000 development. Creating your own example database will increase your skill and Programming improve your understanding of SQL Server. From Novice to Professional SQL Server 2000 Stored 2 By the end of the book, you will have made a significant step toward becoming Procedures Handbook a competent SQL Server developer and database administrator. You’ll have the 0 skills and solid base of understanding needed to productively deliver database solutions in SQL Server 2008. It’s my hope that knowing SQL Server will be as 0 good for your career as it has been for my own. Sincerely, 8 The quick and efficient path to Robin Dewson proficiency in SQL Server 2008 Companion eBook http://www.fat-belly.com Df o er THE APRESS ROADMAP v e Soene l$a1s0t peaBgoeo kfo vre drseitoanils SQLB Seegrinvnerin 2g008 Beginning Beginning Pro T-SQL 2008 lo Database Design SQL Queries Programmer’s Guide for Developers p e r Robin Dewson SOURCE CODE ONLINE ISBN-13: 978-1-59059-958-7 s www.apress.com ISBN-10: 1-59059-958-6 54999 Dewson US $49.99 Shelve in Databases/SQL Server User level: 9 781590 599587 Beginner this print for content only—size & color not accurate spine = 0.939" 496 page count Dewson_958-7FRONT.fm Page i Thursday, July 3, 2008 3:20 PM Beginning SQL Server 2008 for Developers From Novice to Professional ■ ■ ■ Robin Dewson Dewson_958-7FRONT.fm Page ii Thursday, July 3, 2008 3:20 PM Beginning SQL Server 2008 for Developers: From Novice to Professional Copyright © 2008 by Robin Dewson All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-59059-958-7 ISBN-10 (pbk): 1-59059-958-6 ISBN-13 (electronic): 978-1-4302-0584-5 ISBN-10 (electronic): 1-4302-0584-9 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Lead Editor: Jonathan Gennick Technical Reviewer: Jasper Smith Editorial Board: Clay Andres, Steve Anglin, Ewan Buckingham, Tony Campbell, Gary Cornell, Jonathan Gennick, Matthew Moodie, Joseph Ottinger, Jeffrey Pepper, Frank Pohlmann, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Project Manager: Kylie Johnston Copy Editor: Nicole Abramowitz Associate Production Director: Kari Brooks-Copony Production Editor: Ellie Fountain Compositor: Susan Glinert Proofreader: Nancy Sixsmith, ConText Editorial Services, Inc. Indexer: Broccoli Information Management Artist: Kinetic Publishing Services, LLC Cover Designer: Kurt Krames Manufacturing Director: Tom Debolski Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail [email protected], or visit http://www.springeronline.com. For information on translations, please contact Apress directly at 2855 Telegraph Avenue, Suite 600, Berkeley, CA 94705. Phone 510-549-5930, fax 510-549-5939, e-mail [email protected], or visit http:// www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at http://www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at http://www.apress.com. Dewson_958-7FRONT.fm Page iii Thursday, July 3, 2008 3:20 PM This book, as many of my books are, is dedicated to my family. First, to my mum and dad whom I love very much and who made me what I am today. Without their help, under- standing, and patience when it came to my use of the television for the Sinclair ZX80 and the Sinclair ZX81, and without helping me find and organize my further education, I probably would have wasted a great opportunity. To my three kids—Ellen, Cameron, and Scott—who have been brilliant and wonderful and whom I am very, very proud of in many ways. And they are such great kids because they have who can only be termed the best mother kids can have, right there helping, loving, and, yes, screaming at them when needed. Julie, I love you more than I love my Irn-Bru, rugby, and lemon meringue pie . . . and you know how much those mean to me! Dewson_958-7FRONT.fm Page iv Thursday, July 3, 2008 3:20 PM Dewson_958-7FRONT.fm Page v Thursday, July 3, 2008 3:20 PM Contents at a Glance About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xvii About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii ■ CHAPTER 1 SQL Server 2008 Overview and Installation . . . . . . . . . . . . . . . . . . . . . 1 ■ CHAPTER 2 SQL Server Management Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 ■ CHAPTER 3 Database Design and Creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 ■ CHAPTER 4 Security and Compliance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 ■ CHAPTER 5 Defining Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 ■ CHAPTER 6 Creating Indexes and Database Diagramming . . . . . . . . . . . . . . . . 151 ■ CHAPTER 7 Database Backups, Recovery, and Maintenance . . . . . . . . . . . . . . 181 ■ CHAPTER 8 Working with the Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 ■ CHAPTER 9 Building a View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307 ■ CHAPTER 10 Stored Procedures and Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329 ■ CHAPTER 11 T-SQL Essentials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355 ■ CHAPTER 12 Advanced T-SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395 ■ CHAPTER 13 Triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417 ■ CHAPTER 14 SQL Server 2008 Reporting Services . . . . . . . . . . . . . . . . . . . . . . . . . 439 ■ INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .459 v Dewson_958-7FRONT.fm Page vi Thursday, July 3, 2008 3:20 PM Dewson_958-7FRONT.fm Page vii Thursday, July 3, 2008 3:20 PM Contents About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xvii About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii ■ CHAPTER 1 SQL Server 2008 Overview and Installation . . . . . . . . . . . . . . . 1 Why SQL Server 2008? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Evolution of SQL Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Hardware Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 CPU. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Hard Disk Space. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Operating System Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 The Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Beginning the Install. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Choosing the Features to Install . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Naming the Instance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Choosing Service Accounts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Selecting an Authentication Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Defining the Data Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Creating the Reporting Services Database . . . . . . . . . . . . . . . . . . . . 14 Configuring Error and Usage Reports. . . . . . . . . . . . . . . . . . . . . . . . . 16 Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Services Accounts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Looking at the Authentication Mode. . . . . . . . . . . . . . . . . . . . . . . . . . 18 The sa Login . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 vii Dewson_958-7FRONT.fm Page viii Thursday, July 3, 2008 3:20 PM viii ■ CONTENTS ■ CHAPTER 2 SQL Server Management Studio . . . . . . . . . . . . . . . . . . . . . . . . . . 25 A Quick Overview of SSMS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Examining SSMS’s Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Environment Node . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Source Control Node. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Text Editor Node. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Query Execution Node . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Query Results Node . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Query Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 ■ CHAPTER 3 Database Design and Creation . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Defining a Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Prebuilt Databases Within SQL Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 master . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 tempdb. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 model. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 msdb . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 AdventureWorks/AdventureWorksDW . . . . . . . . . . . . . . . . . . . . . . . . 55 Choosing the Database System Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 OLTP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 OLAP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Example System Choice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Gathering the Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Determining the Information to Store in the Database . . . . . . . . . . . . . . . 59 Financial Products . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Customers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Customer Addresses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Shares . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 External and Ignored Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Building Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 Using Keys. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 Creating Relationships. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 More on Foreign Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Description: