SQL Antipatterns. Extracted from: Avoiding the Pitfalls of Database Programming. The Pragmatic Bookshelf

Similar documents
Programming Clojure. Extracted from: Second Edition. The Pragmatic Bookshelf

Distributed and Parallel Computing with Ruby

Pragmatic Guide to Sass

Java By Comparison. Extracted from: Become a Java Craftsman in 70 Examples. The Pragmatic Bookshelf

Build Database Apps in Elixir for Scalability and Performance

Practical Programming, Third Edition

Agile Web Development with Rails 5

Developing Android on Android

Java by Comparison. Extracted from: Become a Java Craftsman in 70 Examples. The Pragmatic Bookshelf

Learn Functional Programming with Elixir

Automate with Grunt. Extracted from: The Build Tool for JavaScript. The Pragmatic Bookshelf

Complex Network Analysis in Python

Practical Vim, Second Edition

Node.js 8 the Right Way

Practical Programming, 2nd Edition

iphone SDK Development

Reactive Programming with RxJS 5

Node.js the Right Way

Pragmatic Guide to Git

Dart for Hipsters. Extracted from: The Pragmatic Bookshelf

Build ios Games with Sprite Kit

Agile Web Development with Rails 5.1

Agile Web Development with Rails 5

Reactive Programming with RxJS

Copyright 2009 The Pragmatic Programmers, LLC.

Practical Vim, Second Edition

Deploying with JRuby 9k

Cocoa Programming A Quick-Start Guide for Developers

Build Safe and Maintainable Front-End Applications

Web Design for Developers A Programmer s Guide to Design Tools and Techniques

Modern Vim. Extracted from: Craft Your Development Environment with Vim 8 and Neovim. The Pragmatic Bookshelf

Beginning Mac Programming

Build Reactive Websites with RxJS

Programming Google Glass, Second Edition

Pragmatic Guide to Sass 3

ios 8 SDK Development

Programming Clojure, Third Edition

Effective Testing with RSpec 3

The SQL Guide to Pervasive PSQL. Rick F. van der Lans

Web Design for Developers A Programmer s Guide to Design Tools and Techniques

ios 9 SDK Development

Design It! Extracted from: From Programmer to Software Architect. The Pragmatic Bookshelf

SQL Antipatterns Tables and Queries That Don t Work. Bill Karwin

Python Companion to Data Science

Programming Kotlin. Extracted from: Creating Elegant, Expressive, and Performant JVM and Android Applications. The Pragmatic Bookshelf

Complex Network Analysis in Python

ios 8 SDK Development

Real World Kanban Do Less Accomplish More With Lean Thinking

Mastering Clojure Macros

Docker for Rails Developers

The ThoughtWorks Anthology 2

HA150. SAP HANA 2.0 SPS03 - SQL and SQLScript for SAP HANA COURSE OUTLINE. Course Version: 15 Course Duration:

Practical object-oriented models in SQL. Bill Karwin July 22, OSCON

Release It! Second Edition

SQL. Draft Version. Head First. A Brain-Friendly Guide. Lynn Beighley. A learner s companion to database programming using SQL

Modern Systems Analysis and Design

Things You Should Know

Functional Programming in Java

Copyright 2006The Pragmatic Programmers, LLC.

SAS Universal Viewer 1.3

Getting MEAN. with Mongo, Express, Angular, and Node SIMON HOLMES MANNING SHELTER ISLAND

An Introduction to Programming with IDL

HA150 SQL Basics for SAP HANA

object/relational persistence What is persistence? 5

Scenario Manager User Guide. Release September 2013

Creating Column Profiles on LDAP Data Objects

Ebook Niche Explorer User Manual

SAS Business Rules Manager 1.2

Chapter 1 Readme.doc definitions you need to know 1

"Charting the Course... MOC C: Querying Data with Transact-SQL. Course Summary

imagerunner 2545i/ i/ / Remote UI Guide

SYSTEM 2000 Essentials

HA150. SAP HANA 2.0 SPS02 - SQL and SQLScript for SAP HANA COURSE OUTLINE. Course Version: 14 Course Duration: 3 Day(s)

SAS File Management. Improving Performance CHAPTER 37

SAS Data Integration Studio 3.3. User s Guide

Using the SQL Editor. Overview CHAPTER 11

DATABASE MANAGEMENT SYSTEMS

Manually Defining Constraints in Enterprise Data Manager

Informatica PowerExchange for Tableau User Guide

Fundamentals, Design, and Implementation, 9/e Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M.

Everyday Scripting With Ruby: For Teams, Testers, And You By Brian Marick READ ONLINE

UtilityPak: Dynamics CRM Record Merge. Version 1.0

