Joe Celko's SQL Programming Style
By Joe Celko
4/5
()
About this ebook
- 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!
Joe Celko
Joe Celko served 10 years on ANSI/ISO SQL Standards Committee and contributed to the SQL-89 and SQL-92 Standards. Mr. Celko is author a series of books on SQL and RDBMS for Elsevier/MKP. He is an independent consultant based in Austin, Texas. He has written over 1200 columns in the computer trade and academic press, mostly dealing with data and databases.
Read more from Joe Celko
Joe Celko's SQL for Smarties: Advanced SQL Programming Rating: 3 out of 5 stars3/5Joe Celko's SQL Puzzles and Answers Rating: 4 out of 5 stars4/5Joe Celko's Trees and Hierarchies in SQL for Smarties Rating: 0 out of 5 stars0 ratingsJoe Celko's Thinking in Sets: Auxiliary, Temporal, and Virtual Tables in SQL Rating: 3 out of 5 stars3/5Joe Celko's Data, Measurements and Standards in SQL Rating: 0 out of 5 stars0 ratingsJoe Celko's Analytics and OLAP in SQL Rating: 4 out of 5 stars4/5
Related to Joe Celko's SQL Programming Style
Related ebooks
SQL Server: Tips and Tricks - 2 Rating: 4 out of 5 stars4/5SQL: Practical Guide for Developers Rating: 2 out of 5 stars2/5Relational Database Design and Implementation: Clearly Explained Rating: 0 out of 5 stars0 ratingsSQL Server MVP Deep Dives Rating: 0 out of 5 stars0 ratingsMaking Sense of NoSQL: A guide for managers and the rest of us Rating: 0 out of 5 stars0 ratingsAn Introduction to Data Base Design Rating: 0 out of 5 stars0 ratingsBeginning Oracle Database 12c Administration: From Novice to Professional Rating: 0 out of 5 stars0 ratingsDatabase Design and SQL for DB2 Rating: 5 out of 5 stars5/5Graph Databases in Action: Examples in Gremlin Rating: 0 out of 5 stars0 ratingsPro SQL Server Internals Rating: 0 out of 5 stars0 ratingsPROC SQL: Beyond the Basics Using SAS, Third Edition Rating: 0 out of 5 stars0 ratingsExpert T-SQL Window Functions in SQL Server 2019: The Hidden Secret to Fast Analytic and Reporting Queries Rating: 0 out of 5 stars0 ratingsCreating your MySQL Database: Practical Design Tips and Techniques Rating: 3 out of 5 stars3/5The Data Model Resource Book: Volume 3: Universal Patterns for Data Modeling Rating: 0 out of 5 stars0 ratingsThe Data Model Resource Book, Volume 1: A Library of Universal Data Models for All Enterprises Rating: 0 out of 5 stars0 ratingsJump Start MySQL: Master the Database That Powers the Web Rating: 0 out of 5 stars0 ratingsDynamic SQL: Applications, Performance, and Security in Microsoft SQL Server Rating: 0 out of 5 stars0 ratingsSemantic Web for the Working Ontologist: Effective Modeling in RDFS and OWL Rating: 4 out of 5 stars4/5Mastering PL/SQL Through Illustrations: From Learning Fundamentals to Developing Efficient PL/SQL Blocks (English Edition) Rating: 0 out of 5 stars0 ratingsJoe Celko's Analytics and OLAP in SQL Rating: 4 out of 5 stars4/5Joe Celko's Data, Measurements and Standards in SQL Rating: 0 out of 5 stars0 ratingsPhysical Database Design: The Database Professional's Guide to Exploiting Indexes, Views, Storage, and More Rating: 5 out of 5 stars5/5Relational Database Design Clearly Explained Rating: 5 out of 5 stars5/5Database Modeling and Design: Logical Design Rating: 0 out of 5 stars0 ratingsDatabase Design: Know It All Rating: 5 out of 5 stars5/5The SQL Workshop: Learn to create, manipulate and secure data and manage relational databases with SQL Rating: 0 out of 5 stars0 ratingsInformation Modeling and Relational Databases Rating: 0 out of 5 stars0 ratings100+ SQL Queries T-SQL for Microsoft SQL Server Rating: 4 out of 5 stars4/5
Programming For You
Python: For Beginners A Crash Course Guide To Learn Python in 1 Week Rating: 4 out of 5 stars4/5Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5HTML & CSS: Learn the Fundaments in 7 Days Rating: 4 out of 5 stars4/5Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5SQL: For Beginners: Your Guide To Easily Learn SQL Programming in 7 Days Rating: 5 out of 5 stars5/5101 Amazing Nintendo NES Facts: Includes facts about the Famicom Rating: 4 out of 5 stars4/5Java for Beginners: A Crash Course to Learn Java Programming in 1 Week Rating: 5 out of 5 stars5/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Pokemon Go: Guide + 20 Tips and Tricks You Must Read Hints, Tricks, Tips, Secrets, Android, iOS Rating: 5 out of 5 stars5/5Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5Python Machine Learning By Example Rating: 4 out of 5 stars4/5Learn SQL in 24 Hours Rating: 5 out of 5 stars5/5Linux: Learn in 24 Hours Rating: 5 out of 5 stars5/5PYTHON: Practical Python Programming For Beginners & Experts With Hands-on Project Rating: 5 out of 5 stars5/5Excel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5C# 7.0 All-in-One For Dummies Rating: 0 out of 5 stars0 ratingsGrokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5SQL All-in-One For Dummies Rating: 3 out of 5 stars3/5ReactJS by Example - Building Modern Web Applications with React Rating: 4 out of 5 stars4/5
Reviews for Joe Celko's SQL Programming Style
5 ratings0 reviews
Book preview
Joe Celko's SQL Programming Style - Joe Celko
author
Introduction
I AM NOT trying to teach you to program in SQL in this book. You might want to read that again. If that is what you wanted, there are better books. This ought to be the second book you buy, not the first.
I assume that you already write SQL at some level and want to get better at it. If you want to learn SQL programming tricks, get a copy of my other book, SQL for Smarties (3rd edition, 2005). I am trying to teach the reader how to work in logical and declarative terms, instead of in a procedural or OO manner—Query Eye for the Database Guy,
if you will forgive a horrible contemporary pun.
Few, if any, SQL programmers came to SQL before learning and writing for years in a procedural or object-oriented language. They then got one particular SQL product and were told to learn it on their own or with a book that has a title like SQL for Brain-Dead Morons,
Learn SQL in Ten Easy Lessons or Five Hard Ones,
or worse.
This is absurd! It takes at least five years to learn to be a master carpenter or chef. Why would you believe people could become SQL gurus in a weekend? What they become is bad SQL programmers, who speak SQL in dialect from the local SQL product with a strong accent from their previous languages. You might want to read Teach Yourself Programming in Ten Years
by Peter Norvig (www.norvig.com/21-days.html) or No Silver Bullets
by Fred Brooks, Computer, 20(4): 10–19, April 1987) to get a reality check.
The horrible part is that these people often don’t know they are bad programmers. At one extreme, the entire shop where they work is just as bad, and they never see anything else. At the other extreme, if anyone tries to tell them about their problems, they become defensive or angry. If you look at postings on SQL newsgroups, many programmers just want to get a kludge for an immediate problem and not actually obtain a true long-term solution.
If these were woodworking newsgroups, their questions would be the equivalent of What are the best kind of rocks to use to pound screws into fine furniture?
When someone tells them to use large chunks of granite, they are happy, but if you try to tell them about screwdrivers, they explode into a rage.
You might want to read an essay on this phenomenon: Unskilled and Unaware of It: How Difficulties in Recognizing One’s Own Incompetence Lead to Inflated Self-Assessments
by Justin Kruger and David Dunning (Department of Psychology, Cornell University, www.apa.org/journals/psp/psp7761121.html).
Or look at the actual and self-assessments of American high school students in mathematics and sciences that were part of the Bush administration’s No Child Left Behind Act.
1.1 Purpose of the Book
So how did we old farts learn to be better programmers when dinosaurs walked the earth? One of the best helpers we had in the late 1970s when the structured programming revolution came along was a series of books entitled [Pascal | FORTRAN | COBOL | BASIC] with Style: Programming Proverbs
by Henry Ledgard and some of his colleagues at MIT. The covers were done like a Victorian novel with angels, scrolls, and old-style typographical elements. And like a Victorian novel, the books were subtitled Principles of Good Programming with Numerous Examples to Improve Programming Style and Proficiency.
These books and others made a big difference for most of us because they taught us how to think like good programmers.
My goals in this book are to improve SQL programming style and proficiency. To be more exact:
1. To help an individual programmer write Standard SQL without an accent or a dialect. It is difficult to unlearn old habits but not impossible, and it is best to learn the right way from the start. Amateurs write code for themselves. A professional writes codeto be maintained and used by other people. My rule of thumb has been that you need to have a full year of SQL programming before you have your epiphany and suddenly see the world in three: valued logic, data models, and sets.
2. To give an SQL shop a coding standard for internal use. I have tried carefully to give a rationale for each of my rules, and I have given exceptions to those rules when I could think of them. You may disagree with some of my choices, but you will have to provide research and examples to defend your position. It is not good enough to simply declare: Well, that’s the way we wrote code in FooTran, so it must be the will of God!
as an argument.
If you are the team leader, you now have a book (and author) that you can hold up and blame for anything that your people do not like. Even if I am later shown to be wrong about something, you will have been consistent. It is much easier to repair errors if they were made consistently.
3. To give programmers the mental tools to approach a new problem with SQL as their tool. I tell people it takes about a year to get it
and drop your procedural programming habits.
1.2 Acknowledgments
Craig Mullins provided the structure of the chapter on VIEWs in an article in www.DBAzine.com. The formatting style is taken from a house style I have used in CMP magazines and other publications for more than a decade. Peter Gulutzan provided the data for the naming conventions in actual products from an article inwww.DBAzine.com. The affix conventions in Chapter 1 are based on internal standards from Teradata Corporation. The scales and measurements and the encoding schemes material appeared in several of my old magazine columns in DBMS and Database Programming & Design before they were collected into a chapter in my book Data & Database (Morgan-Kaufmann Publishers). I have tried to give credit in the text, but so many people have participated in the newsgroups over the years that I know I am forgetting someone.
And, obviously, thanks to Henry Ledgard and his Programming Proverbs
series for the inspiration.
I would also like to thank all of the newbie programmers who wrote bad code. It sounds a bit sarcastic, but it is not meant to be. Many of thenewbies are programmers who were thrown into a DBA or SQL programmer job by management without training or an experienced mentor. I do not want to blame the victims unless they are really not working on getting better. Your errors in syntax, semantics, and style showed me how you were thinking. Diagnosis is the first step to treatment.
1.3 Corrections, Comments, and Future Editions
Corrections and additions for future editions can be sent to Morgan-Kaufmann publishers directly or to me at my e-mail address, jcelko212@earthlink.net.
Names and Data Elements
This is the old joke:
When I was a kid, we had three cats.
What were their names?
Cat, cat, and cat.
That sounds screwed up. How did you tell them apart?
Who cares? Cats don’t come when you call them anyway!
YOUR DATA WILL not come when it is called either if you do not give it a name that is always distinct and recognizable. This is an important part of any database project. Bad names for the data elements make the code difficult, or even impossible, to read.
I am not kidding about impossible to read. In the old days, software companies used to deliberately scramble source code names and remove formatting to hide the algorithm from the buyers. The tradition seems to linger on, even if not by intent. In August 2004, a SQL newsgroup had a posting in which all of the names were one letter and a long string of digits.
There are now ISO-11179 metadata standards that describe rules for naming data elements and for registering standards. Because they are an ISO standard, they are what you should be using in SQL as well as everywhere else.
That standard, a bit of typography, and some common sense will give you the rules you need to get started.
1.1 Names
In the early days, every programmer had his or her own personal naming conventions. Unfortunately, they were often highly creative. My favorite was a guy who picked a theme for his COBOL paragraph names: one program might use countries, another might use flowers, and so forth. This is obviously weird behavior even for a programmer, but many programmers had personal systems that made sense to themselves but not to other people.
For example, the first FORTRAN I used allowed only six-letter names, so I became adept at using and inventing six-letter names. Programmers who started with weakly typed or typeless languages like to use Hungarian notation (see Leszynski and Reddick). Old habits are hard to give up.
When software engineering became the norm, every shop developed its own naming conventions and enforced them with some kind of data dictionary. Perhaps the most widespread set of rules was MIL STD 8320.1, set up by the U.S. Department of Defense, but it never became popular outside of the federal government. This was a definite improvement over the prior nonsystem, but each shop varied quite a bit; some had formal rules for name construction, whereas others simply registered whatever the first name given to a data element was.
Today, we have ISO-11179 standards, which are becoming increasingly widespread, required for certain government work, and being put into data repository products. Tools and repositories of standardized encoding schemes are being built to this standard. Given this and XML as a standard exchange format, ISO-11179 will be the way that metadata is referenced in the future.
1.1.1 Watch the Length of Names
Rationale:
The SQL-92 standards have a maximum identifier length of 18 characters. This length came from the older COBOL standards. These days, SQL implementations allow longer names, but if you cannot say it in 18 characters, then you have a problem. Table 1.1 shows the maximum length for names of the most important SQL schema objects according to ISO and several popular SQL products.
Table 1.1 I dentifier lengths
The numbers in the table are either bytes or characters. A maximum character length can be smaller than a maximum byte length if you use a multibyte character set.
Do not use super-long names. People have to read them, type them, and print them out. They also have to be able to understand those names when they look at the code, search for them in the data dictionary, and so forth. Finally, the names need to be shared in host programs that might not allow the same maximum length.
But do not go to the other extreme of highly condensed names that are impossible to read without weeks of study. The old Bachman design tool was used to build DB2 databases back when column length was limited to 18 bytes. Sometimes the tool would change the logical attribute name to a physical column name by removing all of the vowels. Craig Mullins referred to this as Bachman having a vowel movement on my DDL.
This is a bad approach to getting the name to fit within a smaller number of characters.
Exceptions:
These exceptions would be on a case-by-case basis and probably the result of legacy systems that had different naming restrictions.