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

Expert SQL Server 2008 Development

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 (9.89 MB, 454 trang )

Aitchison
Machanic
SQL Server 2008 Development
Companion
eBook Available
Expert
trim = 7.5" x 9.25" spine = 0.84375" 456 page count
THE EXPERT’S VOICE
®
IN SQL SERVER
Expert

SQL Server 2008
Development
Alastair Aitchison
and
Adam Machanic
Advanced SQL Server techniques for
database professionals
this print for content only—size & color not accurate
BOOKS FOR PROFESSIONALS BY PROFESSIONALS
®
US $49.99
Shelve in:
Databases / SQL Server
User level:
Intermediate / Advanced
www.apress.com
SOURCE CODE ONLINE
Companion eBook


See last page for details
on $10 eBook version
ISBN 978-1-4302-7213-7
9 781430 272137
5 49 9 9
Expert SQL Server 2008 Development
Dear Reader,
There are already many books written about SQL Server, so why did I choose to
help write yet another one? What makes this any different to the rest? The answer
is that Expert SQL Server 2008 Development, unlike most books on the subject, is
not intended to provide a comprehensive reference to the features available in
SQL Server 2008. Such information is available in Microsoft Books Online, and
has been repeated in many books already. Instead, my aim is to share knowledge
and skills required to create first-class database applications, which exemplify
best practices in database development.
The topics covered in this book represent interesting, sometimes complex,
and frequently misunderstood facets of database development. Understanding
these areas will set you apart as an expert SQL Server developer. Some of the top-
ics are hotly debated in the software community, and there is not always a single
“best” solution to any given problem. Instead, I’ll show you a variety of approach-
es, and give you the information and tools to decide which is most appropriate
for your particular environment.
After reading this book, you will gain an appreciation of areas such as test-
ing and exception handling, to ensure your code is robust, scalable, and easy to
maintain. You’ll learn how to create secure databases by controlling access to sen-
sitive information, and you’ll find out how to encrypt data to protect it from pry-
ing eyes. You’ll also learn how to create flexible, high-performance applications
using dynamic SQL and SQLCLR, and you’ll discover various models of handling
concurrent users of a database. Finally, I’ll teach you how to deal with complex
data representing temporal, spatial, and hierarchical information. Together, we’ll

uncover some of the interesting issues that can arise in these situations.
I’ve worked hard on this book, to make it useful to readers of all skill levels.
Beginner, expert, or in between, you’ll find something of use in this book. My
hope is that it helps you become truly an expert SQL Server developer.
Alastair Aitchison
THE APRESS ROADMAP
Expert
SQL Server 2008 Development
Beginning T-SQL 2008
Accelerated SQL Server 2008
Pro T-SQL 2008
Programmer’s Guide
SQL Server 2008
Transact-SQL Recipes
CYAN
MAGENTA
YELLOW
BLACK
PANTONE 123 C
Alastair Aitchison,
Author of
Beginning Spatial With
SQL Server 2008
Adam Machanic,
Author of
Expert SQL Server 2005
Development


Expert SQL Server

2008 Development












  
Alastair Aitchison
Adam Machanic


Expert SQL Server 2008 Development
Copyright © 2009 by Alastair Aitchison and Adam Machanic
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-7213-7
ISBN-13 (electronic): 978-1-4302-7212-0
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.
President and Publisher: Paul Manning

Lead Editor: Jonathan Gennick
Technical Reviewer: Evan Terry
Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell,
Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes,
Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft,
Matt Wade, Tom Welsh
Coordinating Editor: Mary Tobin
Copy Editor: Damon Larson
Compositor: Bytheway Publishing Services
Indexer: Barbara Palumbo
Artist: April Milne
Cover Designer: Anna Ishchenko
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 .
For information on translations, please e-mail , or visit .
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
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 . You will need to
answer questions pertaining to this book in order to successfully download the code.


For Clare and Douglas

 CONTENTS

