You are previewing Joe Celko's SQL Programming Style.
O'Reilly logo
Joe Celko's SQL Programming Style

Book Description

Are you an SQL programmer that, like many, came to SQL after learning and writing procedural or object-oriented code? Or have switched jobs to where a different brand of SQL is being used, or maybe even been told to learn SQL yourself?

If even one answer is yes, then you need this book. A "Manual of Style" for the SQL programmer, this book is a collection of heuristics and rules, tips, and tricks that will help you improve SQL programming style and proficiency, and for formatting and writing portable, readable, maintainable SQL code. Based on many years of experience consulting in SQL shops, and gathering questions and resolving his students’ SQL style issues, Joe Celko can help you become an even better SQL programmer.

+ Help you write Standard SQL without an accent or a dialect that is used in another programming language or a specific flavor of SQL, code that can be maintained and used by other people.
+ Enable you to give your group a coding standard for internal use, to enable programmers to use a consistent style.
+ Give you the mental tools to approach a new problem with SQL as your tool, rather than another programming language — one that someone else might not know!

Table of Contents

  1. Cover
  2. Title Page
  3. Copyright
  4. Dedication
  5. Table of Contents
  6. Introduction
    1. 1.1 Purpose of the Book
    2. 1.2 Acknowledgments
    3. 1.3 Corrections, Comments, and Future Editions
  7. Chapter 1: Names and Data Elements
    1. 1.1 Names
    2. 1.2 Follow the ISO-11179 Standards Naming Conventions
    3. 1.3 Problems in Naming Data Elements
  8. Chapter 2: Fonts, Punctuation, and Spacing
    1. 2.1 Typography and Code
    2. 2.2 Word Spacing
    3. 2.3 Follow Normal Punctuation Rules
    4. 2.4 Use Full Reserved Words
    5. 2.5 Avoid Proprietary Reserved Words if a Standard Keyword Is Available in Your SQL Product
    6. 2.6 Avoid Proprietary Statements if a Standard Statement Is Available
    7. 2.7 Rivers and Vertical Spacing
    8. 2.8 Indentation
    9. 2.9 Use Line Spacing to Group Statements
  9. Chapter 3: Data Declaration Language
    1. 3.1 Put the Default in the Right Place
    2. 3.2 The Default Value Should Be the Same Data Type as the Column
    3. 3.3 Do Not Use Proprietary Data Types
    4. 3.4 Place the PRIMARY KEY Declaration at the Start of the CREATE TABLE Statement
    5. 3.5 Order the Columns in a Logical Sequence and Cluster Them in Logical Groups
    6. 3.6 Indent Referential Constraints and Actions under the Data Type
    7. 3.7 Give Constraints Names in the Production Code
    8. 3.8 Put CHECK() Constraint Near what they Check
    9. 3.9 Put Multiple Column Constraints as Near to Both Columns as Possible
    10. 3.10 Put Table-Level CHECK() Constraints at the End of the Table Declaration
    11. 3.11 Use CREATE ASSERTION for Multi-table Constraints
    12. 3.12 Keep CHECK() Constraints Single Purposed
    13. 3.13 Every Table Must Have a Key to Be a Table
    14. 3.14 Do Not Split Attributes
    15. 3.15 Do Not Use Object-Oriented Design for an RDBMS
  10. Chapter 4: Scales and Measurements
    1. 4.1 Measurement Theory
    2. 4.2 Types of Scales
    3. 4.3 Using Scales
    4. 4.4 Scale Conversion
    5. 4.5 Derived Units
    6. 4.6 Punctuation and Standard Units
    7. 4.7 General Guidelines for Using Scales in a Database
  11. Chapter 5: Data Encoding Schemes
    1. 5.1 Bad Encoding Schemes
    2. 5.2 Encoding Scheme Types
    3. 5.3 General Guidelines for Designing Encoding Schemes
    4. 5.4 Multiple Character Sets
  12. Chapter 6: Coding Choices
    1. 6.1 Pick Standard Constructions over Proprietary Constructions
    2. 6.2 Pick Compact Constructions over Longer Equivalents
    3. 6.3 Use Comments
    4. 6.4 Avoid Optimizer Hints
    5. 6.5 Avoid Triggers in Favor of DRI Actions
    6. 6.6 Use SQL Stored Procedures
    7. 6.7 Avoid User-Defined Functions and Extensions inside the Database
    8. 6.8 Avoid Excessive Secondary Indexes
    9. 6.9 Avoid Correlated Subqueries
    10. 6.10 Avoid UNIONS
    11. 6.11 Testing SQL
  13. Chapter 7: How to Use Views
    1. 7.1 VIEW Naming Conventions Are the Same as Tables
    2. 7.2 VIEWs Provide Row- and Column-Level Security
    3. 7.3 VIEWs Ensure Efficient Access Paths
    4. 7.4 VIEWs Mask Complexity from the User
    5. 7.5 VIEWs Ensure Proper Data Derivation
    6. 7.6 VIEWs Rename Tables and/or Columns
    7. 7.7 VIEWs Enforce Complicated Integrity Constraints
    8. 7.8 Updatable VIEWs
    9. 7.9 Have a Reason for Each VIEW
    10. 7.10 Avoid VIEW Proliferation
    11. 7.11 Synchronize VIΕWs with Base Tables
    12. 7.12 Improper Use of VIEWs
    13. 7.13 Learn about Materialized VIEWs
  14. Chapter 8: How to Write Stored Procedures
    1. 8.1 Most SQL 4GLs Are Not for Applications
    2. 8.2 Basic Software Engineering
    3. 8.3 Use Classic Structured Programming
    4. 8.4 Avoid Portability Problems
    5. 8.5 Scalar versus Structured Parameters
    6. 8.6 Avoid Dynamic SQL
  15. Chapter 9: Heuristics
    1. 9.1 Put the Specification into a Clear Statement
    2. 9.2 Add the Words “Set of All . . ./” in Front of the Nouns
    3. 9.3 Remove Active Verbs from the Problem Statement
    4. 9.4 You Can Still Use Stubs
    5. 9.5 Do Not Worry about Displaying the Data
    6. 9.6 Your First Attempts Need Special Handling
    7. 9.7 Do Not Think with Boxes and Arrows
    8. 9.8 Draw Circles and Set Diagrams
    9. 9.9 Learn Your Dialect
    10. 9.10 Imagine That Your WHERE Clause Is “Super Ameba”
    11. 9.11 Use the Newsgroups and Internet
  16. Chapter 10: Thinking in SQL
    1. 10.1 Bad Programming in SQL and Procedural Languages
    2. 10.2 Thinking of Columns as Fields
    3. 10.3 Thinking in Processes, Not Declarations
    4. 10.4 Thinking the Schema Should Look Like the Input Forms
  17. Resources
    1. Military Standards
    2. Metadata Standards
    3. ANSI and ISO Standards
    4. U.S. Government Codes
    5. Retail Industry
    6. Code Formatting and Naming Conventions
  18. Bibliography
    1. Reading Psychology
    2. Programming Considerations
  19. Index
  20. About the author
  21. Instructions for online access