Tải bản đầy đủ (.pdf) (672 trang)

pro sql server 2005 database design and optimization

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (4.78 MB, 672 trang )

Louis Davidson
with Kevin Kline and Kurt Windisch
Pro SQL Server 2005
Database Design and
Optimization
5297FM 3/31/06 1:37 PM Page i
Pro SQL Server 2005 Database Design and Optimization
Copyright © 2006 by Louis Davidson, Kevin Kline, and Kurt Windisch
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): 981-1-59059-529-9
ISBN-10 (pbk): 1-59059-529-7
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: Matthew Moodie
Technical Reviewers: Dejan Sarka, Andrew Watt
Editorial Board: Steve Anglin, Ewan Buckingham, Gary Cornell, Jason Gilmore, Jonathan Gennick,
Jonathan Hassell, James Huddleston, Chris Mills, Matthew Moodie, Dominic Shakeshaft, Jim Sumser,
Keir Thomas, Matt Wade
Project Manager: Elizabeth Seymour
Copy Edit Manager: Nicole LeClerc
Copy Editors: Susannah Pfalzer, Nicole LeClerc
Assistant Production Director: Kari Brooks-Copony
Production Editor: Laura Esterman
Compositor: Lynn L’Heureux
Proofreader: Lori Bring
Indexer: Valerie Perry


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 , or
visit .
For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley, CA
94710. Phone 510-549-5930, fax 510-549-5939, e-mail , or visit .
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 in the Source Code section.
5297FM 3/31/06 1:37 PM Page ii
To my wife Val and daughter Chrissy for putting up with me again spending two months of
Sundays stuck behind a laptop. Their love and support mean the world to me.
—Louis Davidson
5297FM 3/31/06 1:37 PM Page iii
5297FM 3/31/06 1:37 PM Page iv
Contents at a Glance
Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
About the Authors
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
About the Technical Reviewers
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
Acknowledgments
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
■CHAPTER 1 Introduction to Database Concepts. . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
■CHAPTER 2 Data Modeling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
■CHAPTER 3 Conceptual Data Modeling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71

■CHAPTER 4 The Normalization Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
■CHAPTER 5 Implementing the Base Table Structures . . . . . . . . . . . . . . . . . . . 181
■CHAPTER 6 Protecting the Integrity of Your Data . . . . . . . . . . . . . . . . . . . . . . . . 273
■CHAPTER 7 Securing Access to the Data
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
■CHAPTER 8 Table Structures and Indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
■CHAPTER 9 Coding for Concurrency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439
■CHAPTER 10 Code-Level Architectural Decisions . . . . . . . . . . . . . . . . . . . . . . . . . 489
■CHAPTER 11 Database Interoperability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 541
■APPENDIX A Codd’s 12 Rules for an RDBMS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573
■APPENDIX B Datatype Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 581
■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 613
v
5297FM 3/31/06 1:37 PM Page v
5297FM 3/31/06 1:37 PM Page vi
Contents
Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
About the Authors
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
About the Technical Reviewers
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
Acknowledgments
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
■CHAPTER 1 Introduction to Database Concepts. . . . . . . . . . . . . . . . . . . . . . . . . 1
Database Design Phases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Conceptual . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Logical . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Physical . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

Relational Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Database and Schema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Tables, Rows, and Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
The Information Principle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Domains . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Missing Values (NULLs) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Foreign Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Types of Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Data Access Language (SQL) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Understanding Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Functional Dependency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Determinant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Multivalued Dependency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
vii
5297FM 3/31/06 1:37 PM Page vii
■CHAPTER 2 Data Modeling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Introduction to Data Modeling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Entities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Entity Naming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Primary Key . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Alternate Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Foreign Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Domains . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Naming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46