Contents at a Glance
 Contents at a Glance.............................................................................................. iv
 Contents.................................................................................................................. v
 About the Author.................................................................................................. xvi
 About the Technical Reviewer ............................................................................ xvii
 Acknowledgments ............................................................................................. xviii
 Preface................................................................................................................. xix
 Chapter 1: Software Development Methodologies for the Database World ............1
 Chapter 2: Best Practices for Database Programming .........................................23
 Chapter 3: Testing Database Routines..................................................................49
 Chapter 4: Errors and Exceptions .........................................................................71
 Chapter 5: Privilege and Authorization...............................................................101
 Chapter 6: Encryption .........................................................................................121
 Chapter 7: SQLCLR: Architecture and Design Considerations.............................159
 Chapter 8: Dynamic T-SQL ..................................................................................195
 Chapter 9: Designing Systems for Application Concurrency ..............................235
 Chapter 10: Working with Spatial Data...............................................................283
 Chapter 11: Working with Temporal Data...........................................................321
 Chapter 12: Trees, Hierarchies, and Graphs .......................................................371
Index ....................................................................................................................419
iv

Contents
 Contents at a Glance .............................................................................................. iv
 Contents.................................................................................................................. v
 About the Author.................................................................................................. xvi
 About the Technical Reviewer ............................................................................ xvii
 Acknowledgments ............................................................................................. xviii
 Preface................................................................................................................. xix
 Chapter 1: Software Development Methodologies for the Database World ............1

Architecture Revisited .......................................................................................................1
Coupling.................................................................................................................................................... 3
Cohesion................................................................................................................................................... 4
Encapsulation ........................................................................................................................................... 5
Interfaces.................................................................................................................................................. 5
Interfaces As Contracts ............................................................................................................................ 6
Interface Design ....................................................................................................................................... 6
Integrating Databases and Object-Oriented Systems........................................................8
Data Logic............................................................................................................................................... 10
Business Logic........................................................................................................................................ 11
Application Logic .................................................................................................................................... 12
The “Object-Relational Impedance Mismatch” ...............................................................12
Are Tables Really Classes in Disguise? .................................................................................................. 13
Modeling Inheritance.............................................................................................................................. 14
ORM: A Solution That Creates Many Problems................................................................17
v
 CONTENTS
Introducing the Database-As-API Mindset.......................................................................18
The Great Balancing Act ..................................................................................................19
Performance........................................................................................................................................... 19
Testability ............................................................................................................................................... 20
Maintainability ........................................................................................................................................ 20
Security .................................................................................................................................................. 21
Allowing for Future Requirements.......................................................................................................... 21
Summary .........................................................................................................................22
Best Practices for Database Programming......................................................................23
 Chapter 2: Best Practices for Database Programming .........................................23
Defensive Programming ..................................................................................................23
Attitudes to Defensive Programming...................................................................................................... 24
Why Use a Defensive Approach to Database Development?.................................................................. 27

Best Practice SQL Programming Techniques ..................................................................28
Identify Hidden Assumptions in Your Code............................................................................................. 29
Don’t Take Shortcuts .............................................................................................................................. 33
Testing.................................................................................................................................................... 36
Code Review........................................................................................................................................... 39
Validate All Input..................................................................................................................................... 40
Future-proof Your Code .......................................................................................................................... 42
Limit Your Exposure................................................................................................................................ 43
Exercise Good Coding Etiquette.............................................................................................................. 43
Comments ......................................................................................................................................... 44
Indentations and Statement Blocks................................................................................................... 45
If All Else Fails. . . ................................................................................................................................... 46
Creating a Healthy Development Environment ................................................................46
Summary .........................................................................................................................47
vi
 CONTENTS
 Chapter 3: Testing Database Routines..................................................................49
