A Developer's Guide to Data Modeling for SQL Server
Total Page:16
File Type:pdf, Size:1020Kb
Praise for A Developer’s Guide to Data Modeling for SQL Server “Eric and Joshua do an excellent job explaining the importance of data modeling and how to do it correctly. Rather than relying only on academic concepts, they use real-world ex- amples to illustrate the important concepts that many database and application develop- ers tend to ignore. The writing style is conversational and accessible to both database design novices and seasoned pros alike. Readers who are responsible for designing, imple- menting, and managing databases will benefit greatly from Joshua’s and Eric’s expertise.” —Anil Desai, Consultant, Anil Desai, Inc. “Almost every IT project involves data storage of some kind, and for most that means a relational database management system (RDBMS). This book is written for a database- centric audience (database modelers, architects, designers, developers, etc.). The authors do a great job of showing us how to take a project from its initial stages of requirements gathering all the way through to implementation. Along the way we learn how to handle some of the real-world design issues that typically surface as we go through the process. “The bottom line here is simple. This is the book you want to have just finished read- ing when your boss says ‘We have a new project I would like your help with.’” —Ronald Landers, Technical Consultant, IT Professionals, Inc. “The Data Model is the foundation of the application. I’m pleased to see additional books being written to address this critical phase. This book presents a balanced and pragmatic view with the right priorities to get your SQL server project off to a great start and a long life.” —Paul Nielsen, SQL Server MVP, SQLServerBible.com “This is a truly excellent introduction to the database design methodology that will work for both novices and advanced designers. The authors do a good job at explaining the ba- sics of relational database modeling and how they fit into modern business architecture. This book teaches us how to identify the business problems that have to be satisfied by a database and then proceeds to explain how to build a solid solution from scratch.” —Alexzander N. Nepomnjashiy, Microsoft SQL Server DBA, NeoSystems North-West, Inc. “A Developer’s Guide to Data Modeling for SQL Server explains the concepts and prac- tice of data modeling with a clarity that makes the technology accessible to anyone build- ing databases and data-driven applications. “Eric Johnson and Joshua Jones combine a deep understanding of the science of data modeling with the art that comes with years of experience. If you’re new to data model- ing, or find the need to brush up on its concepts, this book is for you.” —Peter Varhol, Executive Editor, Redmond Magazine This page intentionally left blank A Developer’s Guide to Data Modeling for SQL Server COVERING SQL SERVER 2005 AND 2008 This page intentionally left blank A Developer’s Guide to Data Modeling for SQL Server COVERING SQL SERVER 2005 AND 2008 Eric Johnson Joshua Jones Upper Saddle River, NJ • Boston • Indianapolis • San Francisco New York • Toronto • Montreal • London • Munich • Paris • Madrid Capetown • Sydney • Tokyo • Singapore • Mexico City Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals. The authors and publisher have taken care in the preparation of this book, but make no expressed or implied war- ranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein. The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales, which may include electronic versions and/or custom covers and content particular to your business, training goals, marketing focus, and branding interests. For more information, please contact: U.S. Corporate and Government Sales (800)382-3419 [email protected] For sales outside the United States please contact: International Sales [email protected] Visit us on the Web: informit.com/aw Library of Congress Cataloging-in-Publication Data Johnson, Eric, 1978– A developer’s guide to data modeling for SQL server : covering SQL server 2005 and 2008 / Eric Johnson and Joshua Jones. — 1st ed. p. cm. Includes index. ISBN 978-0-321-49764-2 (pbk. : alk. paper) 1. SQL server. 2. Database design. 3. Data structures (Computer science) I. Jones, Joshua, 1975- II. Title. QA76.9.D26J65 2008 005.75'85—dc22 2008016668 Copyright © 2008 Pearson Education, Inc. All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permis- sion must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or trans- mission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permissions, write to: Pearson Education, Inc. Rights and Contracts Department 501 Boylston Street, Suite 900 Boston, MA 02116 Fax (617) 671-3447 ISBN-13: 978-0-321-49764-2 ISBN-10: 0-321-49764-3 Text printed in the United States on recycled paper at Courier in Stoughton, Massachusetts. First printing, June 2008 For Michelle and Evan—Eric To my wife and children; I have time to play now—Josh This page intentionally left blank CONTENTS Preface xv Acknowledgments xvii About the Authors xix PART I Data Modeling Theory . 1 Chapter 1 Data Modeling Overview . 3 Databases . 4 Relational Database Management Systems. 5 Why a Sound Data Model Is Important . 6 Data Consistency . 6 Scalability . 8 Meeting Business Requirements. 10 Easy Data Retrieval . 10 Performance Tuning . 13 The Process of Data Modeling. 14 Modeling Theory. 15 Business Requirements . 16 Building the Logical Model. 18 Building the Physical Model . 19 Summary . 21 Chapter 2 Elements Used in Logical Data Models . 23 Entities . 23 Attributes . 24 Data Types . 25 Primary and Foreign Keys . 30 Domains. 31 Single-Valued and Multivalued Attributes . 32 Referential Integrity . 32 ix x Contents Relationships. 35 Relationship Types . 35 Relationship Options . 40 Cardinality . 41 Using Subtypes and Supertypes . 42 Supertypes and Subtypes Defined . 42 When to Use Subtype Clusters . 44 Summary . 44 Chapter 3 Physical Elements of Data Models . 45 Physical Storage . 45 Tables . 45 Views. 47 Data Types . 49 Referential Integrity . 59 Primary Keys . 59 Foreign Keys . 63 Constraints . 66 Implementing Referential Integrity . 68 Programming . 71 Stored Procedures . 71 User-Defined Functions. 72 Triggers . 73 CLR Integration . 75 Implementing Supertypes and Subtypes . 75 Supertype Table . 76 Subtype Tables . 77 Supertype and Subtype Tables . 78 Supertypes and Subtypes: A Final Word . 79 Summary . 79 Chapter 4 Normalizing a Data Model. 81 What Is Normalization? . 81 Normal Forms. 81 Determining Normal Forms . 90 Denormalization . 91 Summary . 94 Contents xi PART II Business Requirements . 95 Chapter 5 Requirements Gathering . 97 Requirements Gathering Overview . 98 Gathering Requirements Step by Step . 98 Conducting Interviews . 98 Observation . 101 Previous Processes and Systems . 103 Use Cases . 105 Business Needs . 111 Balancing Technical Limitations with Business Needs . 112 Gathering Usage Data . ..