Pro
SQL
Server 2012
Relational
Database
Design
and
Implementation
Louis Davidson
with
Jessica
M. Moss
Apress-Contents
Foreword xix
Aboutthe Author xxii
About the Technical Reviewer xxiii
Acknowledgments
xxvIntroduction xxvii
Chapter
1: The Fundamentals... 1Taking
aBrief JauntThrough History
2 IntroducingCodd's Rules foran RDBMS 3NoddingatSQL Standards 8
Recognizing
Relational Data Structures 9 Introducing Databases and Schemas 10Understanding Tables,Rows,and Columns 10
WorkingwithMissingValues(NULLs) 15
DefiningDomains 16
Storing Metadata 17
Assigning UniquenessConstraints(Keys) 18
Understanding Relationships
24Workingwith Binary Relationships 26
Workingwith Nonbinary Relationships 29
Understanding
Dependencies
30Workingwith FunctionalDependencies 30
Workingwith Determinants 31
:CONTENTS
Relational
Programming
31Outlining
theDatabase-Specific Project
Phases 32Conceptual Phase 33
LogicalPhase 34
Physical 35
Storage Phase 35
Summary
, 35Chapter 2: Introductionto
Requirements
37Documenting Requirements
39Gathering Requirements
41Interviewing
Clients 42Asking
theRight
Questions 43What Data Is Needed? 43
How Will the Data Be Used? 44
What Rules Govern the Use of the Data? 44
What Data IsReported On? 45
Where Is the Data Now? 46
Will the Data Need to Be Integratedwith OtherSystems? 47 How Much Is This Data Worth? 47
Who Will Use the Data? 47
Working
withExisting Systems
andPrototypes
48Utilizing
OtherTypes
of Documentation 48EarlyProjectDocumentation 49
ContractsorClientWork Orders 49
Level ofServiceAgreement 49
AuditPlans 50
Following
Best Practices 50Summary
50CONTENTS
Chapter
3: TheLanguage
of DataModeling
53Introducing
DataModeling
54Entities 55 Attributes 58 PrimaryKeys 59 Alternate Keys 61 Foreign Keys 62 Domains 63 Naming 65
Relationships
67 Identifying Relationships 68 Nonidentifying Relationships 69 Role Names 71 Relationship Cardinality 72Verb Phrases(Relationship Names) 79
Descriptive Information 81
Alternative
Modeling
Methodologies 82Information Engineering 83
Chen ERD 85
Visio 86
ManagementStudio Database Diagrams 87
Best Practices ...88
Summary
89Chapter
4: Initial Data Model Production 91Example
Scenario 92Identifying
Entities 93People 94
Places 95
CONTENTS Ideas 96 Documents 97 Groups 98 Other Entities 99 Entity Recap 100
Relationships
betweenEntities 102One-to-N Relationships 102
Many-to-ManyRelationships 106
Listing Relationships 107
Identifying
Attributes and Domains 109Identifiers m
Descriptive Information 113
Locators 113
Values 115
RelationshipAttributes 116
A List ofEntities, Attributes,and Domains 117
Identifying
Business Rules 120Identifying
Fundamental Processes 122The IntermediateVersion ofthe
Logical
Model 124IdentifyingObviousAdditional Data Needs 124
Review with the Client 125
RepeatUntil the CustomerAgreeswith YourModel 126
Best Practices 126
Summary
127Chapter
5: Normalization , 129The Process of Normalization 130
Table and Column
Shape
131All Columns Must Be Atomic 131
All Rows Must Contain the Same Number of Values 139
jCONTENTS
All Rows Must Be Different 141
CluesThatan Existing DesignIs Not in First Normal Form 143
Relationships
BetweenColumns 144BCNF Defined 144
PartialKey Dependency 146 EntireKey Dependency 147
Surrogate Keys
EffectonDependency
149DependencyBetween Rows 151
Clues That Your Database Is Not inBCNF 152 Positional Meaning 155 Tables with
Multiple Meanings
, 156Fourth Normal Form: IndependentMultivalued Dependencies 157 Fifth Normal Form 159
Denormalization 162
Best Practices 165
Summary
165The
Story
of the Book So Far 167Chapter
6:Physical
ModelImplementation
CaseStudy
169Choosing
Names 172Table Naming 173
Naming Columns 175
Model NameAdjustments 176
Choosing Key
Implementation
177PrimaryKey ....177
AlternateKeys 182
Determining
DomainImplementation
.• 184Implementas aColumnorTable? 186
wiCONTENTS
Choosing Nullability 198
Choosinga Collation 199
Setting Up
Schemas 200Adding Implementation
Columns 201Using
DDL to Create the Database 202Creatingthe BasicTable Structures 204
Adding UniquenessConstraints 214
BuildingDefault Constraints 218
Adding Relationships (ForeignKeys) 219
Adding Basic Check Constraints 224 Triggersto Maintain Automatic Values 226
DocumentingYour Database 230
Viewingthe Basic Metadata 233
Unit
Testing
Your Structures 237Best Practices 241
Summary
242Chapter
7: Data Protection with CheckConstraints andTriggers
245Check Constraints 247
CHECK Constraints BasedonSimple Expressions 252
CHECK ConstraintsUsingFunctions 254
EnhancingErrors Caused byConstraints 258
DML
Triggers
260AFTERTriggers 261 RelationshipsThatSpanDatabases and Servers 279 INSTEAD OFTriggers 283
Dealing
withTriggers
andConstraints Errors 292Best Practices 297
Summary
, , 298CONTENTS
Chapter
8: Patterns and Anti-Patterns 301Desirable Patterns 302
Uniqueness 302
Data-DrivenDesign 319
Hierarchies 320
Images,
Documents, and OtherFiles,OhMy
332Generalization 340 Storing User-SpecifiedData 345
Anti-Patterns 359
UndecipherableData 360
One-Size-Fits-AllKeyDomain 361
GenericKeyReferences 364
OverusingUnstructured Data 367
Summary
368Chapter9: Database
Security
andSecurity
Patterns 371 DatabaseAccessPrerequisites
372Guidelines for ServerSecurity 373
Principals and Securables 374
Connectingto the Server 375
Using Loginand User 376
Usingthe Contained Database Model 379
Impersonation 383
Database Securables 386
Grantable Permissions 387
ControllingAccess toObjects 388
Roles 392
Schemas 400
a,CONTENTS
ControllingAccess to Data viaT-SQL-Coded
Objects
402StoredProceduresand ScalarFunctions 402
Impersonation
within Objects 404 ViewsandTable-Valued Functions410
Crossing
Database Lines 417 UsingCross-DatabaseChaining 418 UsingImpersonationtoCross Database Lines 423UsingaCertilicate-BasedTrust
424 DifferentServer(DistributedQueries) 426
Obfuscating
Data 427Monitoring
andAuditing
429Serverand Database Audit
430
WatchingTable History UsingDMLTriggers 434
DDLTriggers 438
Loggingwith Profiler 441
BestPractices 442
Summary 443
Chapter
10: Table Structures andIndexing
445Physical
Database Structure 447FilesandFilegroups 447
Extents andPages 450
DataonPages 453
Partitioning... 456
Indexes Overview 459
Basic Index Structure 460
Index
Types
462Clustered Indexes 462 Nonclustered Indexes 463
CONTENTS
Nonclustered Indexeson Clustered Tables 461
Nonclustered Indexeson aHeap 462
Basicsof Index Creation 464
BasicIndex UsagePatterns 466
Using Clustered Indexes 467
UsingNonclustered Indexes 471
Using UniqueIndexes 484
Advanced Index
Usage
Scenarios 484IndexingForeign Keys 489
IndexingViews 493
Index
Dynamic Management
View Queries 497MissingIndexes 497
Index Utilization Statistics 500
Fragmentation 501
BestPractices 501
Summary
,...503Chapter11:
Coding
forConcurrency
505What Is
Concurrency?
506OS and Hardware Concerns 508
Transactions 509
Transaction Syntax 510
Compiled SQLServer Code 518
Isolating
Sessions 527Locks 528
Isolation Levels 533
Coding
forIntegrity
andConcurrency
546PessimisticLocking 546 ImplementingaSingle-ThreadedCode Block 549
Optimistic Locking 552
;CONTENTS
Row-BasedLocking 553
LogicalUnitof Work 558
Best Practices 560
Summary
561Chapter
12: Reusable Standard DatabaseComponents
563Numbers Table 564
DeterminingtheContents ofaString 568 Finding GapsinaSequenceof Numbers 570
SeparatingCommaDelimited Items 571
StupidMathematicTricks 573
CalendarTable 576 Utility
Objects
585 Monitoring Objects 586 Extended DDL Utilities 588Logging
Objects
590 Other Possibilities 592Summary
593Chapter
13:Considering
Data AccessStrategies...
595Ad Hoc SQL 597 Advantages 597 Pitfalls 607 Stored Procedures 613 Encapsulation 614 DynamicProcedures 616 Security 619 Performance 621 Pitfalls 623
AllThingsConsidered...What Do IChoose? 627
CONTENTS
T-SQLandthe CLR 629
GuidelinesforChoosingT-SQL 633 GuidelinesforChoosingaCLRObject 634
CLRObject Types 634
Best Practices 637
Summary
638Chapter
14:Reporting Design
639Reporting Styles
639Analytical Reporting 640
Aggregation Reporting 641
Requirements-Gathering
Process 641 DimensionalModeling
forAnalytical Reporting
642Dimensions 644
Facts 655
Analytical Querying
661Queries 661
Indexing 663
Summary Modeling
forAggregation Reporting
664InitialSummaryTable 665
AdditionalSummaryTables 667