Approaches to Testing.....................................................................................................49
Unit and Functional Testing .............................................................................................50
Unit Testing Frameworks ....................................................................................................................... 52
Regression Testing................................................................................................................................. 55
Guidelines for Implementing Database Testing Processes and Procedures....................55
Why Is Testing Important?...................................................................................................................... 56
What Kind of Testing Is Important? ........................................................................................................ 56
How Many Tests Are Needed?................................................................................................................ 57
Will Management Buy In?....................................................................................................................... 58
Performance Monitoring Tools ........................................................................................58
Real-Time Client-Side Monitoring .......................................................................................................... 59
Server-Side Traces................................................................................................................................. 60
System Monitoring ................................................................................................................................. 61

Dynamic Management Views (DMVs)..................................................................................................... 62
Extended Events ..................................................................................................................................... 63
Data Collector ......................................................................................................................................... 65
Analyzing Performance Data ...........................................................................................67
Capturing Baseline Metrics .................................................................................................................... 67
Big-Picture Analysis ............................................................................................................................... 68
Granular Analysis.................................................................................................................................... 68
Fixing Problems: Is It Sufficient to Focus on the Obvious?..................................................................... 70
Summary .........................................................................................................................70
 Chapter 4: Errors and Exceptions .........................................................................71
Exceptions vs. Errors .......................................................................................................71
How Exceptions Work in SQL Server ...............................................................................72
Statement-Level Exceptions................................................................................................................... 73
Batch-Level Exceptions .......................................................................................................................... 73
vii
 CONTENTS
Parsing and Scope-Resolution Exceptions ............................................................................................. 75
Connection and Server-Level Exceptions ............................................................................................... 76
The XACT_ABORT Setting....................................................................................................................... 77
Dissecting an Error Message.................................................................................................................. 78
Error Number..................................................................................................................................... 78
Error Level ......................................................................................................................................... 79
Error State ......................................................................................................................................... 79
Additional Information ....................................................................................................................... 80
SQL Server’s RAISERROR Function......................................................................................................... 81
Formatting Error Messages ............................................................................................................... 82
Creating Persistent Custom Error Messages..................................................................................... 83
Logging User-Thrown Exceptions...................................................................................................... 85
Monitoring Exception Events with Traces .............................................................................................. 85
Exception Handling..........................................................................................................85

Why Handle Exceptions in T-SQL?.......................................................................................................... 86
Exception “Handling” Using @@ERROR................................................................................................. 86
SQL Server’s TRY/CATCH Syntax............................................................................................................ 87
Getting Extended Error Information in the Catch Block ..................................................................... 89
Rethrowing Exceptions...................................................................................................................... 90
When Should TRY/CATCH Be Used? ..................................................................................................91
Using TRY/CATCH to Build Retry Logic .............................................................................................. 91
Exception Handling and SQLCLR ............................................................................................................ 93
Transactions and Exceptions...........................................................................................96
The Myths of Transaction Abortion......................................................................................................... 96
XACT_ABORT: Turning Myth into (Semi-)Reality ....................................................................................98
TRY/CATCH and Doomed Transactions................................................................................................... 99
Summary .......................................................................................................................100
 Chapter 5: Privilege and Authorization...............................................................101
The Principle of Least Privilege .....................................................................................102
viii
 CONTENTS
Creating Proxies in SQL Server............................................................................................................. 103
Server-Level Proxies ....................................................................................................................... 103
Database-Level Proxies................................................................................................................... 104
Data Security in Layers: The Onion Model............................................................................................ 104
Data Organization Using Schemas.................................................................................105
Basic Impersonation Using EXECUTE AS .......................................................................107
Ownership Chaining.......................................................................................................110
Privilege Escalation Without Ownership Chains ............................................................112
Stored Procedures and EXECUTE AS .................................................................................................... 112
Stored Procedure Signing Using Certificates ....................................................................................... 114
Assigning Server-Level Permissions .................................................................................................... 117
Summary .......................................................................................................................119
 Chapter 6: Encryption .........................................................................................121