Identifying Relationship . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Nonidentifying Relationship . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Optional Identifying Relationship . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Cardinality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Role Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Other Types of One-to-N Relationships . . . . . . . . . . . . . . . . . . . . . . . . 54
Subtypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Many-to-Many Relationship . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Verb Phrases (Relationship Names) . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Descriptive Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Alternative Modeling Methodologies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Information Engineering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Chen ERD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 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 Gathering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Client Interviews
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Questions to Be Answered . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
Existing Systems and Prototypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Other Types of Documentation
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Identifying Objects and Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Identifying Entities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
Relationships Between Entities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
Identifying Attributes and Domains . . . . . . . . . . . . . . . . . . . . . . . . . . . 99

■CONTENTSviii
5297FM 3/31/06 1:37 PM Page viii
Identifying Business Rules and Processes . . . . . . . . . . . . . . . . . . . . . . . . . 112
Identifying Business Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
Identifying Fundamental Processes . . . . . . . . . . . . . . . . . . . . . . . . . . 114
Finishing the Conceptual Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
Identifying Obvious Additional Data Needs . . . . . . . . . . . . . . . . . . . . 116
Review with the Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Repeat Until the Customer Agrees with Your List of Objects . . . . . 118
Best Practices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
■CHAPTER 4 The Normalization Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Why Normalize? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Eliminating Duplicated Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Avoiding Unnecessary Coding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Keeping Tables Thin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Maximizing Clustered Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Lowering the Number of Indexes Per Table . . . . . . . . . . . . . . . . . . . 123
How Far to Normalize . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
The Process of Normalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Entity and Attribute Shape: First Normal Form
. . . . . . . . . . . . . . . . . . . . . 125
All Attributes Must Be Atomic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
All Instances in an Entity Must Contain the Same
Number of Values
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
All Occurrences of an Entity Type in an Entity
Must Be Different
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Programming Anomalies Avoided by First Normal Form

. . . . . . . . 132
Clues That Existing Data Is Not in First Normal Form
. . . . . . . . . . . 136
Relationships Between Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Second Normal Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Third Normal Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
Boyce-Codd Normal Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Multivalued Dependencies in Entities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Fourth Normal Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
Fifth Normal Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
Denormalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
Best Practices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Bonus Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
The Story of the Book So Far . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
■CONTENTS ix
5297FM 3/31/06 1:37 PM Page ix
■CHAPTER 5 Implementing the Base Table Structures . . . . . . . . . . . . . . . . 181
The Design Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
Reviewing the Logical Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
Transforming the Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
Naming Concerns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
Dealing with Subtypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
Choosing Primary Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
Domain Specification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
Setting up Schemas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
Reviewing the “Final” Implementation Model . . . . . . . . . . . . . . . . . 214
Property Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Implementing the Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
Basic Table Creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218

Uniqueness Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
Default Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
Relationships (Foreign Keys) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
Large-Value Datatype Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
Collation (Sort Order) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
Computed Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
Implementing Complex Datatypes . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
Best Practices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
■CHAPTER 6 Protecting the Integrity of Your Data . . . . . . . . . . . . . . . . . . . . . 273
Best Practices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
Example Schema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
Basic Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
Constraints Based on Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
Handling Errors Caused by Constraints . . . . . . . . . . . . . . . . . . . . . . 286
Programmatic Data Protection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
DML Triggers
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
Programmatic Data Protection Outside the RDBMS . . . . . . . . . . . . . . . . . 329
More Best Practices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
The Continuing Story of the Book So Far . . . . . . . . . . . . . . . . . . . . . . . . . . 333
■CONTENTSx
5297FM 3/31/06 1:37 PM Page x
■CHAPTER 7 Securing Access to the Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
Controlling Data Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
Principals and Securables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337

