Scout24/sqlstyle.guide

Name: sqlstyle.guide

Owner: AutoScout24

Description: A consistent code style guide for SQL to ensure legible and maintainable projects

Forked from: treffynnon/sqlstyle.guide

Created: 2018-02-19 10:00:09.0

Updated: 2018-02-19 10:04:22.0

Pushed: 2018-02-19 11:56:20.0

Homepage:

Size: 330

Language: HTML

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

SQL style guide

Overview

These guidelines are designed to be compatible with Joe Celko's book to make adoption for teams who have already read that book easier. This guide is a little more opinionated in some areas and in others a little more relaxed. It is certainly more succinct where Celko's book contains anecdotes and reasoning behind each rule as thoughtful prose.

SQL style guide by Simon Holywell is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

General
Do
CT file_hash  -- stored ssdeep hash
 file_system
E file_name = '.vimrc';
pdating the file record after writing to the file */
TE file_system

le_modified_date = '1980-02-22 13:19:01.00000',
le_size = 209732
E file_name = '.vimrc';
Avoid
Naming conventions
General
CT first_name
 staff;
Tables
Columns
Aliasing or correlations
CT first_name AS fn
 staff AS s1
 students AS s2
 s2.mentor_id = s1.staff_num;
CT SUM(s.monitor_tally) AS monitor_total
 staff AS s;
Stored procedures
Uniform suffixes

The following suffixes have a universal meaning ensuring the columns can be read and understood easily from SQL code. Use the correct suffix where appropriate.

Query syntax
Reserved words

Always use uppercase for the reserved keywords like SELECT and WHERE.

It is best to avoid the abbreviated keywords and use the full length ones where available (prefer ABSOLUTE to ABS).

Do not use database server specific keywords where an ANSI SQL keyword already exists performing the same function. This helps to make code more portable.

CT model_num
 phones AS p
E p.release_date > '2014-09-30';
White space

To make the code easier to read it is important that the correct compliment of spacing is used. Do not crowd code or remove natural language spaces.

Spaces

Spaces should be used to line up the code so that the root keywords all end on the same character boundary. This forms a river down the middle making it easy for the readers eye to scan over the code and separate the keywords from the implementation detail. Rivers are bad in typography, but helpful here.

ECT 
species_name,
G(f.height) AS average_height,
G(f.diameter) AS average_diameter
 flora AS f
E f.species_name = 'Banksia'
 f.species_name = 'Sheoak'
 f.species_name = 'Wattle'
P BY f.species_name, f.observation_date


N ALL

ECT 
species_name,
G(b.height) AS average_height,
G(b.diameter) AS average_diameter
 botanic_garden_flora AS b
E b.species_name = 'Banksia'
 b.species_name = 'Sheoak'
 b.species_name = 'Wattle'
P BY 
species_name, 
observation_date

Notice that the main keywords such as SELECT, FROM, etc. are all left aligned.

Although not exhaustive always include spaces:

CT 
title, 
release_date, 
recording_date
 albums AS a
E a.title = 'Charcoal Lane'
 a.title = 'The New Danger';
Line spacing

Always include newlines/vertical space:

Keeping all the keywords aligned to the righthand side and the values left aligned creates a uniform gap down the middle of query. It makes it much easier to scan the query definition over quickly too.

RT INTO albums (title, release_date, recording_date)
LUES 
('Charcoal Lane', '1990-01-01 01:01:01.00000', '1990-01-01 01:01:01.00000'),
('The New Danger', '2008-01-01 01:01:01.00000', '1990-01-01 01:01:01.00000');
ql
TE albums
release_date = '1990-01-01 01:01:01.00000'
E title = 'The New Danger';
ql
CT 
title,
release_date,
recording_date,
production_date -- grouped dates together
 albums AS a
E a.title = 'Charcoal Lane'
 a.title = 'The New Danger';
Indentation

To ensure that SQL is readable it is important that standards of indentation are followed.

Joins

Joins should be indented to the other side of the river and grouped with a new line where necessary.

CT r.last_name
 riders AS r
NER JOIN bikes AS b
ON r.bike_vin_num = b.vin_num
AND b.engines > 2

NNER JOIN crew AS c
ON r.crew_chief_last_name = c.last_name
AND c.chief = 'Y';
Subqueries

Subqueries should also be aligned to the right side of the river and then laid out using the same style as any other query. Sometimes it will make sense to have the closing parenthesis on a new line at the same character position as its opening partner?this is especially true where you have nested subqueries.

CT 
last_name,
ELECT MAX(YEAR(championship_date))
ROM champions AS c
HERE c.last_name = r.last_name
AND c.confirmed = 'Y'
AS last_championship_year
 riders AS r
E r.last_name IN
ELECT c.last_name
ROM champions AS c
HERE YEAR(championship_date) > '2008'
AND c.confirmed = 'Y'

Preferred formalisms
CT 
SE postcode
WHEN 'BN1' THEN 'Brighton'
WHEN 'EH1' THEN 'Edinburgh'
D AS city
 office_locations
E country = 'United Kingdom'
D opening_time BETWEEN 8 AND 9
D postcode IN ('EH1', 'BN1', 'NN1', 'KW1')
Create syntax

When declaring schema information it is also important to maintain human readable code. To facilitate this ensure the column definitions are ordered and grouped where it makes sense to do so.

Indent column definitions by four (4) spaces within the CREATE definition.

Choosing data types
Specifying default values
Constraints and keys

Constraints and their subset, keys, are a very important component of any database definition. They can quickly become very difficult to read and reason about though so it is important that a standard set of guidelines are followed.

Choosing keys

Deciding the column(s) that will form the keys in the definition should be a carefully considered activity as it will effect performance and data integrity.

  1. The key should be unique to some degree.
  2. Consistency in terms of data type for the value across the schema and a lower likelihood of this changing in the future.
  3. Can the value be validated against a standard format (such as one published by ISO)? Encouraging conformity to point 2.
  4. Keeping the key as simple as possible whilst not being scared to use compound keys where necessary.

It is a reasoned and considered balancing act to be performed at the definition of a database. Should requirements evolve in the future it is possible to make changes to the definitions to keep them up to date.

Defining constraints

Once the keys are decided it is possible to define them in the system using constraints along with field value validation.

General Layout and order Validation Example
TE TABLE staff (
IMARY KEY (staff_num),
staff_num      INT(5)       NOT NULL,
first_name     VARCHAR(100) NOT NULL,
pens_in_drawer INT(2)       NOT NULL,
NSTRAINT pens_in_drawer_range
ECK(pens_in_drawer >= 1 AND pens_in_drawer < 100)

Designs to avoid

This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.