Do You Really Need Encryption?....................................................................................121
What Should Be Protected?.................................................................................................................. 121
What Are You Protecting Against?........................................................................................................ 122
SQL Server 2008 Encryption Key Hierarchy...................................................................123
The Automatic Key Management Hierarchy ......................................................................................... 123
Symmetric Keys, Asymmetric Keys, and Certificates...................................................................... 124
Database Master Key....................................................................................................................... 125
Service Master Key.......................................................................................................................... 125
Alternative Encryption Management Structures................................................................................... 125
Symmetric Key Layering and Rotation ............................................................................................ 126
Removing Keys from the Automatic Encryption Hierarchy.............................................................. 126
Extensible Key Management ........................................................................................................... 127
Data Protection and Encryption Methods ......................................................................128
Hashing................................................................................................................................................. 129
Symmetric Key Encryption ................................................................................................................... 130
ix
 CONTENTS
Asymmetric Key Encryption.................................................................................................................. 134
Transparent Data Encryption................................................................................................................ 136
Balancing Performance and Security ............................................................................139
Implications of Encryption on Query Design..................................................................145
Equality Matching Using Hashed Message Authentication Codes........................................................ 148
Wildcard Searches Using HMAC Substrings......................................................................................... 153
Range Searches.................................................................................................................................... 157
Summary .......................................................................................................................158
 Chapter 7: SQLCLR: Architecture and Design Considerations.............................159
Bridging the SQL/CLR Gap: The SqlTypes Library..........................................................160
Wrapping Code to Promote Cross-Tier Reuse ...............................................................161
The Problem ......................................................................................................................................... 161
One Reasonable Solution...................................................................................................................... 161

A Simple Example: E-Mail Address Format Validation ......................................................................... 162
SQLCLR Security and Reliability Features .....................................................................163
Security Exceptions .............................................................................................................................. 164
Host Protection Exceptions................................................................................................................... 165
The Quest for Code Safety.................................................................................................................... 168
Selective Privilege Escalation via Assembly References...................................................................... 168
Working with Host Protection Privileges ......................................................................................... 169
Working with Code Access Security Privileges ............................................................................... 173
Granting Cross-Assembly Privileges..................................................................................................... 175
Database Trustworthiness............................................................................................................... 175
Strong Naming................................................................................................................................. 177
Performance Comparison: SQLCLR vs. TSQL.................................................................178
Creating a “Simple Sieve” for Prime Numbers..................................................................................... 179
Calculating Running Aggregates .......................................................................................................... 181
String Manipulation .............................................................................................................................. 183
x
 CONTENTS
Enhancing Service Broker Scale-Out with SQLCLR .......................................................185
XML Serialization.................................................................................................................................. 185
XML Deserialization.............................................................................................................................. 186
Binary Serialization with SQLCLR ......................................................................................................... 187
Binary Deserialization........................................................................................................................... 191
Summary .......................................................................................................................194
 Chapter 8: Dynamic T-SQL ..................................................................................195
Dynamic T-SQL vs. Ad Hoc T-SQL..................................................................................196
The Stored Procedure vs. Ad Hoc SQL Debate...............................................................196
Why Go Dynamic?..........................................................................................................197
Compilation and Parameterization ....................................................................................................... 198
Auto-Parameterization.......................................................................................................................... 200
Application-Level Parameterization...................................................................................................... 202

Performance Implications of Parameterization and Caching ............................................................... 203
Supporting Optional Parameters....................................................................................205
Optional Parameters via Static T-SQL .................................................................................................. 206
Going Dynamic: Using EXECUTE ........................................................................................................... 212
SQL Injection......................................................................................................................................... 218
sp_executesql: A Better EXECUTE ........................................................................................................ 220
Performance Comparison..................................................................................................................... 223
Dynamic SQL Security Considerations...........................................................................230
Permissions to Referenced Objects...................................................................................................... 230
Interface Rules ..................................................................................................................................... 230
Summary .......................................................................................................................232
 Chapter 9: Designing Systems for Application Concurrency ..............................235
The Business Side: What Should Happen When Processes Collide?.............................236
Isolation Levels and Transactional Behavior .................................................................237
Blocking Isolation Levels ...................................................................................................................... 239
xi
 CONTENTS
