this print for content only—size & color not accurate spine = 1.2793" 680 page count Books for professionals By professionals ® Pro SQL Server 2008 Relational Database Design and Implementation Dear Reader, I have written this book for you, the database professional, to give you the knowledge and practical steps needed to design and implement database solutions using SQL Server. I have filled this book with hard-won knowledge and experience that I’ve accumulated through many years of working with relational databases—including nearly every version of SQL Server. My first goal in writing this book is to explain many of the important concepts you need to learn from a database design textbook. Secondly, I will show practi- cal techniques for applying these concepts to build solid SQL Server database solutions. I start with the concepts of relational theory, data modeling, and normaliza- tion. Then I show you how to produce a database that works right the first time, using common design patterns and all of the objects types that SQL Server gives you. From there, I give you practical advice on implementing your systems with concurrency and performance in mind. Throughout this book, you will find that I emphasize the practical while always giving proper respect to the underlying theory. Great practices always rest on a solid theoretical foundation. After reading this book, you will have the tools you need to get going with your database implementation. And if we have a bit of fun along the way, that’ll be our secret. Thank you, Louis Davidson Author of Pro SQL Server 2005 Database Design and Optimization Pro SQL Server 2000 Database Design US $59.99 Shelve in Databases/SQL Server User level: Intermediate–Advanced Davidson Pro SQL Server 2008 Relational Database Design and Implementation The eXperT’s Voice ® in sQl serVer Pro SQL Server 2008 Relational Database Design and Implementation cyan MaGenTa yelloW Black panTone 123 c Louis Davidson With Kevin Kline, Scott Klein, and Kurt Windisch Foreword by Paul Nielsen, SQL Server MVP Companion eBook Available THE APRESS ROADMAP Accelerated SQL Server 2008 Beginning SQL Server 2008 for Developers Beginning SQL Queries Beginning Database Design Pro SQL Server 2008 Relational Database Design and Implementation www.apress.com SOURCE CODE ONLINE Companion eBook See last page for details on $10 eBook version Design for performance! Create optimized SQL Server databases that scale well and are logically sound in order to meet your business requirements. ISBN 978-1-4302-0866-2 9 781430 208662 5 5 9 9 9 Louis Davidson With Kevin Kline, Scott Klein, and Kurt Windisch Pro SQL Server 2008 Relational Database Design and Implementation 8662FM.qxp 7/28/08 6:19 PM Page i Pro SQL Server 2008 Relational Database Design and Implementation Copyright © 2008 by Louis Davidson 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-4302-0866-2 ISBN-13 (electronic): 978-1-4302-0867-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. Java™ and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc., in the US and other countries. Apress, Inc., is not affiliated with Sun Microsystems, Inc., and this book was writ- ten without endorsement from Sun Microsystems, Inc. Lead Editor: Jon Gennick Technical Reviewers: Wayne Snyder, Evan Terry, Don Watters 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: Tracy Brown Collins Copy Editors: Heather Lang, Kim Wimpsett Associate Production Director: Kari Brooks-Copony Production Editor: Ellie Fountain Compositor: Lynn L’Heureux Proofreader: Patrick Vincent 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 infor mation in this book is distr ibuted on an “ as is ” basis , without warr anty. Although every precau- tion 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. 8662FM.qxp 7/28/08 6:19 PM Page ii To my father-in-law, Verlin Wheeler. He passed away during the writing of this book, and I really never got to know him as I was told he was in his younger days. At the very least, he rivaled my love for gadgets and was an early adopter of lots of technology, like satellite dishes and even computers. I use his computer every day to manage my media collection— he probably would have liked that, though I think he would have preferred to have used the media in the library. 8662FM.qxp 7/28/08 6:19 PM Page iii 8662FM.qxp 7/28/08 6:19 PM Page iv Contents at a Glance Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii About the Technical Reviewers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii ■ CHAPTER 1 Introduction to Database Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 ■ CHAPTER 2 The Language of Data Modeling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 ■ CHAPTER 3 Conceptual Data Modeling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 ■ CHAPTER 4 The Normalization Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 ■ CHAPTER 5 Implementing the Base Table Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 ■ CHAPTER 6 Protecting the Integrity of Your Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273 ■ CHAPTER 7 Patterns and Query Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339 ■ CHAPTER 8 Securing Access to the Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385 ■ CHAPTER 9 Table Structures and Indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447 ■ CHAPTER 10 Coding for Concurrency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499 ■ CHAPTER 11 Considering Data Access Strategies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 555 ■ APPENDIX A Codd’ s 12 Rules for an RDBMS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 595 ■ APPENDIX B Scalar Datatype Refer ence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 603 ■ INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 635 v 8662FM.qxp 7/28/08 6:19 PM Page v 8662FM.qxp 7/28/08 6:19 PM Page vi Contents Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii About the Technical Reviewers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii ■ CHAPTER 1 Introduction to Database Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Database Design Phases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Conceptual . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Logical . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Physical . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Relational Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Database and Schema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Tables, Rows, and Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 The Information Principle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Domains . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Missing Values (NULLs) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Relationship Between Entities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Data Access Language (SQL) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Understanding Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Functional Dependency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Determinant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 ■ CHAPTER 2 The Language of Data Modeling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Introduction to Data Modeling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Entities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Primar y Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Alternate Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 vii 8662FM.qxp 7/28/08 6:19 PM Page vii Foreign Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Domains . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Naming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Identifying Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Nonidentifying Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Role Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Relationship Cardinality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Verb Phrases (Relationship Names) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Descriptive Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Alternative Modeling Methodologies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 Information Engineering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 Chen ERD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Visio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 Management Studio Database Diagrams . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Best Practices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 ■ CHAPTER 3 Conceptual Data Modeling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Understanding the Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Documenting the Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Requirements Ga thering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Client Interviews . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Questions to Be Answered . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 Existing Systems and Prototypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 Other Types of Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 Identifying Objects and Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Identifying Entities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 Relationships Between Entities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Identifying Attributes and Domains . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 Identifying Business Rules and Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 Identifying Business Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 Identifying Fundamental Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 Finishing the Conceptual Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 Identifying Obvious Additional Data Needs . . . . . . . . . . . . . . . . . . . . . . . . . 113 Review with the Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 Repeat Until the Customer Agrees with Your Model . . . . . . . . . . . . . . . . . 114 Best Practices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 ■ CONTENTSviii 8662FM.qxp 7/28/08 6:19 PM Page viii [...]... 403 Controlling Object Access Via T -SQL Coded Objects 405 Stored Procedures and Scalar Functions 406 Impersonation Within Objects 408 Crossing Database Lines 413 Different Server (Distributed Queries) 418 Views and Table-Valued Functions ... 362 Entity-Attribute-Value (EAV) 363 Adding Columns to a Table 367 Commonly Implemented Objects 373 8662FM.qxp 7/28/08 6:19 PM Page xi sCONTENTS Anti-patterns 374 One-Size-Fits-All Key Domain ... 419 General Usage 419 Implementing Configurable Row-Level Security with Views 422 Obfuscating Data 426 Monitoring and Auditing 429 Server and Database Audit 430 Watching Table History Using DML Triggers ... Optimization Basics 501 OS and Hardware Issues 504 Transactions 505 Transaction Syntax 506 Compiled SQL Server Code 514 SQL Server Concurrency Controls ... 576 Pitfalls 578 Opinions 582 T -SQL and the CLR 584 Guidelines for Choosing T -SQL 588 Guidelines for Choosing NET 589 CLR Object Types ... 438 Logging with Profiler 441 Best Practices 443 Summary 444 s CHAPTER 9 Table Structures and Indexing 447 Physical Database Structure 448 Files and Filegroups ... Caused by Constraints 288 DML Triggers 290 Handling Errors from Triggers and Constraints 327 Manual Data Protection 330 Stored Procedures 332 Client Code 334... 177 Reviewing the Logical Design 181 Transforming the Design 181 Choosing Names 182 Dealing with Subtypes 186 Determining Tree Implementation 190 Choosing Key Implementation ... 270 Summary 271 s CHAPTER 6 Protecting the Integrity of Your Data 273 Best Practices 274 Automatic Data Protection 276 Declarative Data Protection 277 Basic Syntax ... 120 The Process of Normalization 120 Entity and Attribute Shape: First Normal Form 121 All Attributes Must Be Atomic 121 All Instances in an Entity Must Contain the Same Number of Values 126 All Occurrences of an Entity Type in an Entity Must Be Different 127 Programming Anomalies . SQL Server 2008 Relational Database Design and Implementation The eXperT’s Voice ® in sQl serVer Pro SQL Server 2008 Relational Database Design and Implementation. Server 2008 for Developers Beginning SQL Queries Beginning Database Design Pro SQL Server 2008 Relational Database Design and Implementation www .apress. com