Database Security Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
Controlling Object Access Via Coded Objects . . . . . . . . . . . . . . . . . 357
Views and Table-Valued Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 370
Obfuscating Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
Keeping an Eye on Users . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380
Watching Table History Using Triggers . . . . . . . . . . . . . . . . . . . . . . . 381
DDL Triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
Logging with Profiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388
Best Practices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392
■CHAPTER 8 Table Structures and Indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
Physical Database Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396
Files and Filegroups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396
Extents and Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
Indexes Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402
Basic Index Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402
Index Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404
Basics of Index Creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
Basic Index Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
Advanced Index Usage Scenarios . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
Foreign Key Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428
Using Indexed Views to Optimize Denormalizations . . . . . . . . . . . . 432
Best Practices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436
■CHAPTER 9 Coding for Concurrency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439
What Is Concurrency? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439
Query Optimization Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441
OS and Hardware Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444
Transaction Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445

Compiled SQL Server Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453
SQL Server Concurrency Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460
Locks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460
Isolation Levels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465
■CONTENTS xi
5297FM 3/31/06 1:37 PM Page xi
Coding for Integrity and Concurrency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475
Pessimistic Locking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476
Optimistic Locking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 478
Logical Unit of Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485
Best Practices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 488
■CHAPTER 10 Code-Level Architectural Decisions . . . . . . . . . . . . . . . . . . . . . . 489
Data-Access Strategies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489
Ad Hoc SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 490
Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 501
Opinions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512
Choosing Between T-SQL and CLR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 514
Good Reasons to Use .NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515
Hosting the CLR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 516
Using the .NET CLR for SQL Server Objects . . . . . . . . . . . . . . . . . . . 518
Guidelines and Opinions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 536
Best Practices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 537
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 538
■CHAPTER 11 Database Interoperability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 541
Step One: Datatypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 541
Step Two: Identifier Rules
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543
Step Three: Basic SQL Statements
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545

The DELETE Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 546
The INSERT Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 549
The SELECT Statement
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 552
The UPDATE Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557
Step Four: Creating Database Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . 560
Creating Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 560
Creating Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 564
Creating Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 566
Creating Triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 567
Creating Procedures and Functions . . . . . . . . . . . . . . . . . . . . . . . . . 570
Best Practices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 571
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 572
■CONTENTSxii
5297FM 3/31/06 1:37 PM Page xii
■APPENDIX A Codd’s 12 Rules for an RDBMS . . . . . . . . . . . . . . . . . . . . . . . . . . . 573
Rule 1: The Information Rule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573
Rule 2: Guaranteed Access Rule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 574
Rule 3: Systematic Treatment of NULL Values
. . . . . . . . . . . . . . . . . . . . . . 574
Rule 4: Dynamic On-Line Catalog Based on the Relational Model . . . . 575
Rule 5: Comprehensive Data Sublanguage Rule . . . . . . . . . . . . . . . . . . . . 575
Rule 6: View Updating Rule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 576
Rule 7: High-Level Insert, Update, and Delete . . . . . . . . . . . . . . . . . . . . . . 576
Rule 8: Physical Data Independence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 576
Rule 9: Logical Data Independence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 577
Rule 10: Integrity Independence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 578
Rule 11: Distribution Independence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 579
Rule 12: Non-Subversion Rule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 579
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 580

■APPENDIX B Datatype Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 581
Precise Numeric Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 582
Integer Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 583
Decimal Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 585
Approximate Numeric Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 589
Date and Time Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 591
smalldatetime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 591
datetime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 591
Using User-Defined Datatypes to Manipulate Dates
and Times
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 592
Character Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 594
char(length) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 595
varchar(length) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 596
varchar(max) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 596
text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 598
Unicode Character Strings: nchar, nvarchar,
nvarchar(max), ntext
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 598
Binary Data
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 598
binary(length) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 599
varbinary(length) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 600
varbinary(max)
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 600
image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 601
■CONTENTS xiii
5297FM 3/31/06 1:37 PM Page xiii
Other Datatypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 601
rowversion (a.k.a. timestamp) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 601

uniqueidentifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 602
cursor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 605
table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 605
XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 607
sql_variant Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 607
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 611
■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 613
■CONTENTSxiv
5297FM 3/31/06 1:37 PM Page xiv
Foreword
If you’re standing in a bookstore trying to decide whether or not to buy this book, let me help
you out—go ahead and get it! If you’re looking for a book like this, then you need this book,
not the next one on the shelf. Keep reading and I’ll tell you why.
Database design is an important thing. Project success or failure can hinge on solid
design. If done poorly, it’s one of the most crippling things you can do during the lifetime of a
project, and one of the most expensive to repair. Implementation of the design is also impor-
tant, and it’s also easy to mess this up.
Many books cover design, and many others cover implementation. Finding complete
coverage of both topics in a single tome allows you to get a consistent, logical view from
beginning to end. Although I’ve read both the SQL 2000 version and the SQL 2005 version of
this book, I wanted to see what others have said about the SQL 2000 version. Readers have
given Louis a 4.5 (out of 5). I agree. This is a fine book.
The book and Louis are similar in many ways. His friendly, easy-to-understand writing
style reflects Louis himself. He holds the coveted MVP award (Most Valuable Professional)
for SQL Server from Microsoft in recognition of his expertise and SQL community support.
Louis blogs regularly and is a valuable speaker and Special Interest Group (SIG) leader for the
Professional Association for SQL Server (PASS). Studying with this book feels like getting
advice and mentoring from a trusted friend.
Louis credits a few special mentors with his early training—people who wanted to do things
right. In the same way, his book can help you learn how to do things right. You’ll get practical

advice and ideas that, combined with your good work, can lead to successful projects.
Do not fear—you can do this! Many books on this subject are difficult to read, littered with
relational formulas. You will understand what Louis has to say, and you’ll get a quick kick-start
on best practices. I encourage you to read the book slowly and carefully, however. Engage your
brain. Think about the alternatives that Louis presents, understand them, and apply them to
your own environment.
I like Louis Davidson. I like this book. You will too!
Wayne Snyder
Managing consultant, Mariner
()
PASS Board of Directors ()
xv
5297FM 3/31/06 1:37 PM Page xv
5297FM 3/31/06 1:37 PM Page xvi
About the Authors
■LOUIS DAVIDSON has been in the IT industry for more than 14 years as a corporate database
developer and architect. The majority of his experience has been with Microsoft SQL Server, in
every version that has been released since 4.21a. Louis is a senior data architect for Compass
Technology, supporting the Christian Broadcasting Network and NorthStar Studios in
Nashville, Tennessee.
Louis has a bachelor’s degree in computer science from the University of Tennessee at
Chattanooga, with a minor in mathematics. He has been a volunteer with the Professional
Association for SQL Server (PASS) for more than 5 years. In October 2004, Louis was awarded
the Most Valuable Professional (MVP) award for SQL Server by Microsoft, an honor he is proud
to have been given. In his “free” time, Louis can often be found writing for his blog
or on the Microsoft SQL Server newsgroups and forums.
■KURT WINDISCH is a senior technical specialist with Levi, Ray & Shoup, Inc., a global provider
of technology solutions with headquarters in Springfield, Illinois. He has more than 15 years of
experience in IT, and is a DBA and technical architect for the internal IT department at LRS.
He spent 5 years serving on the board of directors for PASS, has written for several SQL Server