READ COMMITTED Isolation............................................................................................................. 239
REPEATABLE READ Isolation............................................................................................................ 239
SERIALIZABLE Isolation.................................................................................................................... 240
Nonblocking Isolation Levels................................................................................................................ 241
READ UNCOMMITTED Isolation ........................................................................................................ 241
SNAPSHOT Isolation ........................................................................................................................ 242
From Isolation to Concurrency Control ................................................................................................. 242
Preparing for the Worst: Pessimistic Concurrency ........................................................243
Progressing to a Solution ..................................................................................................................... 244
Enforcing Pessimistic Locks at Write Time .......................................................................................... 249
Application Locks: Generalizing Pessimistic Concurrency ................................................................... 250
Hoping for the Best: Optimistic Concurrency.................................................................259
Embracing Conflict: Multivalue Concurrency Control ....................................................266

Sharing Resources Between Concurrent Users.............................................................269
Controlling Resource Allocation............................................................................................................ 272
Calculating Effective and Shared Maximum Resource Allocation........................................................ 277
Controlling Concurrent Request Processing......................................................................................... 279
Summary .......................................................................................................................281
 Chapter 10: Working with Spatial Data...............................................................283
Modeling Spatial Data....................................................................................................283
Spatial Reference Systems................................................................................................................... 286
Geographic Coordinate Systems ..................................................................................................... 286
Projected Coordinate Systems ........................................................................................................ 286
Applying Coordinate Systems to the Earth ........................................................................................... 288
Datum .............................................................................................................................................. 288
Prime Meridian ................................................................................................................................ 288
Projection ........................................................................................................................................ 289
Spatial Reference Identifiers ................................................................................................................ 290
xii
 CONTENTS
Geography vs. Geometry................................................................................................292
Standards Compliance ......................................................................................................................... 293
Accuracy............................................................................................................................................... 294
Technical Limitations and Performance ............................................................................................... 294
Creating Spatial Data.....................................................................................................296
Well-Known Text .................................................................................................................................. 296
Well-Known Binary ............................................................................................................................... 297
Geography Markup Language............................................................................................................... 298
Importing Data...................................................................................................................................... 298
Querying Spatial Data ....................................................................................................302
Nearest-Neighbor Queries.................................................................................................................... 304
Finding Locations Within a Given Bounding Box .................................................................................. 308
Spatial Indexing .............................................................................................................313

How Does a Spatial Index Work?.......................................................................................................... 313
Optimizing the Grid............................................................................................................................... 315
Summary .......................................................................................................................319
 Chapter 11: Working with Temporal Data...........................................................321
Modeling Time-Based Information ................................................................................321
SQL Server’s Date/Time Data Types..............................................................................322
Input Date Formats ............................................................................................................................... 323
Output Date Formatting ........................................................................................................................ 325
Efficiently Querying Date/Time Columns .............................................................................................. 326
Date/Time Calculations......................................................................................................................... 329
Truncating the Time Portion of a datetime Value ............................................................................ 330
Finding Relative Dates..................................................................................................................... 332
How Many Candles on the Birthday Cake?...................................................................................... 335
Defining Periods Using Calendar Tables........................................................................336
Dealing with Time Zones ...............................................................................................341
xiii
 CONTENTS
Storing UTC Time.................................................................................................................................. 343
Using the datetimeoffset Type.............................................................................................................. 344
Working with Intervals...................................................................................................346
Modeling and Querying Continuous Intervals....................................................................................... 347
Modeling and Querying Independent Intervals..................................................................................... 354
Overlapping Intervals............................................................................................................................ 358
Time Slicing.......................................................................................................................................... 362
Modeling Durations........................................................................................................365
Managing Bitemporal Data ............................................................................................366
Summary .......................................................................................................................370
 Chapter 12: Trees, Hierarchies, and Graphs .......................................................371
Terminology: Everything Is a Graph...............................................................................371
The Basics: Adjacency Lists and Graphs .......................................................................373

