SQL Antipatterns: Avoiding the Pitfalls of Database Programming [1 ed.] 9781934356555, 1934356557

Each chapter in this book helps you identify, explain, and correct a unique and dangerous antipattern. The four parts of

218 32 2MB

English Pages 334 Year 2010

Report DMCA / Copyright

DOWNLOAD PDF FILE

Table of contents :
Contents......Page 7
Introduction......Page 13
Who This Book Is For......Page 14
What's in This Book......Page 15
What's Not in This Book......Page 17
Conventions......Page 18
Example Database......Page 19
Acknowledgments......Page 22
Logical Database Design Antipatterns......Page 24
Jaywalking......Page 25
Antipattern: Format Comma-Separated Lists......Page 26
How to Recognize the Antipattern......Page 29
Solution: Create an Intersection Table......Page 30
Naive Trees......Page 34
Antipattern: Always Depend on One's Parent......Page 35
How to Recognize the Antipattern......Page 39
Legitimate Uses of the Antipattern......Page 40
Solution: Use Alternative Tree Models......Page 41
ID Required......Page 54
Objective: Establish Primary Key Conventions......Page 55
Antipattern: One Size Fits All......Page 57
Legitimate Uses of the Antipattern......Page 61
Solution: Tailored to Fit......Page 62
Keyless Entry......Page 65
Antipattern: Leave Out the Constraints......Page 66
How to Recognize the Antipattern......Page 69
Solution: Declare Constraints......Page 70
Objective: Support Variable Attributes......Page 73
Antipattern: Use a Generic Attribute Table......Page 74
Legitimate Uses of the Antipattern......Page 80
Solution: Model the Subtypes......Page 82
Polymorphic Associations......Page 89
Objective: Reference Multiple Parents......Page 90
Antipattern: Use Dual-Purpose Foreign Key......Page 91
How to Recognize the Antipattern......Page 94
Legitimate Uses of the Antipattern......Page 95
Solution: Simplify the Relationship......Page 96
Objective: Store Multivalue Attributes......Page 102
Antipattern: Create Multiple Columns......Page 103
How to Recognize the Antipattern......Page 106
Legitimate Uses of the Antipattern......Page 107
Solution: Create Dependent Table......Page 108
Metadata Tribbles......Page 110
Antipattern: Clone Tables or Columns......Page 111
How to Recognize the Antipattern......Page 116
Legitimate Uses of the Antipattern......Page 117
Solution: Partition and Normalize......Page 118
Physical Database Design Antipatterns......Page 122
Rounding Errors......Page 123
Antipattern: Use FLOAT Data Type......Page 124
Solution: Use NUMERIC Data Type......Page 128
Objective: Restrict a Column to Specific Values......Page 131
Antipattern: Specify Values in the Column Definition......Page 132
How to Recognize the Antipattern......Page 135
Solution: Specify Values in Data......Page 136
Phantom Files......Page 139
Antipattern: Assume You Must Use Files......Page 140
How to Recognize the Antipattern......Page 143
Legitimate Uses of the Antipattern......Page 144
Solution: Use BLOB Data Types As Needed......Page 145
Index Shotgun......Page 148
Antipattern: Using Indexes Without a Plan......Page 149
How to Recognize the Antipattern......Page 153
Solution: MENTOR Your Indexes......Page 154
Query Antipatterns......Page 161
Fear of the Unknown......Page 162
Antipattern: Use Null as an Ordinary Value, or Vice Versa......Page 163
How to Recognize the Antipattern......Page 166
Solution: Use Null as a Unique Value......Page 168
Ambiguous Groups......Page 173
Antipattern: Reference Nongrouped Columns......Page 174
How to Recognize the Antipattern......Page 176
Legitimate Uses of the Antipattern......Page 178
Solution: Use Columns Unambiguously......Page 179
Random Selection......Page 183
Antipattern: Sort Data Randomly......Page 184
How to Recognize the Antipattern......Page 185
Solution: In No Particular Order…......Page 186
Poor Man's Search Engine......Page 190
Antipattern: Pattern Matching Predicates......Page 191
How to Recognize the Antipattern......Page 192
Solution: Use the Right Tool for the Job......Page 193
Spaghetti Query......Page 204
Antipattern: Solve a Complex Problem in One Step......Page 205
How to Recognize the Antipattern......Page 207
Legitimate Uses of the Antipattern......Page 208
Solution: Divide and Conquer......Page 209
Implicit Columns......Page 214
Antipattern: a Shortcut That Gets You Lost......Page 215
How to Recognize the Antipattern......Page 217
Legitimate Uses of the Antipattern......Page 218
Solution: Name Columns Explicitly......Page 219
Application Development Antipatterns......Page 221
Objective: Recover or Reset Passwords......Page 222
Antipattern: Store Password in Plain Text......Page 223
Legitimate Uses of the Antipattern......Page 225
Solution: Store a Salted Hash of the Password......Page 227
SQL Injection......Page 234
Antipattern: Execute Unverified Input As Code......Page 235
How to Recognize the Antipattern......Page 242
Solution: Trust No One......Page 243
Pseudokey Neat-Freak......Page 250
Antipattern: Filling in the Corners......Page 251
Solution: Get Over It......Page 254
See No Evil......Page 259
Antipattern: Making Bricks Without Straw......Page 260
How to Recognize the Antipattern......Page 262
Legitimate Uses of the Antipattern......Page 263
Solution: Recover from Errors Gracefully......Page 264
Diplomatic Immunity......Page 266
Antipattern: Make SQL a Second-Class Citizen......Page 267
How to Recognize the Antipattern......Page 268
Solution: Establish a Big-Tent Culture of Quality......Page 269
Magic Beans......Page 278
Objective: Simplify Models in MVC......Page 279
Antipattern: The Model Is an Active Record......Page 280
How to Recognize the Antipattern......Page 286
Solution: The Model Has an Active Record......Page 287
Appendixes......Page 293
What Does Relational Mean?......Page 294
Myths About Normalization......Page 296
What Is Normalization?......Page 298
Common Sense......Page 308
Bibliography......Page 309
A......Page 311
C......Page 312
D......Page 314
E......Page 315
F......Page 316
I......Page 317
M......Page 318
N......Page 319
P......Page 320
R......Page 322
S......Page 324
T......Page 325
V......Page 326
Z......Page 327
Recommend Papers

