this print for content only—size & color not accurate spine = 0.939" 496 page count Books for professionals By professionals ® Beginning SQL Server 2008 target='_blank' alt='beginning c 2008 from novice to professional download' title='beginning c 2008 from novice to professional download'>count Books for professionals By professionals ® Beginning SQL Server 2008 rget='_blank' alt='beginning c sharp 2008 from novice to professional' title='beginning c sharp 2008 from novice to professional'>count Books for professionals By professionals ® Beginning SQL Server 2008 for Developers: From Novice to Professional Dear Reader, 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 everything from enterprise data centers to handheld PDAs and mobile phones that run Windows Embedded CE. Knowing SQL Server is key to developing use- ful applications in a Microsoft environment. The aim of this book is for you to be confident when installing, setting up, and working with SQL Server 2008. This book assumes little to no knowledge about databases in general or about SQL Server 2008 in particular. By the time you have worked through the examples, you will be able to create and manage your own database solutions. You will be well on your way to becoming a highly proficient SQL Server developer. As you read this book, you’ll find terminology and features explained every step of the way through instruction and example. I cover the basics of T-SQL— SQL Server’s data-manipulation language—before introducing more advanced techniques. Whenever possible, I show you both the GUI- and text-based 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 development. Creating your own example database will increase your skill and improve your understanding of SQL Server. By the end of the book, you will have made a significant step toward becoming a competent SQL Server developer and database administrator. You’ll have the 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 good for your career as it has been for my own. Sincerely, Robin Dewson http://www.fat-belly.com Author of Beginning SQL Server 2005 for Developers: From Novice to Professional Beginning SQL Server 2005 Express for Developers: From Novice to Professional Pro SQL Server 2005 Assemblies Pro SQL Server 2005 Beginning C# Databases: From Novice to Professional Beginning SQL Server 2000 Programming SQL Server 2000 Stored Procedures Handbook US $49.99 Shelve in Databases/SQL Server User level: Beginner SQL Server 2008 The eXperT’s Voice ® in sQl serVer Beginning SQL Server 2008 for Developers From Novice to Professional cyan MaGenTa yelloW Black panTone 123 c Robin Dewson Companion eBook Available THE APRESS ROADMAP Beginning SQL Server 2008 for Developers Beginning Database Design Beginning SQL Queries Pro T-SQL 2008 Programmer’s Guide www.apress.com SOURCE CODE ONLINE Companion eBook See last page for details on $10 eBook version ISBN-13: 978-1-59059-958-7 ISBN-10: 1-59059-958-6 9 781590 599587 5 4 9 9 9 The quick and efficient path to proficiency in SQL Server 2008 Dewson Beginning for Developers Beginning SQL Server 2008 for Developers From Novice to Professional ■■■ Robin Dewson Dewson_958-7FRONT.fm Page i 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 orders-ny@springer-sbm.com , 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 info@apress.com , 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 ii 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 iii Thursday, July 3, 2008 3:20 PM Dewson_958-7FRONT.fm Page iv Thursday, July 3, 2008 3:20 PM v 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 Dewson_958-7FRONT.fm Page v Thursday, July 3, 2008 3:20 PM Dewson_958-7FRONT.fm Page vi Thursday, July 3, 2008 3:20 PM vii 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 Dewson_958-7FRONT.fm Page vii 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 Dewson_958-7FRONT.fm Page viii Thursday, July 3, 2008 3:20 PM [...]... http:// www.fat-belly.com Dewson_95 8-7 C01.fm Page 1 Monday, June 30, 2008 5:01 PM CHAPTER 1 ■■■ SQL Server 2008 Overview and Installation W elcome to Beginning SQL Server 2008 for Developers This book has been written for those who are interested in learning how to create solutions with Microsoft SQL Server 2008, but have no prior knowledge of SQL Server 2008 You may well have had exposure to other databases,... helps you decide which version of SQL Server 2008 to buy, shows you how to install and configure SQL Server 2008, and explains how to use the graphical user interface (GUI) tool, SQL Server Management Studio You will use this tool to work through a fully functional database example that is built from a design covered within the book, using graphical and code-based try-it-out exercises You’ll then learn... SQL Server 2005 experience and want a quick method to get up to speed on SQL Server 2008 You may find this book useful for understanding the basics of other databases in the marketplace, especially when working with T -SQL Many databases use an ANSI-standard SQL, so moving from SQL Server to Oracle, Sybase, etc., after reading this book will be a great deal easier This chapter covers the following topics:... $2.55 billion in 2001) to SQL Server (with 40% of the market) In 2002, the gap had grown, with SQL Server at 45% and Oracle slipping to 27%.a 3 Dewson_95 8-7 C01.fm Page 4 Monday, June 30, 2008 5:01 PM 4 CHAPTER 1 ■ SQL S ERVER 2008 OVERV IEW A ND INSTALLA TION Table 1-1 The Stages in the Evolution of SQL Server (Continued) Year Version Description 2005 SQL Server 2005 Many areas of SQL Server have been rewritten,... find that SQL Server 2008 will run on Windows Vista Home Basic Edition and above, as well as Windows XP From the server side, it will work on Windows Server 2003 with Service Pack 2 and Windows Server 2008 It will also work on the 64-bit operating systems for Windows XP Professional, as well as the 64-bit editions of Windows Server 2003 and 2008 So there is plenty of scope for running SQL Server on... this within SQL Server With SQL Server 2008, there is also the ability to query the database using NET and LINQ rather than T -SQL ■Note Including NET code is an advanced topic and outside the scope of this book For more information, try Pro SQL Server 2005 Assemblies by Robin Dewson and Julian Skinner (Apress, 2005) Dewson_95 8-7 C01.fm Page 7 Monday, June 30, 2008 5:01 PM C HAPT ER 1 ■ SQL SE RVER... rewritten, such as the ability to load data via a utility called Integration Services, but the greatest leap forward was the introduction of the NET Framework This allowed NET SQL Server specific objects to be built, giving SQL Server the flexible functionality that Oracle had with its inclusion of Java 2008 SQL Server 2008 The aim of SQL Server 2008 is to deal with the many different forms that data can now... engines, although SQL Server could work just as effectively With the new functionality in SQL Server 2008, Oracle will be under pressure to expand its existing functionality to meet this challenge SQL Server has always been a one-purchase solution, such that (providing you buy the correct version) tools that allow you to analyze your data or copy data from one data source such as Excel into SQL Server will... as MySQL, Oracle, or Microsoft Access, but SQL Server uses different interfaces and has a different way of working compared to much of the competition The aim of this book is to bring you quickly up to a level at which you are developing competently with SQL Server 2008 This book is specifically dedicated to beginners and to those who at this stage wish to use only SQL Server 2008 It is also for those... or even come from a server- based background such as Oracle, this book will provide you with the insight to get up and running with SQL Server 2008 Right from the start, this book will expand your basic knowledge, and you will soon find yourself moving from a beginner toward a competent and professional developer This book aims to cater to a wide range of developers, from those who prefer to use a graphical . publisher. ISBN-13 (pbk): 97 8-1 -5 905 9-9 5 8-7 ISBN-10 (pbk): 1-5 905 9-9 5 8-6 ISBN-13 (electronic): 97 8-1 -4 30 2-0 58 4-5 ISBN-10 (electronic): 1-4 30 2-0 58 4-9 Printed. in SQL Server 2008 Dewson Beginning for Developers Beginning SQL Server 2008 for Developers From Novice to Professional ■■■ Robin Dewson Dewson_95 8-7 FRONT.fm