Constraining the Edges......................................................................................................................... 374
Basic Graph Queries: Who Am I Connected To? ................................................................................... 376
Traversing the Graph ............................................................................................................................ 378
Adjacency List Hierarchies ............................................................................................388
Finding Direct Descendants.................................................................................................................. 389
Traversing down the Hierarchy ............................................................................................................ 391
Ordering the Output......................................................................................................................... 392
Are CTEs the Best Choice? .............................................................................................................. 396
Traversing up the Hierarchy ................................................................................................................. 400
Inserting New Nodes and Relocating Subtrees .................................................................................... 401
Deleting Existing Nodes........................................................................................................................ 401
Constraining the Hierarchy................................................................................................................... 402
Persisted Materialized Paths .........................................................................................405
Finding Subordinates............................................................................................................................ 406
Navigating up the Hierarchy ................................................................................................................. 407
xiv
 CONTENTS
xv
Inserting Nodes .................................................................................................................................... 408
Relocating Subtrees ............................................................................................................................. 409
Deleting Nodes ..................................................................................................................................... 411
Constraining the Hierarchy................................................................................................................... 411
The hierarchyid Datatype...............................................................................................412
Finding Subordinates............................................................................................................................ 413
Navigating up the Hierarchy ................................................................................................................. 414
Inserting Nodes .................................................................................................................................... 415
Relocating Subtrees ............................................................................................................................. 416
Deleting Nodes ..................................................................................................................................... 417
Constraining the Hierarchy................................................................................................................... 417
Summary .......................................................................................................................418

Index ................................................................................................................419

About the Author
 Alastair Aitchison is a freelance technology consultant based in Norwich, England. He has experience
across a wide variety of software and service platforms, and has worked with SQL Server 2008 since the
earliest technical previews were made publicly available. He has implemented various SQL Server
solutions requiring highly concurrent processes and large data warehouses in the financial services
sector, combined with reporting and analytical capability based on the Microsoft business intelligence
stack. Alastair has a particular interest in analysis of spatial data, and is the author of Beginning Spatial
with SQL Server 2008 (Apress, 2009). He speaks at user groups and conferences, and is a highly active
contributor to several online support communities, including the Microsoft SQL Server Developer
Center forums.
xvi

About the Technical Reviewer


 Evan Terry is the Chief Technical Consultant at The Clegg Company,
specializing in data management, information and data architecture, database
systems, and business intelligence. His past and current clients include the State
of Idaho, Albertsons, American Honda Motors, and Toyota Motor Sales, USA. He is
the coauthor of Beginning Relational Data Modeling, has published several articles
in DM Review, and has presented at industry conferences and conducted private
workshops on the subjects of data and information quality, and information
management. He has also been the technical reviewer of several Apress books
relating to SQL Server databases. For questions or consulting needs, Evan can be
reached at
xvii
 CONTENTS
Acknowledgments

When I was asked to write this book, I jumped at the chance to work with the great bunch of folks at
Apress again. I am particularly lucky to have the assistance once more of two hugely talented
individuals, in the form of Jonathan Gennick and Evan Terry. As my editor, Jonathan has encouraged,
taught, and mentored me through the authoring process, and has never wavered in his support even
when the going got a bit tough (which, as in any publication schedule, at times it did!). Evan not only
provided the benefit of his wealth of technical knowledge, but also his authoring expertise, and at times
he simply provided a sensible voice of reason, all of which helped to improve the book significantly. I
would also like to thank Mary Tobin, who managed to keep track of all the deadlines and project
management issues, Damon Larson, for correcting my wayward use of the English language, and all the
other individuals who helped get this book into the form that you are now holding in your hands. Thank
you all.
My family have once again had to endure me spending long hours typing away at the keyboard, and
I thank them for their tolerance, patience, and support. I couldn’t do anything without them.
And thankyou to you, the reader, for purchasing this book. I hope that you find the content
interesting, useful, and above all, enjoyable to read.
xviii