DBLOAD Procedure Reference

Rooms Management Manual

User Manual. SmartLite WebQuiz SQL Edition

Eessaar, E. "On Query-based Search of Possible Design Flaws of SQL Databases" Introduction Queries that are used to detect design flaws...

Textadept Quick Reference. Mitchell

Oracle Financial Services Regulatory Reporting Administration Guide PK STR. Release 2.4 January 2014

List Building Starter Course. Lesson 2. Writing Your Campaign. Sean Mize

XQ: An XML Query Language Language Reference Manual

Oracle PL/SQL. DUMmIES. by Michael Rosenblum and Dr. Paul Dorsey FOR

20461: Querying Microsoft SQL Server 2014 Databases

Querying Microsoft SQL Server

Migrating Mappings and Mapplets from a PowerCenter Repository to a Model Repository

ORACLE DATABASE 12C INTRODUCTION

SAS IT Resource Management 3.3

Inside Relational Databases with Examples in Access

The Unicode Standard Version 11.0 Core Specification

COMP Instructor: Dimitris Papadias WWW page:

Oracle SQL Internals Handbook. Donald K. Burleson Joe Celko Dave Ensor Jonathan Lewis Dave Moore Vadim Tropashko John Weeg

Transcription:

Extracted from: SQL Antipatterns Avoiding the Pitfalls of Database Programming This PDF file contains pages extracted from SQL Antipatterns, published by the Pragmatic Bookshelf. For more information or to purchase a paperback or PDF copy, please visit http://www.pragprog.com. Note: This extract contains some colored text (particularly in code listing). This is available only in online versions of the books. The printed versions are black and white. Pagination might vary between the online and printer versions; the content is otherwise identical. Copyright 2010 The Pragmatic Programmers, LLC. 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. The Pragmatic Bookshelf Dallas, Texas Raleigh, North Carolina

SQL Antipatterns Avoiding the Pitfalls of Database Programming Bill Karwin The Pragmatic Bookshelf Dallas, Texas Raleigh, North Carolina

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, PragProg 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. 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-13: 978-1-934356-55-5 Encoded using the finest acid-free high-entropy binary digits. Book version: P3.0 March 2012

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? Part 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? 3. Naive Trees.............? 3.1 Objective: Store and Query Hierarchies? 3.2 Antipattern: Always Depend on One s Parent? 3.3 How to Recognize the Antipattern? 3.4 Legitimate Uses of the Antipattern? 3.5 Solution: Use Alternative Tree Models? 4. 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 v 5. 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? 6. 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? 7. 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? 8. 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? 9. 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? Part II Physical Database Design Antipatterns 10. Rounding Errors............? 10.1 Objective: Use Fractional Numbers Instead of Integers? 10.2 Antipattern: Use FLOAT Data Type? 10.3 How to Recognize the Antipattern?

Contents vi 10.4 Legitimate Uses of the Antipattern? 10.5 Solution: Use NUMERIC Data Type? 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? 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? 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? Part III Query Antipatterns 14. Fear of the Unknown...........? 14.1 Objective: Distinguish Missing Values? 14.2 Antipattern: Use Null as an Ordinary Value, or Vice Versa? 14.3 How to Recognize the Antipattern? 14.4 Legitimate Uses of the Antipattern? 14.5 Solution: Use Null as a Unique Value? 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?

Contents vii 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? 17. Poor Man s Search Engine.........? 17.1 Objective: Full-Text Search? 17.2 Antipattern: Pattern Matching Predicates? 17.3 How to Recognize the Antipattern? 17.4 Legitimate Uses of the Antipattern? 17.5 Solution: Use the Right Tool for the Job? 18. Spaghetti Query............? 18.1 Objective: Decrease SQL Queries? 18.2 Antipattern: Solve a Complex Problem in One Step? 18.3 How to Recognize the Antipattern? 18.4 Legitimate Uses of the Antipattern? 18.5 Solution: Divide and Conquer? 19. Implicit Columns............? 19.1 Objective: Reduce Typing? 19.2 Antipattern: a Shortcut That Gets You Lost? 19.3 How to Recognize the Antipattern? 19.4 Legitimate Uses of the Antipattern? 19.5 Solution: Name Columns Explicitly? Part IV Application Development Antipatterns 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? 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?

viii Contents 21.4 Legitimate Uses of the Antipattern? 21.5 Solution: Trust No One? 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? Part V Appendixes A1. Rules of Normalization..........? A1.1 What Does Relational Mean?? A1.2 Myths About Normalization? A1.3 What Is Normalization?? A1.4 Common Sense? A2. Bibliography.............? Index...............?