SQL Antipatterns: Avoiding the Pitfalls of Database Programming [1 ed.]
 9781934356555, 1934356557

  • 0 0 0
  • Like this paper and download? You can publish your own PDF file online for free in a few minutes! Sign Up
File loading please wait...
Citation preview

What Readers Are Saying About SQL Antipatterns

I am a strong advocate of best practices. I prefer to learn from other people’s mistakes. This book is a comprehensive collection of those other people’s mistakes and, quite surprisingly, some of my own. I wish I had read this book sooner. Marcus Adams Senior Software Engineer Bill has written an engaging, useful, important, and unique book. Software developers will certainly benefit from reading the antipatterns and solutions described here. I immediately applied techniques from this book and improved my applications. Fantastic work! Frederic Daoud Author of Stripes: ...And Java Web Development Is Fun Again and Getting Started with Apache Click SQL Antipatterns is a must-read for software developers, who will frequently encounter the database design choices presented in this book. It helps development teams to understand the consequences of their database designs and to make the best decisions possible based on requirements, expectations, measurements, and reality. Darby Felton Cofounder, DevBots Software Development I really like how Bill has approached this book; it shows his unique style and sense of humor. Those things are really important when discussing potentially dry topics. Bill has succeeded in making the teachings accessible for developers in a good descriptive form, as well as being easy to reference later. In short, this is an excellent new resource for your pragmatic bookshelf! Arjen Lentz Executive Director of Open Query (http://openquery.com); Coauthor of High Performance MySQL, Second Edition

This book is obviously the product of many years of practical experience with SQL databases. Each topic is covered in great depth, and the attention to detail in the book was beyond my expectations. Although it’s not a beginner’s book, any developer with a reasonable amount of SQL experience should find it to be a valuable reference and would be hard-pressed not to learn something new. Mike Naberezny Partner at Maintainable Software; Coauthor of Rails for PHP Developers This is an excellent book for the software engineer who knows basic SQL but finds herself needing to design SQL databases for projects that go a little beyond the basics. Liz Neely Senior Database Programmer Karwin’s book is full of good and practical advice, and it was published at the right time. While many people are focusing on the new and seemingly fancy stuff, professionals now have the chance and the perfect book to sharpen their SQL knowledge. Maik Schmidt Author of Enterprise Recipes with Ruby and Rails and Enterprise Integration with Ruby Bill has captured the essence of a slew of traps that we’ve probably all dug for ourselves at one point or another when working with SQL — without even realizing we’re in trouble. Bill’s antipatterns range from “I can’t believe I did that (again!)” hindsight gotchas to tricky scenarios where the best solution may run counter to the SQL dogma you grew up with. A good read for SQL diehards, novices, and everyone in between. Danny Thorpe Microsoft Principal Engineer; Author of Delphi Component Design

SQL Antipatterns Avoiding the Pitfalls of Database Programming Bill Karwin

The Pragmatic Bookshelf Raleigh, North Carolina Dallas, Texas

Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf and the linking g device are trademarks of The Pragmatic Programmers, LLC. Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein. Our Pragmatic courses, workshops, and other products can help you and your team create better software and have more fun. For more information, as well as the latest Pragmatic titles, please visit us at http://www.pragprog.com

Copyright © 2010 Bill Karwin. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher. Printed in the United States of America. ISBN-10: 1-934356-55-7 ISBN-13: 978-1-934356-55-5 Printed on acid-free paper. P1.0 printing, May 2010 Version: 2010-6-9

Contents 1

Introduction 1.1 Who This Book Is For . . 1.2 What’s in This Book . . 1.3 What’s Not in This Book 1.4 Conventions . . . . . . . 1.5 Example Database . . . 1.6 Acknowledgments . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

13 14 15 17 18 19 22

I

Logical Database Design Antipatterns

2

Jaywalking 2.1 Objective: Store Multivalue Attributes . . . . 2.2 Antipattern: Format Comma-Separated Lists 2.3 How to Recognize the Antipattern . . . . . . 2.4 Legitimate Uses of the Antipattern . . . . . . 2.5 Solution: Create an Intersection Table . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

25 26 26 29 30 30

Naive 3.1 3.2 3.3 3.4 3.5

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

34 35 35 39 40 41

. . . . .

54 55 57 61 61 62

3

4

Trees Objective: Store and Query Hierarchies . . . Antipattern: Always Depend on One’s Parent How to Recognize the Antipattern . . . . . . Legitimate Uses of the Antipattern . . . . . . Solution: Use Alternative Tree Models . . . .

24

ID Required 4.1 Objective: Establish Primary Key Conventions 4.2 Antipattern: One Size Fits All . . . . . . . . . . 4.3 How to Recognize the Antipattern . . . . . . . 4.4 Legitimate Uses of the Antipattern . . . . . . . 4.5 Solution: Tailored to Fit . . . . . . . . . . . . .

. . . . .

. . . . .

. . . . .

. . . . .

CONTENTS

5

6

7

8

9

Keyless Entry 5.1 Objective: Simplify Database Architecture . 5.2 Antipattern: Leave Out the Constraints . . 5.3 How to Recognize the Antipattern . . . . . 5.4 Legitimate Uses of the Antipattern . . . . . 5.5 Solution: Declare Constraints . . . . . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

65 66 66 69 70 70

Entity-Attribute-Value 6.1 Objective: Support Variable Attributes . . . 6.2 Antipattern: Use a Generic Attribute Table 6.3 How to Recognize the Antipattern . . . . . 6.4 Legitimate Uses of the Antipattern . . . . . 6.5 Solution: Model the Subtypes . . . . . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

73 73 74 80 80 82

Polymorphic Associations 7.1 Objective: Reference Multiple Parents . . . . 7.2 Antipattern: Use Dual-Purpose Foreign Key . 7.3 How to Recognize the Antipattern . . . . . . 7.4 Legitimate Uses of the Antipattern . . . . . . 7.5 Solution: Simplify the Relationship . . . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

89 90 91 94 95 96

Multicolumn Attributes 8.1 Objective: Store Multivalue Attributes 8.2 Antipattern: Create Multiple Columns 8.3 How to Recognize the Antipattern . . 8.4 Legitimate Uses of the Antipattern . . 8.5 Solution: Create Dependent Table . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

102 102 103 106 107 108

Metadata Tribbles 9.1 Objective: Support Scalability . . . . . 9.2 Antipattern: Clone Tables or Columns 9.3 How to Recognize the Antipattern . . 9.4 Legitimate Uses of the Antipattern . . 9.5 Solution: Partition and Normalize . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

110 111 111 116 117 118

Report erratum this copy is (P1.0 printing, May 2010)

8

CONTENTS

II Physical Database Design Antipatterns

122

10 Rounding Errors 123 10.1 Objective: Use Fractional Numbers Instead of Integers 124 10.2 Antipattern: Use FLOAT Data Type . . . . . . . . . . . . 124 10.3 How to Recognize the Antipattern . . . . . . . . . . . . 128 10.4 Legitimate Uses of the Antipattern . . . . . . . . . . . . 128 10.5 Solution: Use NUMERIC Data Type . . . . . . . . . . . . 128 11 31 Flavors 11.1 Objective: Restrict a Column to Specific Values . . . 11.2 Antipattern: Specify Values in the Column Definition 11.3 How to Recognize the Antipattern . . . . . . . . . . . 11.4 Legitimate Uses of the Antipattern . . . . . . . . . . . 11.5 Solution: Specify Values in Data . . . . . . . . . . . .

. . . . .

131 131 132 135 136 136

12 Phantom Files 12.1 Objective: Store Images or Other Bulky Media 12.2 Antipattern: Assume You Must Use Files . . . 12.3 How to Recognize the Antipattern . . . . . . . 12.4 Legitimate Uses of the Antipattern . . . . . . . 12.5 Solution: Use BLOB Data Types As Needed . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

139 140 140 143 144 145

13 Index Shotgun 13.1 Objective: Optimize Performance . . . . . . 13.2 Antipattern: Using Indexes Without a Plan 13.3 How to Recognize the Antipattern . . . . . 13.4 Legitimate Uses of the Antipattern . . . . . 13.5 Solution: MENTOR Your Indexes . . . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

148 149 149 153 154 154

III Query Antipatterns 14 Fear 14.1 14.2 14.3 14.4 14.5

. . . . .

. . . . .

161

of the Unknown 162 Objective: Distinguish Missing Values . . . . . . . . . . 163 Antipattern: Use Null as an Ordinary Value, or Vice Versa 163 How to Recognize the Antipattern . . . . . . . . . . . . 166 Legitimate Uses of the Antipattern . . . . . . . . . . . . 168 Solution: Use Null as a Unique Value . . . . . . . . . . 168

Report erratum this copy is (P1.0 printing, May 2010)

9

CONTENTS

15 Ambiguous Groups 15.1 Objective: Get Row with Greatest Value per Group 15.2 Antipattern: Reference Nongrouped Columns . . . 15.3 How to Recognize the Antipattern . . . . . . . . . 15.4 Legitimate Uses of the Antipattern . . . . . . . . . 15.5 Solution: Use Columns Unambiguously . . . . . .

. . . . .

. . . . .

. . . . .

173 174 174 176 178 179

16 Random Selection 16.1 Objective: Fetch a Sample Row . . 16.2 Antipattern: Sort Data Randomly . 16.3 How to Recognize the Antipattern 16.4 Legitimate Uses of the Antipattern 16.5 Solution: In No Particular Order. . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

183 184 184 185 186 186

Man’s Search Engine Objective: Full-Text Search . . . . . . . . Antipattern: Pattern Matching Predicates How to Recognize the Antipattern . . . . Legitimate Uses of the Antipattern . . . . Solution: Use the Right Tool for the Job .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

190 191 191 192 193 193

18 Spaghetti Query 18.1 Objective: Decrease SQL Queries . . . 18.2 Antipattern: Solve a Complex Problem 18.3 How to Recognize the Antipattern . . 18.4 Legitimate Uses of the Antipattern . . 18.5 Solution: Divide and Conquer . . . . .

. . . . . . . . in One Step . . . . . . . . . . . . . . . . . . . . . . . .

. . . . .

. . . . .

204 205 205 207 208 209

19 Implicit Columns 19.1 Objective: Reduce Typing . . . . . . . 19.2 Antipattern: a Shortcut That Gets You 19.3 How to Recognize the Antipattern . . 19.4 Legitimate Uses of the Antipattern . . 19.5 Solution: Name Columns Explicitly . .

. . . Lost . . . . . . . . .

. . . . .

. . . . .

214 215 215 217 218 219

17 Poor 17.1 17.2 17.3 17.4 17.5

. . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

Report erratum this copy is (P1.0 printing, May 2010)

10

CONTENTS

IV Application Development Antipatterns

221

20 Readable Passwords 20.1 Objective: Recover or Reset Passwords . . . . . 20.2 Antipattern: Store Password in Plain Text . . . 20.3 How to Recognize the Antipattern . . . . . . . 20.4 Legitimate Uses of the Antipattern . . . . . . . 20.5 Solution: Store a Salted Hash of the Password

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

222 222 223 225 225 227

21 SQL Injection 21.1 Objective: Write Dynamic SQL Queries . . . . 21.2 Antipattern: Execute Unverified Input As Code 21.3 How to Recognize the Antipattern . . . . . . . 21.4 Legitimate Uses of the Antipattern . . . . . . . 21.5 Solution: Trust No One . . . . . . . . . . . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

234 235 235 242 243 243

. . . . .

250 251 251 254 254 254

. . . . .

259 260 260 262 263 264

. . . . .

266 267 267 268 269 269

. . . . .

278 279 280 286 287 287

22 Pseudokey Neat-Freak 22.1 Objective: Tidy Up the Data . . . . 22.2 Antipattern: Filling in the Corners 22.3 How to Recognize the Antipattern 22.4 Legitimate Uses of the Antipattern 22.5 Solution: Get Over It . . . . . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

23 See No Evil 23.1 Objective: Write Less Code . . . . . . . . . . 23.2 Antipattern: Making Bricks Without Straw 23.3 How to Recognize the Antipattern . . . . . 23.4 Legitimate Uses of the Antipattern . . . . . 23.5 Solution: Recover from Errors Gracefully .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

24 Diplomatic Immunity 24.1 Objective: Employ Best Practices . . . . . . . . . . 24.2 Antipattern: Make SQL a Second-Class Citizen . . 24.3 How to Recognize the Antipattern . . . . . . . . . 24.4 Legitimate Uses of the Antipattern . . . . . . . . . 24.5 Solution: Establish a Big-Tent Culture of Quality 25 Magic Beans 25.1 Objective: Simplify Models in MVC . . . . . 25.2 Antipattern: The Model Is an Active Record 25.3 How to Recognize the Antipattern . . . . . 25.4 Legitimate Uses of the Antipattern . . . . . 25.5 Solution: The Model Has an Active Record

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

Report erratum this copy is (P1.0 printing, May 2010)

11

CONTENTS

V Appendixes

293

A

294 294 296 298 308

B

Rules A.1 A.2 A.3 A.4

of Normalization What Does Relational Mean? Myths About Normalization . What Is Normalization? . . . Common Sense . . . . . . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

Bibliography

309

Index

311

Report erratum this copy is (P1.0 printing, May 2010)

12

An expert is a person who has made all the mistakes that can be made in a very narrow field. Niels Bohr

Chapter 1

Introduction I turned down my first SQL job. Shortly after I finished my college degree in computer and information science at the University of California, I was approached by a manager who worked at the university and knew me through campus activities. He had his own software startup company on the side that was developing a database management system portable between various UNIX platforms using shell scripts and related tools such as awk (at this time, modern dynamic languages like Ruby, Python, PHP, and even Perl weren’t popular yet). The manager approached me because he needed a programmer to write the code to recognize and execute a limited version of the SQL language. He said, “I don’t need to support the full language—that would be too much work. I need only one SQL statement: SELECT.” I hadn’t been taught SQL in school. Databases weren’t as ubiquitous as they are today, and open source brands like MySQL and PostgreSQL didn’t exist yet. But I had developed complete applications in shell, and I knew something about parsers, having done projects in classes like compiler design and computational linguistics. So, I thought about taking the job. How hard could it be to parse a single statement of a specialized language like SQL? I found a reference for SQL and noticed immediately that this was a different sort of language from those that support statements like if( ) and while( ), variable assignments and expressions, and perhaps functions. To call SELECT only one statement in that language is like calling an engine only one part of an automobile. Both sentences are literally true, but they certainly belie the complexity and depth of their subjects. To support execution of that single SQL statement, I realized I would

W HO T HIS B OOK I S F OR

have to develop all the code for a fully functional relational database management system and query engine. I declined this opportunity to code an SQL parser and RDBMS engine in shell script. The manager underrepresented the scope of his project, perhaps because he didn’t understand what an RDBMS does. My early experience with SQL seems to be a common one for software developers, even those who have a college degree in computer science. Most people are self-taught in SQL, learning it out of self-defense when they find themselves working on a project that requires it, instead of studying it explicitly as they would most programming languages. Regardless of whether the person is a hobbyist or a professional programmer or an accomplished researcher with a PhD, SQL seems to be a software skill that programmers learn without training. Once I learned something about SQL, I was surprised how different it is from procedural programming languages such as C, Pascal, and shell, or object-oriented languages like C++, Java, Ruby, or Python. SQL is a declarative programming language like LISP, Haskell, or XSLT. SQL uses sets as a fundamental data structure, while object-oriented languages use objects. Traditionally trained software developers are turned off by this so-called impedance mismatch, so many programmers are drawn to object-oriented libraries to avoid learning how to use SQL effectively. Since 1992, I’ve worked with SQL a lot. I’ve used it when developing applications, I’ve provided technical support and developed training and documentation for the InterBase RDBMS product, and I’ve developed libraries for SQL programming in Perl and PHP. I’ve answered thousands of questions on Internet mailing lists and newsgroups. I see a lot of repeat business—frequently asked questions that show that software developers make the same mistakes over and over again.

1.1 Who This Book Is For I’m writing SQL Antipatterns for software developers who need to use SQL so I can help you use the language more effectively. It doesn’t matter whether you’re a beginner or a seasoned professional. I’ve talked to people of all levels of experience who would benefit from the subjects in this book.

Report erratum this copy is (P1.0 printing, May 2010)

14

W HAT ’ S IN T HIS B OOK

You may have read a reference on SQL syntax. Now you know all the clauses of a SELECT statement, and you can get some work done. Gradually, you may increase your SQL skills by inspecting other applications and reading articles. But how can you tell good examples from bad examples? How can you be sure you’re learning best practices, instead of yet another way to paint yourself into a corner? You may find some topics in SQL Antipatterns that are well-known to you. You’ll see new ways of looking at the problems, even if you’re already aware of the solutions. It’s good to confirm and reinforce your good practices by reviewing widespread programmer misconceptions. Other topics may be new to you. I hope you can improve your SQL programming habits by reading them. If you are a trained database administrator, you may already know the best ways to avoid the SQL pitfalls described in this book. This book can help you by introducing you to the perspective of software developers. It’s not uncommon for the relationship between developers and DBAs to be contentious, but mutual respect and teamwork can help us to work together more effectively. Use SQL Antipatterns to help explain good practices to the software developers you work with and the consequences of straying from that path.

1.2 What’s in This Book What is an antipattern? An antipattern is a technique that is intended to solve a problem but that often leads to other problems. An antipattern is practiced widely in different ways, but with a thread of commonality. People may come up with an idea that fits an antipattern independently or with help from a colleague, a book, or an article. Many antipatterns of object-oriented software design and project management are documented at the Portland Pattern Repository,1 as well as in the 1998 book AntiPatterns [BMMM98] by William J. Brown et al. SQL Antipatterns describes the most frequently made missteps I’ve seen people naively make while using SQL as I’ve talked to them in technical support and training sessions, worked alongside them developing software, and answered their questions on Internet forums. Many of these blunders I’ve made myself; there’s no better teacher than spending many hours late at night making up for one’s own errors. 1.

Portland Pattern Repository: http://c2.com/cgi-bin/wiki?AntiPattern

Report erratum this copy is (P1.0 printing, May 2010)

15

W HAT ’ S IN T HIS B OOK

Parts of This Book This book has four parts for the following categories of antipatterns: Logical Database Design Antipatterns Before you start coding, you should decide what information you need to keep in your database and the best way to organize and interconnect your data. This includes planning your database tables, columns, and relationships. Physical Database Design Antipatterns After you know what data you need to store, you implement the data management as efficiently as you can using the features of your RDBMS technology. This includes defining tables and indexes and choosing data types. You use SQL’s data definition language—statements such as CREATE TABLE. Query Antipatterns You need to add data to your database and then retrieve data. SQL queries are made with data manipulation language—statements such as SELECT, UPDATE, and DELETE. Application Development Antipatterns SQL is supposed to be used in the context of applications written in another language, such as C++, Java, PHP, Python, or Ruby. There are right ways and wrong ways to employ SQL in an application, and this part of the book describes some common blunders. Many of the antipattern chapters have humorous or evocative titles, such as Golden Hammer, Reinventing the Wheel, or Design by Committee. It’s traditional to give both positive design patterns and antipatterns names that serve as a metaphor or mnemonic. The appendix provides practical descriptions of some relational database theory. Many of the antipatterns this book covers are the result of misunderstanding database theory.

Anatomy of an Antipattern Each antipattern chapter contains the following subheadings: Objective This is the task that you may be trying to solve. Antipatterns are used with an intention to provide that solution but end up causing more problems than they solve.

Report erratum this copy is (P1.0 printing, May 2010)

16

W HAT ’ S N OT IN T HIS B OOK

The Antipattern This section describes the nature of the common solution and illustrates the unforeseen consequences that make it an antipattern. How to Recognize the Antipattern There may be certain clues that help you identify when an antipattern is being used in your project. Certain types of barriers you encounter, or quotes you may hear yourself or others saying, can tip you off to the presence of an antipattern. Legitimate Uses of the Antipattern Rules usually have exceptions. There may be circumstances in which an approach normally considered an antipattern is nevertheless appropriate, or at least the lesser of all evils. Solution This section describes the preferred solutions, which solve the original objective without running into the problems caused by the antipattern.

1.3 What’s Not in This Book I’m not going to give lessons on SQL syntax or terminology. There are plenty of books and Internet references for the basics. I assume you have already learned enough SQL syntax to use the language and get some work done. Performance, scalability, and optimization are important for many people who develop database-driven applications, especially on the Web. There are books specifically about performance issues related to database programming. I recommend SQL Performance Tuning [GP03] and High Performance MySQL, Second Edition [SZT+ 08]. Some of the topics in SQL Antipatterns are relevant to performance, but it’s not the main focus of the book. I try to present issues that apply to all database brands and also solutions that should work with all brands. The SQL language is specified as an ANSI and ISO standard. All brands of databases support these standards, so I describe vendor-neutral use of SQL whenever possible, and I try to be clear when describing vendor extensions to SQL. Data access frameworks and object-relational mapping libraries are helpful tools, but these aren’t the focus of this book. I’ve written most Report erratum this copy is (P1.0 printing, May 2010)

17

C ONVENTIONS

code examples in PHP, in the plainest way I can. The examples are simple enough that they’re equally relevant to most programming languages. Database administration and operation tasks such as server sizing, installation and configuration, monitoring, backups, log analysis, and security are important and deserve a book of their own, but I’m targeting this book to developers using the SQL language more than database administrators. This book is about SQL and relational databases, not alternative technology such as object-oriented databases, key/value stores, columnoriented databases, document-oriented databases, hierarchical databases, network databases, map/reduce frameworks, or semantic data stores. Comparing the strengths and weaknesses and appropriate uses of these alternative solutions for data management would be interesting but is a matter for other books.

1.4 Conventions The following sections describe some conventions I use in this book.

Typography SQL keywords are formatted in all-capitals and in a monospaced font to make them stand out from the text, as in SELECT. SQL tables, also in a monospaced font, are spelled with a capital for the initial letter of each word in the table name, as in Accounts or BugsProducts. SQL columns, also in a monospaced font, are spelled in lowercase, and words are separated by underscores, as in account_name. Literal strings are formatted in italics, as in [email protected].

Terminology SQL is correctly pronounced “ess-cue-ell,” not “see-quell.” Though I have no objection to the latter being used colloquially, I try to use the former, so in this book you will read phrases like “an SQL query,” not “a SQL query.” In the context of database-related usage, the word index refers to an ordered collection of information. The preferred plural of this word is

Report erratum this copy is (P1.0 printing, May 2010)

18

E XAMPLE D ATABASE

indexes. In other contexts, an index may mean an indicator and is typically pluralized as indices. Both are correct according to most dictionaries, and this causes some confusion among writers. In this book, I spell the plural as indexes. In SQL, the terms query and statement are somewhat interchangeable, being any complete SQL command that you can execute. For the sake of clarity, I use query to refer to SELECT statements and statement for all others, including INSERT, UPDATE, and DELETE statements, as well as data definition statements.

Entity-Relationship Diagrams The most common way to diagram relational databases is with entityrelationship diagrams. Tables are shown as boxes, and relationships are shown as lines connecting the boxes, with symbols at either end of the lines describing the cardinality of the relationship. For examples, see Figure 1.1, on the following page.

1.5 Example Database I illustrate most of the topics in SQL Antipatterns using a database for a hypothetical bug-tracking application. The entity-relationship diagram for this database is shown in Figure 1.2, on page 21. Notice the three connections between the Bugs table and the Accounts table, representing three separate foreign keys. The following data definition language shows how I define the tables. In some cases, choices are made for the sake of examples later in the book, so they might not always be the choices one would make in a real-world application. I try to use only standard SQL so the example is applicable to any brand of database, but some MySQL data types also appear, such as SERIAL and BIGINT. Download Introduction/setup.sql

CREATE TABLE Accounts ( account_id SERIAL PRIMARY KEY, account_name VARCHAR(20), first_name VARCHAR(20), last_name VARCHAR(20), email VARCHAR(100), password_hash CHAR(64), portrait_image BLOB, hourly_rate NUMERIC(9,2) );

Report erratum this copy is (P1.0 printing, May 2010)

19

E XAMPLE D ATABASE

!"#$%&'%(#) *"+,-"++'.#&-/"$-0'1-/"#$-2.13

+$,'

!""#$%&' (#)%&'%!"#$

*"+,-2.1-/"$-,"4)-/"#$-+'//)#&3

+$,'

(#))*%&' (#)%&'%(#) *"+,-56'7.+&-,"3-'#)-8#3&"00)6

10#2$"&'

-%'&.//*0'

!"#$%&'%!"#$ *"+,-56'7.+&-/"$-,"4)-/"#$-2.139 "-2.1-/"$-5)6&"8#-&'-/"#$-56'7.+&3

+$,'

10#2$"&'

!"#$%&'%!"#$ :"/)-"3-"2'4);-