xix
Preface
I’ve worked with Microsoft SQL Server for nearly ten years now, and I’ve used SQL Server 2008 since the
very first preview version was made available to the public. One thing I have noticed is that, with every
new release, SQL Server grows ever more powerful, and ever more complex. There is now a huge array of
features that go way beyond the core functionality expected from a database system and, with so many
different facets to cover, it is becoming ever harder to be a SQL Server "expert". SQL Server developers
are no longer simply expected to be proficent in writing T-SQL code, but also in XML and SQLCLR (and
knowing when to use each). You no longer execute a query to get a single result set from an isolated
database, but handle multiple active result sets derived from queries across distributed servers. The
types of information stored in modern databases represent not just character, numeric, and binary data,
but complex data such as spatial, hierarchical, and filestream data.
Attempting to comprehensively cover any one of these topics alone would easily generate enough

material to fill an entire book, so I'm not even going to try doing so. Instead, I’m going to concentrate on
what I believe you need to know to create high-quality database applications, based on my own practical
experience. I’m not going to waste pages discussing the ins and outs of some obscure or little-used
feature, unless I can show you a genuine use case for it. Nor will I insult your intelligence by laboriously
explaining the basics – I'll assume that you're already familiar with the straightforward examples covered
in Books Online, and now want to take your knowledge further.
All of the examples used in this book are based on real-life scenarios that I've encountered, and they
show you how to deal with problems that you're likely to face in most typical SQL Server environments. I
promise not to show you seemingly perfect solutions, which you then discover only work in the
artificially-cleansed "AdventureWorks" world; as developers we work with imperfect data, and I'll try to
show you examples that deal with the warts and all. The code examples were tested using the SQL Server
2008 Developer Edition with Service Pack 1 installed, but should work on all editions of SQL Server 2008
unless explicitly stated otherwise.
Finally, I hope that you enjoy reading this book and thinking about the issues discussed. The reason
why I enjoy database development is that it presents a never-ending set of puzzles to solve – and even
when you think you have found the optimum answer to a problem, there is always the possibility of
finding an even better solution in the future. While you shouldn't let this search for perfection detract
you from the job at hand (sometimes, "good enough" is all you need), there are always new techniques
to learn, and alternative methods to explore. I hope that you might learn some of them in the pages that
follow.

C H A P T E R 1

  

Software Development
Methodologies for the
Database World
Databases are software. Therefore, database application development should be treated in the same
manner as any other form of software development. Yet, all too often, the database is thought of as a

secondary entity when development teams discuss architecture and test plans, and many database
developers are still not aware of, or do not apply, standard software development best practices to
database applications.
Almost every software application requires some form of data store. Many developers go beyond
simply persisting application data, instead creating applications that are data driven. A data-driven
application is one that is designed to dynamically change its behavior based on data—a better term
might, in fact, be data dependent.
Given this dependency upon data and databases, the developers who specialize in this field have no
choice but to become not only competent software developers, but also absolute experts at accessing
and managing data. Data is the central, controlling factor that dictates the value that any application can
bring to its users. Without the data, there is no need for the application.
The primary purpose of this book is to encourage Microsoft SQL Server developers to become more
integrated with mainstream software development. These pages stress rigorous testing, well-thought-
out architectures, and careful attention to interdependencies. Proper consideration of these areas is the
hallmark of an expert software developer—and database professionals, as core members of any software
development team, simply cannot afford to lack this expertise.
In this chapter, I will present an overview of software development and architectural matters as they
apply to the world of database applications. Some of the topics covered are hotly debated in the
development community, and I will try to cover both sides, even when presenting what I believe to be
the most compelling argument. Still, I encourage you to think carefully about these issues rather than
taking my—or anyone else’s—word as the absolute truth. Software architecture is a constantly changing
field. Only through careful reflection on a case-by-case basis can you hope to identify and understand
the “best” possible solution for any given situation.
Architecture Revisited
Software architecture is a large, complex topic, partly due to the fact that software architects often like to
make things as complex as possible. The truth is that writing first-class software doesn’t involve nearly as
much complexity as many architects would lead you to believe. Extremely high-quality designs are
1
CHAPTER 1  SOFTWARE DEVELOPMENT METHODOLOGIES FOR THE DATABASE WORLD
possible merely by understanding and applying a few basic principles. The three most important