magazines, and has presented at conferences internationally on the topic of database pro-
gramming with SQL Server.
■KEVIN KLINE is the technical strategy manager for SQL Server solutions at Quest Software, a
leading provider of award-winning tools for database management and application monitor-
ing on the SQL Server platform. Kevin is the president of the international Professional
Association for SQL Server (PASS). He has been a Microsoft SQL Server MVP since 2004. Kevin
is the lead author of SQL in a Nutshell: A Desktop Quick Reference (O’Reilly Media Inc., 2004)
and Transact-SQL Programming (O’Reilly Media, Inc., 1999). Kevin writes the monthly SQL
Server Drilldown column for Database Trends & Applications, blogs at ,
and is a resident expert at SearchSQLServer.com. Kevin is a top-rated speaker, appearing at
international conferences such as Microsoft TechEd, DevTeach, PASS, Microsoft IT Forum,
and SQL Connections. When he’s not pulling his hair out over work, he loves to spend time
with his four kids and in his flower and vegetable gardens.
xvii
5297FM 3/31/06 1:37 PM Page xvii
5297FM 3/31/06 1:37 PM Page xviii
About the Technical Reviewers
■DEJAN SARKA, SQL Server MVP, Solid Quality Learning Mentor, is a trainer and consultant
working for many CTECs and development companies in Slovenia and other countries.
Besides training, he continuously works on OLTP, OLAP, and data mining projects, especially
at the design stage. He is a regular speaker at some of the most important international con-
ferences, such as TechEd, PASS, and the MCT conference. He’s also indispensable at regional
Microsoft TechNet meetings; at the NT Conference, the hugest Microsoft conference in Cen-
tral and Eastern Europe; and some other events. He’s the founder of the Slovenian SQL Server
Users Group. As a guest author, he contributed to two books—Inside Microsoft SQL Server
2005: T-SQL Querying (Microsoft Press, 2006) and Inside Microsoft SQL Server 2005: T-SQL Pro-
gramming (Microsoft Press, 2006)—both written by main author Itzik Ben-Gan. Dejan Sarka
also developed two courses for Solid Quality Learning: Data Modeling Essentials and Data
Mining with SQL Server 2005.
■ANDREW WATT is a Microsoft Most Valuable Professional (MVP) for SQL Server. He is an expe-

rienced author and independent consultant specializing in Microsoft technologies.
xix
5297FM 3/31/06 1:37 PM Page xix
5297FM 3/31/06 1:37 PM Page xx
Acknowledgments
Thanks go to:
My savior Jesus Christ, without whom I wouldn’t have had the strength to complete the
task of writing this book.
My daughter Chrissy Davidson for taking the cover picture.
My best friend in the world who got me started with computers back in college when I
still wanted to be a mathematician.
My mentors Mike Farmer, Chip Broecker, and Don Plaster for the leading they gave me
over the years.
Gary Cornell for giving me a chance to write the book that I wanted to write.
Ben Miller and Frank Castora for doing a beta read of the book.
My managers (chronologically speaking) Chuck Hawkins and Julie Porter for their under-
standing and patience with me when my eyes were droopy after a late night of writing, along
with all my friends at Compass Technology ().
Wayne Snyder for writing the awesome foreword.
Kevin Kline and Kurt Windisch for taking up the slack with topics I didn’t want to (couldn’t)
tackle.
The fantastic editing staff I’ve had, without whom the writing would sometimes appear to
come from an illiterate baboon. Most of these are included on the copyright page, but I want
to say a specific thanks to Tony Davis (who left the company just before the end) for making
this book great, despite my frequently rambling writing style.
Raul Garcia, who works on the Microsoft SQL Server Engine team, for information about
using EXECUTE AS and certificate-based security.
James Manning for the advice on READ COMMITTED SNAPSHOT.
Jan Shanahan for putting up with my annoying questions over the past two years.
All the MVPs that I’ve worked with over the past year and a half. Never a better group of

