SQL Antipatterns
Avoiding the Pitfalls of Database Programming
by Bill Karwin
Version: P4.0 (August 2014)
Copyright 2010 Bill Karwin. This book is licensed tothe individual who purchased it. We don't copy-protect itbecause that would limit your ability to use it for yourown purposes. Please don't break this trustyou can use this across all of your devices but please do not share this copywith other members of your team, with friends, or via file sharing services. Thanks.
Dave & Andy.
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://pragprog.com.
For the Best Reading Experience...
We strongly recommend that you read this book with the publisher defaults setting enabled for your reading device or application. Certain formats and characters may not display correctly without this setting. Please refer to the instructions for your reader on how to enable the publisher defaults setting.
Table of Contents
Copyright 2014, The Pragmatic Bookshelf.
What Readers Are Saying About SQL Antipatterns
I am a strong advocate of best practices. I prefer to learn from other peoples mistakes. This book is a comprehensive collection of those other peoples 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 its not a beginners 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 |
Karwins 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 weve probably all dug for ourselves at one point or another when working with SQL without even realizing were in trouble. Bills antipatterns range from I cant 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 |
Chapter 1
Introduction
An expert is a person who has made all the mistakes that can be made ina very narrow field.
Niels Bohr
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 werent 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 dont need to support the full languagethat would be too much work. I need only one SQL statement: SELECT .
I hadnt been taught SQL in school. Databases werent as ubiquitous as they are today, and open source brands like MySQL and PostgreSQL didnt 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 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 didnt understand what an RDBMS does.