concepts that every software developer must know in order to succeed are coupling, cohesion, and
encapsulation:
• Coupling refers to the amount of dependency of one module within a system
upon another module in the same system. It can also refer to the amount of
dependency that exists between different systems. Modules, or systems, are said
to be tightly coupled when they depend on each other to such an extent that a
change in one necessitates a change to the other. This is clearly undesirable, as it
can create a complex (and, sometimes, obscure) network of dependencies
between different modules of the system, so that an apparently simple change in
one module may require identification of and associated changes made to a wide
variety of disparate modules throughout the application. Software developers
should strive instead to produce the opposite: loosely coupled modules and
systems, which can be easily isolated and amended without affecting the rest of
the system.
• Cohesion refers to the degree that a particular module or component provides a
single, well-defined aspect of functionality to the application as a whole. Strongly
cohesive modules, which have only one function, are said to be more desirable
than weakly cohesive modules, which perform many operations and therefore
may be less maintainable and reusable.
• Encapsulation refers to how well the underlying implementation of a module is
hidden from the rest of the system. As you will see, this concept is essentially the
combination of loose coupling and strong cohesion. Logic is said to be
encapsulated within a module if the module’s methods or properties do not
expose design decisions about its internal behaviors.
Unfortunately, these qualitative definitions are somewhat difficult to apply, and in real systems,
there is a significant amount of subjectivity involved in determining whether a given module is or is not
tightly coupled to some other module, whether a routine is cohesive, or whether logic is properly
encapsulated. There is no objective method of measuring these concepts within an application.
Generally, developers will discuss these ideas using comparative terms—for instance, a module may be
said to be less tightly coupled to another module than it was before its interfaces were refactored. But it

might be difficult to say whether or not a given module is tightly coupled to another, in absolute terms,
without some means of comparing the nature of its coupling. Let’s take a look at a couple of examples to
clarify things.
What is Refactoring?
Refactoring is the practice of reviewing and revising existing code, while not adding any new features or
changing functionality—essentially, cleaning up what’s there to make it work better. This is one of those
areas that management teams tend to despise, because it adds no tangible value to the application from a
sales point of view, and entails revisiting sections of code that had previously been considered “finished.”
2
CHAPTER 1  SOFTWARE DEVELOPMENT METHODOLOGIES FOR THE DATABASE WORLD

Coupling
First, let’s look at an example that illustrates basic coupling. The following class might be defined to
model a car dealership’s stock (to keep the examples simple, I’ll give code listings in this section based
on a simplified and scaled-down C#-like syntax):
class Dealership
{
// Name of the dealership
string Name;

// Address of the dealership
string Address;

// Cars that the dealership has
Car[] Cars;

// Define the Car subclass
class Car
{
// Make of the car

string Make;

// Model of the car
string Model;
}
}
This class has three fields: the name of the dealership and address are both strings, but the
collection of the dealership’s cars is typed based on a subclass, Car. In a world without people who are
buying cars, this class works fine—but, unfortunately, the way in which it is modeled forces us to tightly
couple any class that has a car instance to the dealer. Take the owner of a car, for example:
class CarOwner
{
// Name of the car owner
string name;

// The car owner's cars
Dealership.Car[] Cars
}
Notice that the CarOwner’s cars are actually instances of Dealership.Car; in order to own a car, it
seems to be presupposed that there must have been a dealership involved. This doesn’t leave any room
for cars sold directly by their owner—or stolen cars, for that matter! There are a variety of ways of fixing
this kind of coupling, the simplest of which would be to not define Car as a subclass, but rather as its own
stand-alone class. Doing so would mean that a CarOwner would be coupled to a Car, as would a
Dealership—but a CarOwner and a Dealership would not be coupled at all. This makes sense and more
accurately models the real world.
3

×