folks have I found. Steven Dybing and now Ben Miller have been great to work with. I want to
list a few others individually for things they’ve specifically done to help me out: Dejan Sarka
and Andrew Watt for reviewing this book with incredible vigor, and not letting me slide on
even small points; Steve Kass for giving me the code for demonstrating what’s wrong with the
money datatypes, as well as giving cool solutions to problems in newsgroups that made me
think; Erland Somarskog for helping me to understand a bit more about how error handling
works, and many other topics (not to mention his great website, />Adam Machanic for helping me with many topics on my blog and in newsgroups; Aaron
Bertrand for his great website and the shoe memories; Kalen Delaney
for all she has done for me and the community; Dr. Greg Low for putting me on his
podcast; Kim Tripp for the wonderful paper on SNAPSHOT isola-
tion levels. I also want to thank Tony Bain, Hillary Cotter, Mike Epprecht, Geoff Hiten,
Tom
Moreau, Andrew Kelly, Tony Rogerson, Linchi Shea, Paul Nielson, Hugo Kornelis, Tibor Karaszi,
Greg Linwood, Dr. Tom Moreau, Dan Guzman, Jacco Schalkwijk, Anith Sen, Jasper Smith,
xxi
5297FM 3/31/06 1:37 PM Page xxi
Ron Talmage, and Kent Tegels, because all of you have specifically helped me out over the past
year in the newsgroups, teaching me new things to make my book far better.
To the academics out there who have permeated my mind with database theory, such as
E. F. Codd, C. J. Date, Fabian Pascal, and Joe Celko; my professors at the University of Tennessee
at Chattanooga; et al. I wouldn’t know half as much without you.
Even with this large number of folks I have mentioned here, I am afraid I may have missed
someone. If so, thank you!
Louis Davidson
First off, I want to thank Louis for asking me to help contribute to such a practical book
on SQL Server 2005. He’s very knowledgeable and great to work with, and his commitment is
something I admire. I’d also like to thank SQL Server guru Gert Drapers, whose insight into the
SQLCLR and its uses provided lots of ideas to explore with the new technology. Thanks to all
my friends in the PASS organization—past and present members of the board of directors,
members of the Microsoft SQL Server Development and Product Services and Support teams,

PASS volunteers, and PASS members with whom I’ve had the privilege of meeting and build-
ing lasting friendships. Their wisdom and friendship is something I value.
Thanks especially to my son Ron, and three daughters Lauren, Alicia, and Courtney, who
consistently remind me of what’s really important. Finally, thanks to my wife, Sue, who had to
endure many nights and weekends listening to me complain about code not working. She allows
me my computer time but also reminds me there’s more to life than fast-running queries.
Kurt Windisch
■ACKNOWLEDGEMENTSxxii
5297FM 3/31/06 1:37 PM Page xxii
Introduction
I am not young enough to know everything.
—Oscar Wilde
There was a time when I felt I knew everything about SQL and database design. That time
was just before I wrote my first book, Professional SQL Server 2000 Database Design.
1
Even
now, my percentage of all knowledge is dwindling, while at the same time the amount of stuff
that I know grows every day. I realize now that books could be written on what I don’t know
about SQL Server, and this keeps getting truer and truer as the years pass. On the bright side,
this has more to do with the reality that SQL Server just keeps growing and adding more com-
plex and cool features than one person could master. It turns out that a book can be written
on what I do know about SQL Server, and you hold in your very hands the third generation of
that book (or you could be looking at an electronic copy, but the image of a person staring at
an electronic device isn’t nearly as poetic, even if I do prefer a book I can read on my Pocket
PC over one I’d have to lug around).
If you have had any contact with the previous versions of the book (either the Apress model
from 2003, or the one with my mug shot adorning the front cover from 2001, from another pub-
lisher), you may well wonder if it’s worth your time and hard-earned money for this book. If you
never saw these books, you might be figuring you’ve done well enough so far without it. “Do I
really need this book?” Face it, asking me is like asking a vacuum-cleaner salesperson if your old

vacuum cleaner needs to be replaced. I am not the most reliable person to ask. If I had my way,
this book would be used by everybody for everything. Beyond requiring every programmer in
every discipline to own one copy for home and one for office, children would read it in tenth
grade English as one of the classics of American (technical) literature. I would even suggest that
Oprah feature it as her book of the month, and for having read this far in the introduction you
would be required to purchase ten copies right now and give them to ten people you know,
promising them bad luck if they failed in the task. Then I could afford to send my daughter to
Northwestern, or maybe buy a rocket ship (either of which would be quite nice).
To illustrate the theme of each chapter, I’ve picked quotes from some great folks to start
each chapter (even a Dilbert cartoon), but I wanted to highlight one of my favorite quotes:
A man with one watch knows what time it is; a man with two watches is never quite sure.
2
—Lee Segall
xxiii
1. Luckily, that unbounded enthusiasm got me started. If I hadn’t thought I knew everything, I don’t know
if I could have ever started on that first book.
2. I should also be clear that Mr. Segall wasn’t talking about anything technology related. The first part of
the quote is: “It is possible to own too much . . .” As a gadget fanatic, I have to disagree with this part,
but it makes a good point about stuff that tells you the “same things.”
5297FM 3/31/06 1:37 PM Page xxiii
Have you ever been in a room with two clocks, and the time didn’t match? My wife’s alarm
clock is always set ten minutes fast, and occasionally when I’m groggily looking around to see
if it’s time to get up, I look at the wrong clock and end up awake and out of bed far too early.
Because of mismatched data, I made a poor decision and ended up out of bed before eight
o’clock. (I know that all you morning people think I’m nuts, but every one of you who con-
stantly replace alarm clocks because the snooze buttons are worn out understand exactly
where I’m coming from.)
One of the themes that you’ll find repeated throughout the book is that if you only have
one version of a data value, there’s no question which one is the most correct. Of course, your
data is only as right as the person who entered it. The old adage of garbage in, garbage out still

applies. My wife’s clock is intentionally set ten minutes off, after all. Removing my clock
wouldn’t make it right, but I’d eventually get used to the fact that I don’t want to get up before
ten after eight on her clock, instead of eight. The process of eliminating redundancy is known
as normalization, and is covered in its own large chapter (Chapter 4).
Once normalized, databases are straightforward to work with, because everything is in its
logical place, much like a well-organized cupboard. When you need paprika, it’s easier to go to
the paprika slot in the spice rack than it is to have to look for it “wherever it was put,” but
many systems are organized just this way. Even if every item has an assigned place, what value
is it if it’s too hard to find? Imagine if a phone book wasn’t sorted at all. What if the dictionary
was organized by placing a word where it would fit in the text? With proper organization, it will
be almost instinctive where to go to get the data you need, even if you have to write a join or
two. I mean, isn’t that fun after all?
A common misconception that I hope to alleviate with this book is the difference between
denormalization and a poorly designed database. Too often the term denormalized is used as
a nice word to cover up for a poor design. The key is that a denormalized database was, at
some time in its lifespan, normalized. Carefully applied denormalizations are sometimes use-
ful for performance. Not much time is spent on denormalizing in the book, simply because it’s
the process of undoing the process of normalization. (Reading Chapter 4 in reverse will do the
trick nicely. OK, maybe it takes a bit more than that.)
You might also be surprised to find out that database design is quite a straightforward
task, and not as difficult as it may sound. Doing it right is going to take more up-front time at
the beginning of a project than just slapping together the data storage as you go along, but it
pays off throughout the full lifecycle of a project. This brings me to one of the most challeng-
ing things about doing database design right: it takes more time than not doing it (this is a
battle that can frequently be had in project planning meetings). Because there’s nothing visual
to excite the client, database design is one of the phases of a project that often gets squeezed
to make things seem to go faster. Even the least challenging or uninteresting user interface is
still miles more interesting to the average customer than the most beautiful data model. Pro-
gramming the UI takes center stage, even though the data is generally why a system gets
funded and finally created. It’s not that your colleagues won’t notice the difference between a

cruddy data model and one that’s a “thing of beauty.” They certainly will, but the amount of
time required to decide the right way to store data correctly can be overlooked when program-
mers need to “code.” I wish I had an answer for that problem, because I could sell a million
books with just that answer. This book will assist you with some techniques and processes that
will help you through the process of designing databases, in a way that’s clear enough for
novices and helpful to even the most seasoned professional.
■INTRODUCTIONxxiv
5297FM 3/31/06 1:37 PM Page xxiv

×