2015-07-06 07:06:51 -05:00
|
|
|
|
# SQL style guide
|
|
|
|
|
|
|
|
|
|
## Overview
|
|
|
|
|
|
2015-07-08 03:38:32 -05:00
|
|
|
|
You can use this set of guidelines, [fork them][fork] or make your own - the
|
2015-07-06 07:06:51 -05:00
|
|
|
|
key here is that you pick a style and stick to it. To suggest changes
|
2017-03-28 23:32:48 -05:00
|
|
|
|
or fix bugs please open an [issue][issue] or [pull request][pull] on GitHub.
|
2015-07-06 07:06:51 -05:00
|
|
|
|
|
2015-07-09 03:58:06 -05:00
|
|
|
|
These guidelines are designed to be compatible with Joe Celko's [SQL Programming
|
2015-07-09 04:00:02 -05:00
|
|
|
|
Style][celko] 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
|
2015-07-09 03:58:06 -05:00
|
|
|
|
little more relaxed. It is certainly more succinct where [Celko's book][celko]
|
|
|
|
|
contains anecdotes and reasoning behind each rule as thoughtful prose.
|
|
|
|
|
|
2015-07-11 17:36:58 -05:00
|
|
|
|
It is easy to include this guide in [Markdown format][dl-md] as a part of a
|
2015-07-09 03:58:06 -05:00
|
|
|
|
project's code base or reference it here for anyone on the project to freely
|
2015-07-12 16:24:28 -05:00
|
|
|
|
read—much harder with a physical book.
|
2015-07-11 17:36:58 -05:00
|
|
|
|
|
2020-05-25 06:27:01 -05:00
|
|
|
|
SQL style guide by [Simon Holywell][simon] is licensed under a [Creative Commons
|
2015-07-11 17:36:58 -05:00
|
|
|
|
Attribution-ShareAlike 4.0 International License][licence].
|
2019-11-18 06:31:05 -06:00
|
|
|
|
Based on a work at [https://www.sqlstyle.guide/][sqlstyleguide].
|
2015-07-09 03:58:06 -05:00
|
|
|
|
|
2015-07-06 07:06:51 -05:00
|
|
|
|
## General
|
|
|
|
|
|
2015-07-07 09:15:46 -05:00
|
|
|
|
### Do
|
|
|
|
|
|
2015-07-09 07:39:15 -05:00
|
|
|
|
* Use consistent and descriptive identifiers and names.
|
2015-07-12 16:24:28 -05:00
|
|
|
|
* Make judicious use of white space and indentation to make code easier to read.
|
2020-05-25 06:27:01 -05:00
|
|
|
|
* Store [ISO 8601][iso-8601] compliant time and date information
|
2015-07-12 16:24:28 -05:00
|
|
|
|
(`YYYY-MM-DD HH:MM:SS.SSSSS`).
|
2020-05-25 06:27:01 -05:00
|
|
|
|
* Try to only use standard SQL functions instead of vendor-specific functions for
|
2015-07-09 07:39:15 -05:00
|
|
|
|
reasons of portability.
|
2015-07-12 16:24:28 -05:00
|
|
|
|
* Keep code succinct and devoid of redundant SQL—such as unnecessary quoting or
|
2015-07-09 07:39:15 -05:00
|
|
|
|
parentheses or `WHERE` clauses that can otherwise be derived.
|
|
|
|
|
* Include comments in SQL code where necessary. Use the C style opening `/*` and
|
2016-10-10 04:28:04 -05:00
|
|
|
|
closing `*/` where possible otherwise precede comments with `--` and finish
|
2015-07-09 07:39:15 -05:00
|
|
|
|
them with a new line.
|
|
|
|
|
|
2015-07-06 07:06:51 -05:00
|
|
|
|
```sql
|
2015-07-13 06:23:00 -05:00
|
|
|
|
SELECT file_hash -- stored ssdeep hash
|
2015-07-11 17:36:58 -05:00
|
|
|
|
FROM file_system
|
|
|
|
|
WHERE file_name = '.vimrc';
|
|
|
|
|
```
|
|
|
|
|
```sql
|
|
|
|
|
/* Updating the file record after writing to the file */
|
|
|
|
|
UPDATE file_system
|
|
|
|
|
SET file_modified_date = '1980-02-22 13:19:01.00000',
|
|
|
|
|
file_size = 209732
|
|
|
|
|
WHERE file_name = '.vimrc';
|
2015-07-06 07:06:51 -05:00
|
|
|
|
```
|
|
|
|
|
|
2016-05-16 08:57:32 -05:00
|
|
|
|
### Avoid
|
|
|
|
|
|
|
|
|
|
* CamelCase—it is difficult to scan quickly.
|
|
|
|
|
* Descriptive prefixes or Hungarian notation such as `sp_` or `tbl`.
|
|
|
|
|
* Plurals—use the more natural collective term where possible instead. For example
|
|
|
|
|
`staff` instead of `employees` or `people` instead of `individuals`.
|
2019-11-18 06:31:05 -06:00
|
|
|
|
* Quoted identifiers—if you must use them then stick to SQL-92 double quotes for
|
2016-05-16 08:57:32 -05:00
|
|
|
|
portability (you may need to configure your SQL server to support this depending
|
|
|
|
|
on vendor).
|
2019-11-18 06:31:05 -06:00
|
|
|
|
* Object-oriented design principles should not be applied to SQL or database
|
2016-05-16 08:57:32 -05:00
|
|
|
|
structures.
|
|
|
|
|
|
2015-07-09 03:58:06 -05:00
|
|
|
|
## Naming conventions
|
|
|
|
|
|
|
|
|
|
### General
|
|
|
|
|
|
|
|
|
|
* Ensure the name is unique and does not exist as a
|
|
|
|
|
[reserved keyword][reserved-keywords].
|
|
|
|
|
* Keep the length to a maximum of 30 bytes—in practice this is 30 characters
|
2020-05-25 06:27:01 -05:00
|
|
|
|
unless you are using a multi-byte character set.
|
2015-07-09 03:58:06 -05:00
|
|
|
|
* Names must begin with a letter and may not end with an underscore.
|
|
|
|
|
* Only use letters, numbers and underscores in names.
|
|
|
|
|
* Avoid the use of multiple consecutive underscores—these can be hard to read.
|
2015-07-12 16:24:28 -05:00
|
|
|
|
* Use underscores where you would naturally include a space in the name (first
|
|
|
|
|
name becomes `first_name`).
|
2015-07-09 03:58:06 -05:00
|
|
|
|
* Avoid abbreviations and if you have to use them make sure they are commonly
|
|
|
|
|
understood.
|
|
|
|
|
|
|
|
|
|
```sql
|
|
|
|
|
SELECT first_name
|
|
|
|
|
FROM staff;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Tables
|
|
|
|
|
|
|
|
|
|
* Use a collective name or, less ideally, a plural form. For example (in order of
|
2015-07-12 16:24:28 -05:00
|
|
|
|
preference) `staff` and `employees`.
|
2015-07-09 03:58:06 -05:00
|
|
|
|
* Do not prefix with `tbl` or any other such descriptive prefix or Hungarian
|
|
|
|
|
notation.
|
2015-07-12 16:24:28 -05:00
|
|
|
|
* Never give a table the same name as one of its columns and vice versa.
|
2015-07-09 03:58:06 -05:00
|
|
|
|
* Avoid, where possible, concatenating two table names together to create the name
|
2015-07-24 15:48:42 -05:00
|
|
|
|
of a relationship table. Rather than `cars_mechanics` prefer `services`.
|
2015-07-09 03:58:06 -05:00
|
|
|
|
|
|
|
|
|
### Columns
|
|
|
|
|
|
|
|
|
|
* Always use the singular name.
|
2015-07-13 06:54:36 -05:00
|
|
|
|
* Where possible avoid simply using `id` as the primary identifier for the table.
|
2015-07-12 16:24:28 -05:00
|
|
|
|
* Do not add a column with the same name as its table and vice versa.
|
2015-07-09 03:58:06 -05:00
|
|
|
|
* Always use lowercase except where it may make sense not to such as proper nouns.
|
|
|
|
|
|
|
|
|
|
### Aliasing or correlations
|
|
|
|
|
|
|
|
|
|
* Should relate in some way to the object or expression they are aliasing.
|
2015-07-12 16:53:17 -05:00
|
|
|
|
* As a rule of thumb the correlation name should be the first letter of each word
|
2015-07-09 03:58:06 -05:00
|
|
|
|
in the object's name.
|
2015-07-12 16:53:17 -05:00
|
|
|
|
* If there is already a correlation with the same name then append a number.
|
2015-07-09 03:58:06 -05:00
|
|
|
|
* Always include the `AS` keyword—makes it easier to read as it is explicit.
|
|
|
|
|
* For computed data (`SUM()` or `AVG()`) use the name you would give it were it
|
|
|
|
|
a column defined in the schema.
|
|
|
|
|
|
|
|
|
|
```sql
|
|
|
|
|
SELECT first_name AS fn
|
|
|
|
|
FROM staff AS s1
|
|
|
|
|
JOIN students AS s2
|
|
|
|
|
ON s2.mentor_id = s1.staff_num;
|
2015-07-12 16:24:28 -05:00
|
|
|
|
```
|
|
|
|
|
```sql
|
2015-07-09 03:58:06 -05:00
|
|
|
|
SELECT SUM(s.monitor_tally) AS monitor_total
|
|
|
|
|
FROM staff AS s;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Stored procedures
|
|
|
|
|
|
|
|
|
|
* The name must contain a verb.
|
|
|
|
|
* Do not prefix with `sp_` or any other such descriptive prefix or Hungarian
|
|
|
|
|
notation.
|
|
|
|
|
|
|
|
|
|
### 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.
|
|
|
|
|
|
|
|
|
|
* `_id`—a unique identifier such as a column that is a primary key.
|
|
|
|
|
* `_status`—flag value or some other status of any type such as
|
|
|
|
|
`publication_status`.
|
|
|
|
|
* `_total`—the total or sum of a collection of values.
|
|
|
|
|
* `_num`—denotes the field contains any kind of number.
|
|
|
|
|
* `_name`—signifies a name such as `first_name`.
|
|
|
|
|
* `_seq`—contains a contiguous sequence of values.
|
|
|
|
|
* `_date`—denotes a column that contains the date of something.
|
|
|
|
|
* `_tally`—a count.
|
|
|
|
|
* `_size`—the size of something such as a file size or clothing.
|
|
|
|
|
* `_addr`—an address for the record could be physical or intangible such as
|
|
|
|
|
`ip_addr`.
|
|
|
|
|
|
|
|
|
|
## Query syntax
|
2015-07-06 07:06:51 -05:00
|
|
|
|
|
|
|
|
|
### Reserved words
|
|
|
|
|
|
2015-07-08 03:38:32 -05:00
|
|
|
|
Always use uppercase for the [reserved keywords][reserved-keywords]
|
2015-07-07 09:15:46 -05:00
|
|
|
|
like `SELECT` and `WHERE`.
|
2015-07-06 07:06:51 -05:00
|
|
|
|
|
|
|
|
|
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
|
2019-11-18 06:31:05 -06:00
|
|
|
|
exists performing the same function. This helps to make the code more portable.
|
2015-07-06 07:06:51 -05:00
|
|
|
|
|
|
|
|
|
```sql
|
|
|
|
|
SELECT model_num
|
|
|
|
|
FROM phones AS p
|
|
|
|
|
WHERE p.release_date > '2014-09-30';
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### White space
|
|
|
|
|
|
2018-04-29 19:28:09 -05:00
|
|
|
|
To make the code easier to read it is important that the correct complement of
|
2015-07-08 03:38:32 -05:00
|
|
|
|
spacing is used. Do not crowd code or remove natural language spaces.
|
2015-07-06 07:06:51 -05:00
|
|
|
|
|
2015-07-08 02:29:04 -05:00
|
|
|
|
#### Spaces
|
|
|
|
|
|
|
|
|
|
Spaces should be used to line up the code so that the root keywords all end on
|
2015-07-08 03:38:32 -05:00
|
|
|
|
the same character boundary. This forms a river down the middle making it easy for
|
2015-07-08 02:29:04 -05:00
|
|
|
|
the readers eye to scan over the code and separate the keywords from the
|
2015-07-08 03:38:32 -05:00
|
|
|
|
implementation detail. Rivers are [bad in typography][rivers], but helpful here.
|
2015-07-08 02:29:04 -05:00
|
|
|
|
|
|
|
|
|
```sql
|
2017-02-26 17:55:03 -06:00
|
|
|
|
(SELECT f.species_name,
|
|
|
|
|
AVG(f.height) AS average_height, AVG(f.diameter) AS average_diameter
|
|
|
|
|
FROM flora AS f
|
|
|
|
|
WHERE f.species_name = 'Banksia'
|
|
|
|
|
OR f.species_name = 'Sheoak'
|
|
|
|
|
OR f.species_name = 'Wattle'
|
|
|
|
|
GROUP BY f.species_name, f.observation_date)
|
|
|
|
|
|
|
|
|
|
UNION ALL
|
|
|
|
|
|
|
|
|
|
(SELECT b.species_name,
|
|
|
|
|
AVG(b.height) AS average_height, AVG(b.diameter) AS average_diameter
|
|
|
|
|
FROM botanic_garden_flora AS b
|
|
|
|
|
WHERE b.species_name = 'Banksia'
|
|
|
|
|
OR b.species_name = 'Sheoak'
|
|
|
|
|
OR b.species_name = 'Wattle'
|
2019-06-19 12:00:59 -05:00
|
|
|
|
GROUP BY b.species_name, b.observation_date);
|
2015-07-08 02:29:04 -05:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Notice that `SELECT`, `FROM`, etc. are all right aligned while the actual column
|
2020-05-25 06:27:01 -05:00
|
|
|
|
names and implementation-specific details are left aligned.
|
2015-07-08 02:29:04 -05:00
|
|
|
|
|
|
|
|
|
Although not exhaustive always include spaces:
|
2015-07-06 07:06:51 -05:00
|
|
|
|
|
|
|
|
|
* before and after equals (`=`)
|
|
|
|
|
* after commas (`,`)
|
|
|
|
|
* surrounding apostrophes (`'`) where not within parentheses or with a trailing
|
2015-07-09 03:58:06 -05:00
|
|
|
|
comma or semicolon.
|
2015-07-06 07:06:51 -05:00
|
|
|
|
|
2015-07-08 02:29:04 -05:00
|
|
|
|
```sql
|
|
|
|
|
SELECT a.title, a.release_date, a.recording_date
|
|
|
|
|
FROM albums AS a
|
|
|
|
|
WHERE a.title = 'Charcoal Lane'
|
|
|
|
|
OR a.title = 'The New Danger';
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### Line spacing
|
|
|
|
|
|
2015-07-06 07:06:51 -05:00
|
|
|
|
Always include newlines/vertical space:
|
|
|
|
|
|
2015-07-08 02:29:04 -05:00
|
|
|
|
* before `AND` or `OR`
|
2015-07-06 07:06:51 -05:00
|
|
|
|
* after semicolons to separate queries for easier reading
|
|
|
|
|
* after each keyword definition
|
2015-07-07 09:15:46 -05:00
|
|
|
|
* after a comma when separating multiple columns into logical groups
|
2015-07-08 02:29:04 -05:00
|
|
|
|
* to separate code into related sections, which helps to ease the readability of
|
2015-07-09 03:58:06 -05:00
|
|
|
|
large chunks of code.
|
2015-07-06 07:06:51 -05:00
|
|
|
|
|
|
|
|
|
Keeping all the keywords aligned to the righthand side and the values left aligned
|
2019-11-18 06:31:05 -06:00
|
|
|
|
creates a uniform gap down the middle of the query. It also makes it much easier to
|
|
|
|
|
to quickly scan over the query definition.
|
2015-07-06 07:06:51 -05:00
|
|
|
|
|
2015-07-11 17:36:58 -05:00
|
|
|
|
```sql
|
|
|
|
|
INSERT INTO albums (title, release_date, recording_date)
|
|
|
|
|
VALUES ('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');
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```sql
|
|
|
|
|
UPDATE albums
|
|
|
|
|
SET release_date = '1990-01-01 01:01:01.00000'
|
|
|
|
|
WHERE title = 'The New Danger';
|
|
|
|
|
```
|
|
|
|
|
|
2015-07-06 07:06:51 -05:00
|
|
|
|
```sql
|
2015-07-07 09:15:46 -05:00
|
|
|
|
SELECT a.title,
|
|
|
|
|
a.release_date, a.recording_date, a.production_date -- grouped dates together
|
2015-07-06 07:06:51 -05:00
|
|
|
|
FROM albums AS a
|
|
|
|
|
WHERE a.title = 'Charcoal Lane'
|
|
|
|
|
OR a.title = 'The New Danger';
|
|
|
|
|
```
|
|
|
|
|
|
2015-07-07 09:15:46 -05:00
|
|
|
|
### Indentation
|
|
|
|
|
|
|
|
|
|
To ensure that SQL is readable it is important that standards of indentation
|
|
|
|
|
are followed.
|
|
|
|
|
|
|
|
|
|
#### Joins
|
|
|
|
|
|
2015-07-08 14:21:51 -05:00
|
|
|
|
Joins should be indented to the other side of the river and grouped with a new
|
|
|
|
|
line where necessary.
|
2015-07-07 09:15:46 -05:00
|
|
|
|
|
|
|
|
|
```sql
|
|
|
|
|
SELECT r.last_name
|
|
|
|
|
FROM riders AS r
|
|
|
|
|
INNER JOIN bikes AS b
|
|
|
|
|
ON r.bike_vin_num = b.vin_num
|
2018-07-19 18:36:01 -05:00
|
|
|
|
AND b.engine_tally > 2
|
2015-07-07 09:15:46 -05:00
|
|
|
|
|
|
|
|
|
INNER JOIN crew AS c
|
|
|
|
|
ON r.crew_chief_last_name = c.last_name
|
|
|
|
|
AND c.chief = 'Y';
|
|
|
|
|
```
|
|
|
|
|
|
2016-05-19 08:13:02 -05:00
|
|
|
|
#### Subqueries
|
2015-07-07 09:15:46 -05:00
|
|
|
|
|
2016-05-19 08:13:02 -05:00
|
|
|
|
Subqueries should also be aligned to the right side of the river and then laid
|
2015-07-08 14:21:51 -05:00
|
|
|
|
out using the same style as any other query. Sometimes it will make sense to have
|
2017-11-24 14:09:23 -06:00
|
|
|
|
the closing parenthesis on a new line at the same character position as its
|
2016-05-19 08:13:02 -05:00
|
|
|
|
opening partner—this is especially true where you have nested subqueries.
|
2015-07-08 14:21:51 -05:00
|
|
|
|
|
2015-07-07 09:15:46 -05:00
|
|
|
|
```sql
|
|
|
|
|
SELECT r.last_name,
|
|
|
|
|
(SELECT MAX(YEAR(championship_date))
|
|
|
|
|
FROM champions AS c
|
|
|
|
|
WHERE c.last_name = r.last_name
|
|
|
|
|
AND c.confirmed = 'Y') AS last_championship_year
|
|
|
|
|
FROM riders AS r
|
|
|
|
|
WHERE r.last_name IN
|
|
|
|
|
(SELECT c.last_name
|
|
|
|
|
FROM champions AS c
|
|
|
|
|
WHERE YEAR(championship_date) > '2008'
|
|
|
|
|
AND c.confirmed = 'Y');
|
|
|
|
|
```
|
|
|
|
|
|
2015-07-09 07:39:15 -05:00
|
|
|
|
### Preferred formalisms
|
|
|
|
|
|
|
|
|
|
* Make use of `BETWEEN` where possible instead of combining multiple statements
|
|
|
|
|
with `AND`.
|
|
|
|
|
* Similarly use `IN()` instead of multiple `OR` clauses.
|
|
|
|
|
* Where a value needs to be interpreted before leaving the database use the `CASE`
|
|
|
|
|
expression. `CASE` statements can be nested to form more complex logical structures.
|
|
|
|
|
* Avoid the use of `UNION` clauses and temporary tables where possible. If the
|
|
|
|
|
schema can be optimised to remove the reliance on these features then it most
|
|
|
|
|
likely should be.
|
|
|
|
|
|
|
|
|
|
```sql
|
|
|
|
|
SELECT CASE postcode
|
|
|
|
|
WHEN 'BN1' THEN 'Brighton'
|
|
|
|
|
WHEN 'EH1' THEN 'Edinburgh'
|
|
|
|
|
END AS city
|
|
|
|
|
FROM office_locations
|
|
|
|
|
WHERE country = 'United Kingdom'
|
|
|
|
|
AND opening_time BETWEEN 8 AND 9
|
2019-06-19 12:00:59 -05:00
|
|
|
|
AND postcode IN ('EH1', 'BN1', 'NN1', 'KW1');
|
2015-07-09 07:39:15 -05:00
|
|
|
|
```
|
|
|
|
|
|
2015-07-08 14:21:51 -05:00
|
|
|
|
## Create syntax
|
|
|
|
|
|
2020-05-25 06:27:01 -05:00
|
|
|
|
When declaring schema information it is also important to maintain human-readable
|
|
|
|
|
code. To facilitate this ensure that the column definitions are ordered and
|
|
|
|
|
grouped together where it makes sense to do so.
|
2015-07-08 14:21:51 -05:00
|
|
|
|
|
2015-07-09 02:16:39 -05:00
|
|
|
|
Indent column definitions by four (4) spaces within the `CREATE` definition.
|
|
|
|
|
|
2015-07-08 14:21:51 -05:00
|
|
|
|
### Choosing data types
|
|
|
|
|
|
2019-11-18 06:31:05 -06:00
|
|
|
|
* Where possible do not use vendor-specific data types—these are not portable and
|
2015-07-09 02:16:39 -05:00
|
|
|
|
may not be available in older versions of the same vendor's software.
|
|
|
|
|
* Only use `REAL` or `FLOAT` types where it is strictly necessary for floating
|
|
|
|
|
point mathematics otherwise prefer `NUMERIC` and `DECIMAL` at all times. Floating
|
|
|
|
|
point rounding errors are a nuisance!
|
2015-07-08 14:21:51 -05:00
|
|
|
|
|
|
|
|
|
### Specifying default values
|
|
|
|
|
|
|
|
|
|
* The default value must be the same type as the column—if a column is declared
|
2015-07-09 02:16:39 -05:00
|
|
|
|
a `DECIMAL` do not provide an `INTEGER` default value.
|
2015-07-08 14:21:51 -05:00
|
|
|
|
* Default values must follow the data type declaration and come before any
|
2015-07-09 02:16:39 -05:00
|
|
|
|
`NOT NULL` statement.
|
|
|
|
|
|
|
|
|
|
### Constraints and keys
|
|
|
|
|
|
2015-07-09 03:58:06 -05:00
|
|
|
|
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
|
|
|
|
|
|
2017-11-24 14:09:23 -06:00
|
|
|
|
Deciding the column(s) that will form the keys in the definition should be a
|
2015-07-09 03:58:06 -05:00
|
|
|
|
carefully considered activity as it will effect performance and data integrity.
|
|
|
|
|
|
|
|
|
|
1. The key should be unique to some degree.
|
2015-07-12 16:24:28 -05:00
|
|
|
|
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.
|
2015-07-09 03:58:06 -05:00
|
|
|
|
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
|
2015-07-12 16:24:28 -05:00
|
|
|
|
constraints along with field value validation.
|
2015-07-09 03:58:06 -05:00
|
|
|
|
|
2015-07-09 04:10:02 -05:00
|
|
|
|
##### General
|
|
|
|
|
|
|
|
|
|
* Tables must have at least one key to be complete and useful.
|
|
|
|
|
* Constraints should be given a custom name excepting `UNIQUE`, `PRIMARY KEY`
|
|
|
|
|
and `FOREIGN KEY` where the database vendor will generally supply sufficiently
|
|
|
|
|
intelligible names automatically.
|
|
|
|
|
|
|
|
|
|
##### Layout and order
|
|
|
|
|
|
2015-07-09 02:16:39 -05:00
|
|
|
|
* Specify the primary key first right after the `CREATE TABLE` statement.
|
|
|
|
|
* Constraints should be defined directly beneath the column they correspond to.
|
2015-07-09 03:58:06 -05:00
|
|
|
|
Indent the constraint so that it aligns to the right of the column name.
|
|
|
|
|
* If it is a multi-column constraint then consider putting it as close to both
|
2015-07-09 02:16:39 -05:00
|
|
|
|
column definitions as possible and where this is difficult as a last resort
|
2015-07-09 03:58:06 -05:00
|
|
|
|
include them at the end of the `CREATE TABLE` definition.
|
2019-11-18 06:31:05 -06:00
|
|
|
|
* If it is a table-level constraint that applies to the entire table then it
|
2015-07-09 03:58:06 -05:00
|
|
|
|
should also appear at the end.
|
2015-07-09 04:10:02 -05:00
|
|
|
|
* Use alphabetical order where `ON DELETE` comes before `ON UPDATE`.
|
2015-07-12 16:24:28 -05:00
|
|
|
|
* If it make senses to do so align each aspect of the query on the same character
|
|
|
|
|
position. For example all `NOT NULL` definitions could start at the same
|
2015-07-12 16:53:17 -05:00
|
|
|
|
character position. This is not hard and fast, but it certainly makes the code
|
2015-07-12 16:24:28 -05:00
|
|
|
|
much easier to scan and read.
|
2015-07-09 04:10:02 -05:00
|
|
|
|
|
|
|
|
|
##### Validation
|
|
|
|
|
|
2015-07-09 02:16:39 -05:00
|
|
|
|
* Use `LIKE` and `SIMILAR TO` constraints to ensure the integrity of strings
|
2015-07-09 03:58:06 -05:00
|
|
|
|
where the format is known.
|
2015-07-09 02:16:39 -05:00
|
|
|
|
* Where the ultimate range of a numerical value is known it must be written as a
|
|
|
|
|
range `CHECK()` to prevent incorrect values entering the database or the silent
|
|
|
|
|
truncation of data too large to fit the column definition. In the least it
|
|
|
|
|
should check that the value is greater than zero in most cases.
|
|
|
|
|
* `CHECK()` constraints should be kept in separate clauses to ease debugging.
|
2015-07-09 04:10:02 -05:00
|
|
|
|
|
|
|
|
|
##### Example
|
2015-07-09 02:16:39 -05:00
|
|
|
|
|
2015-07-09 03:58:06 -05:00
|
|
|
|
```sql
|
2015-07-09 02:16:39 -05:00
|
|
|
|
CREATE TABLE staff (
|
|
|
|
|
PRIMARY KEY (staff_num),
|
|
|
|
|
staff_num INT(5) NOT NULL,
|
|
|
|
|
first_name VARCHAR(100) NOT NULL,
|
|
|
|
|
pens_in_drawer INT(2) NOT NULL,
|
2015-07-09 03:58:06 -05:00
|
|
|
|
CONSTRAINT pens_in_drawer_range
|
2020-08-27 19:44:07 -05:00
|
|
|
|
CHECK(pens_in_drawer BETWEEN 1 AND 99)
|
2015-07-09 02:16:39 -05:00
|
|
|
|
);
|
|
|
|
|
```
|
2015-07-08 14:21:51 -05:00
|
|
|
|
|
2015-07-09 07:39:15 -05:00
|
|
|
|
### Designs to avoid
|
|
|
|
|
|
2019-11-18 06:31:05 -06:00
|
|
|
|
* Object-oriented design principles do not effectively translate to relational
|
2015-07-09 07:39:15 -05:00
|
|
|
|
database designs—avoid this pitfall.
|
|
|
|
|
* Placing the value in one column and the units in another column. The column
|
2019-11-18 06:31:05 -06:00
|
|
|
|
should make the units self-evident to prevent the requirement to combine
|
2015-07-09 07:39:15 -05:00
|
|
|
|
columns again later in the application. Use `CHECK()` to ensure valid data is
|
|
|
|
|
inserted into the column.
|
2019-11-18 06:31:05 -06:00
|
|
|
|
* [Entity–Attribute–Value][eav] (EAV) tables—use a specialist product intended for
|
2015-07-09 07:39:15 -05:00
|
|
|
|
handling such schema-less data instead.
|
2019-11-18 06:31:05 -06:00
|
|
|
|
* Splitting up data that should be in one table across many tables because of
|
2020-05-25 06:27:01 -05:00
|
|
|
|
arbitrary concerns such as time-based archiving or location in a multinational
|
2015-07-09 07:39:15 -05:00
|
|
|
|
organisation. Later queries must then work across multiple tables with `UNION`
|
|
|
|
|
rather than just simply querying one table.
|
2015-07-06 07:06:51 -05:00
|
|
|
|
|
|
|
|
|
|
2015-07-07 09:25:45 -05:00
|
|
|
|
## Appendix
|
2015-07-06 07:06:51 -05:00
|
|
|
|
|
|
|
|
|
### Reserved keyword reference
|
|
|
|
|
|
2015-07-12 16:24:28 -05:00
|
|
|
|
A list of ANSI SQL (92, 99 and 2003), MySQL 3 to 5.x, PostgreSQL 8.1, MS SQL Server 2000, MS ODBC and Oracle 10.2 reserved keywords.
|
2015-07-06 07:06:51 -05:00
|
|
|
|
|
|
|
|
|
```sql
|
|
|
|
|
A
|
|
|
|
|
ABORT
|
|
|
|
|
ABS
|
|
|
|
|
ABSOLUTE
|
|
|
|
|
ACCESS
|
|
|
|
|
ACTION
|
|
|
|
|
ADA
|
|
|
|
|
ADD
|
|
|
|
|
ADMIN
|
|
|
|
|
AFTER
|
|
|
|
|
AGGREGATE
|
|
|
|
|
ALIAS
|
|
|
|
|
ALL
|
|
|
|
|
ALLOCATE
|
|
|
|
|
ALSO
|
|
|
|
|
ALTER
|
|
|
|
|
ALWAYS
|
|
|
|
|
ANALYSE
|
|
|
|
|
ANALYZE
|
|
|
|
|
AND
|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
CHARACTERISTICS
|
|
|
|
|
CHARACTERS
|
|
|
|
|
CHECK
|
|
|
|
|
CHECKED
|
|
|
|
|
CHECKPOINT
|
|
|
|
|
CHECKSUM
|
|
|
|
|
CLASS
|
|
|
|
|
CLASS_ORIGIN
|
|
|
|
|
CLOB
|
|
|
|
|
CLOSE
|
|
|
|
|
CLUSTER
|
|
|
|
|
CLUSTERED
|
|
|
|
|
COALESCE
|
|
|
|
|
COBOL
|
|
|
|
|
COLLATE
|
|
|
|
|
COLLATION
|
|
|
|
|
COLLATION_CATALOG
|
|
|
|
|
COLLATION_NAME
|
|
|
|
|
COLLATION_SCHEMA
|
|
|
|
|
COLLECT
|
|
|
|
|
COLUMN
|
|
|
|
|
COLUMN_NAME
|
|
|
|
|
COLUMNS
|
|
|
|
|
COMMAND_FUNCTION
|
|
|
|
|
COMMAND_FUNCTION_CODE
|
|
|
|
|
COMMENT
|
|
|
|
|
COMMIT
|
|
|
|
|
COMMITTED
|
|
|
|
|
COMPLETION
|
|
|
|
|
COMPRESS
|
|
|
|
|
COMPUTE
|
|
|
|
|
CONDITION
|
|
|
|
|
CONDITION_NUMBER
|
|
|
|
|
CONNECT
|
|
|
|
|
CONNECTION
|
|
|
|
|
CONNECTION_NAME
|
|
|
|
|
CONSTRAINT
|
|
|
|
|
CONSTRAINT_CATALOG
|
|
|
|
|
CONSTRAINT_NAME
|
|
|
|
|
CONSTRAINT_SCHEMA
|
|
|
|
|
CONSTRAINTS
|
|
|
|
|
CONSTRUCTOR
|
|
|
|
|
CONTAINS
|
|
|
|
|
CONTAINSTABLE
|
|
|
|
|
CONTINUE
|
|
|
|
|
CONVERSION
|
|
|
|
|
CONVERT
|
|
|
|
|
COPY
|
|
|
|
|
CORR
|
|
|
|
|
CORRESPONDING
|
|
|
|
|
COUNT
|
|
|
|
|
COVAR_POP
|
|
|
|
|
COVAR_SAMP
|
|
|
|
|
CREATE
|
|
|
|
|
CREATEDB
|
|
|
|
|
CREATEROLE
|
|
|
|
|
CREATEUSER
|
|
|
|
|
CROSS
|
|
|
|
|
CSV
|
|
|
|
|
CUBE
|
|
|
|
|
CUME_DIST
|
|
|
|
|
CURRENT
|
|
|
|
|
CURRENT_DATE
|
|
|
|
|
CURRENT_DEFAULT_TRANSFORM_GROUP
|
|
|
|
|
CURRENT_PATH
|
|
|
|
|
CURRENT_ROLE
|
|
|
|
|
CURRENT_TIME
|
|
|
|
|
CURRENT_TIMESTAMP
|
|
|
|
|
CURRENT_TRANSFORM_GROUP_FOR_TYPE
|
|
|
|
|
CURRENT_USER
|
|
|
|
|
CURSOR
|
|
|
|
|
CURSOR_NAME
|
|
|
|
|
CYCLE
|
|
|
|
|
DATA
|
|
|
|
|
DATABASE
|
|
|
|
|
DATABASES
|
|
|
|
|
DATE
|
|
|
|
|
DATETIME
|
|
|
|
|
DATETIME_INTERVAL_CODE
|
|
|
|
|
DATETIME_INTERVAL_PRECISION
|
|
|
|
|
DAY
|
|
|
|
|
DAY_HOUR
|
|
|
|
|
DAY_MICROSECOND
|
|
|
|
|
DAY_MINUTE
|
|
|
|
|
DAY_SECOND
|
|
|
|
|
DAYOFMONTH
|
|
|
|
|
DAYOFWEEK
|
|
|
|
|
DAYOFYEAR
|
|
|
|
|
DBCC
|
|
|
|
|
DEALLOCATE
|
|
|
|
|
DEC
|
|
|
|
|
DECIMAL
|
|
|
|
|
DECLARE
|
|
|
|
|
DEFAULT
|
|
|
|
|
DEFAULTS
|
|
|
|
|
DEFERRABLE
|
|
|
|
|
DEFERRED
|
|
|
|
|
DEFINED
|
|
|
|
|
DEFINER
|
|
|
|
|
DEGREE
|
|
|
|
|
DELAY_KEY_WRITE
|
|
|
|
|
DELAYED
|
|
|
|
|
DELETE
|
|
|
|
|
DELIMITER
|
|
|
|
|
DELIMITERS
|
|
|
|
|
DENSE_RANK
|
|
|
|
|
DENY
|
|
|
|
|
DEPTH
|
|
|
|
|
DEREF
|
|
|
|
|
DERIVED
|
|
|
|
|
DESC
|
|
|
|
|
DESCRIBE
|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PS
|
|
|
|
|
OVERLAY
|
|
|
|
|
OVERRIDING
|
|
|
|
|
OWNER
|
|
|
|
|
PACK_KEYS
|
|
|
|
|
PAD
|
|
|
|
|
PARAMETER
|
|
|
|
|
PARAMETER_MODE
|
|
|
|
|
PARAMETER_NAME
|
|
|
|
|
PARAMETER_ORDINAL_POSITION
|
|
|
|
|
PARAMETER_SPECIFIC_CATALOG
|
|
|
|
|
PARAMETER_SPECIFIC_NAME
|
|
|
|
|
PARAMETER_SPECIFIC_SCHEMA
|
|
|
|
|
PARAMETERS
|
|
|
|
|
PARTIAL
|
|
|
|
|
PARTITION
|
|
|
|
|
PASCAL
|
|
|
|
|
PASSWORD
|
|
|
|
|
PATH
|
|
|
|
|
PCTFREE
|
|
|
|
|
PERCENT
|
|
|
|
|
PERCENT_RANK
|
|
|
|
|
PERCENTILE_CONT
|
|
|
|
|
PERCENTILE_DISC
|
|
|
|
|
PLACING
|
|
|
|
|
PLAN
|
|
|
|
|
PLI
|
|
|
|
|
POSITION
|
|
|
|
|
POSTFIX
|
|
|
|
|
POWER
|
|
|
|
|
PRECEDING
|
|
|
|
|
PRECISION
|
|
|
|
|
PREFIX
|
|
|
|
|
PREORDER
|
|
|
|
|
PREPARE
|
|
|
|
|
PREPARED
|
|
|
|
|
PRESERVE
|
|
|
|
|
PRIMARY
|
|
|
|
|
PRINT
|
|
|
|
|
PRIOR
|
|
|
|
|
PRIVILEGES
|
|
|
|
|
PROC
|
|
|
|
|
PROCEDURAL
|
|
|
|
|
PROCEDURE
|
|
|
|
|
PROCESS
|
|
|
|
|
PROCESSLIST
|
|
|
|
|
PUBLIC
|
|
|
|
|
PURGE
|
|
|
|
|
QUOTE
|
|
|
|
|
RAID0
|
|
|
|
|
RAISERROR
|
|
|
|
|
RANGE
|
|
|
|
|
RANK
|
|
|
|
|
RAW
|
|
|
|
|
READ
|
|
|
|
|
READS
|
|
|
|
|
READTEXT
|
|
|
|
|
REAL
|
|
|
|
|
RECHECK
|
|
|
|
|
R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|
|
|
VACUUM
|
|
|
|
|
VALID
|
|
|
|
|
VALIDATE
|
|
|
|
|
VALIDATOR
|
|
|
|
|
VALUE
|
|
|
|
|
VALUES
|
|
|
|
|
VAR_POP
|
|
|
|
|
VAR_SAMP
|
|
|
|
|
VARBINARY
|
|
|
|
|
VARCHAR
|
|
|
|
|
VARCHAR2
|
|
|
|
|
VARCHARACTER
|
|
|
|
|
VARIABLE
|
|
|
|
|
VARIABLES
|
|
|
|
|
VARYING
|
|
|
|
|
VERBOSE
|
|
|
|
|
VIEW
|
|
|
|
|
VOLATILE
|
|
|
|
|
WAITFOR
|
|
|
|
|
WHEN
|
|
|
|
|
WHENEVER
|
|
|
|
|
WHERE
|
|
|
|
|
WHILE
|
|
|
|
|
WIDTH_BUCKET
|
|
|
|
|
WINDOW
|
|
|
|
|
WITH
|
|
|
|
|
WITHIN
|
|
|
|
|
WITHOUT
|
|
|
|
|
WORK
|
|
|
|
|
WRITE
|
|
|
|
|
WRITETEXT
|
|
|
|
|
X509
|
|
|
|
|
XOR
|
|
|
|
|
YEAR
|
|
|
|
|
YEAR_MONTH
|
|
|
|
|
ZEROFILL
|
|
|
|
|
ZONE
|
|
|
|
|
```
|
2015-07-08 03:38:32 -05:00
|
|
|
|
|
2019-09-09 23:53:34 -05:00
|
|
|
|
### Column data types
|
|
|
|
|
|
|
|
|
|
These are some suggested column data types to use for maximum compatibility between database engines.
|
|
|
|
|
|
|
|
|
|
#### Character types:
|
|
|
|
|
|
|
|
|
|
* CHAR
|
|
|
|
|
* CLOB
|
|
|
|
|
* VARCHAR
|
|
|
|
|
|
|
|
|
|
#### Numeric types
|
|
|
|
|
|
|
|
|
|
* Exact numeric types
|
|
|
|
|
* BIGINT
|
|
|
|
|
* DECIMAL
|
|
|
|
|
* DECFLOAT
|
|
|
|
|
* INTEGER
|
|
|
|
|
* NUMERIC
|
|
|
|
|
* SMALLINT
|
|
|
|
|
* Approximate numeric types
|
|
|
|
|
* DOUBLE PRECISION
|
|
|
|
|
* FLOAT
|
|
|
|
|
* REAL
|
|
|
|
|
|
|
|
|
|
#### Datetime types
|
|
|
|
|
|
|
|
|
|
* DATE
|
|
|
|
|
* TIME
|
|
|
|
|
* TIMESTAMP
|
|
|
|
|
|
|
|
|
|
#### Binary types:
|
|
|
|
|
|
|
|
|
|
* BINARY
|
|
|
|
|
* BLOB
|
|
|
|
|
* VARBINARY
|
|
|
|
|
|
|
|
|
|
#### Additional types
|
|
|
|
|
|
2020-05-25 06:27:01 -05:00
|
|
|
|
* BOOLEAN
|
2019-09-09 23:53:34 -05:00
|
|
|
|
* INTERVAL
|
|
|
|
|
* XML
|
|
|
|
|
|
|
|
|
|
|
2015-07-13 05:48:15 -05:00
|
|
|
|
[simon]: https://www.simonholywell.com/?utm_source=sqlstyle.guide&utm_medium=link&utm_campaign=md-document
|
2015-07-11 17:36:58 -05:00
|
|
|
|
"SimonHolywell.com"
|
2015-07-13 05:29:31 -05:00
|
|
|
|
[issue]: https://github.com/treffynnon/sqlstyle.guide/issues
|
|
|
|
|
"SQL style guide issues on GitHub"
|
|
|
|
|
[fork]: https://github.com/treffynnon/sqlstyle.guide/fork
|
|
|
|
|
"Fork SQL style guide on GitHub"
|
|
|
|
|
[pull]: https://github.com/treffynnon/sqlstyle.guide/pulls/
|
|
|
|
|
"SQL style guide pull requests on GitHub"
|
2016-08-31 21:37:57 -05:00
|
|
|
|
[celko]: https://www.amazon.com/gp/product/0120887975/ref=as_li_ss_tl?ie=UTF8&linkCode=ll1&tag=treffynnon-20&linkId=9c88eac8cd420e979675c815771313d5
|
2015-07-09 03:58:06 -05:00
|
|
|
|
"Joe Celko's SQL Programming Style (The Morgan Kaufmann Series in Data Management Systems)"
|
2015-07-13 05:29:31 -05:00
|
|
|
|
[dl-md]: https://raw.githubusercontent.com/treffynnon/sqlstyle.guide/gh-pages/_includes/sqlstyle.guide.md
|
2015-07-09 03:58:06 -05:00
|
|
|
|
"Download the guide in Markdown format"
|
2015-07-09 07:39:15 -05:00
|
|
|
|
[iso-8601]: https://en.wikipedia.org/wiki/ISO_8601
|
|
|
|
|
"Wikipedia: ISO 8601"
|
2019-11-18 06:31:05 -06:00
|
|
|
|
[rivers]: https://practicaltypography.com/one-space-between-sentences.html
|
2015-07-08 03:38:32 -05:00
|
|
|
|
"Practical Typography: one space between sentences"
|
|
|
|
|
[reserved-keywords]: #reserved-keyword-reference
|
2017-11-24 14:09:23 -06:00
|
|
|
|
"Reserved keyword reference"
|
2015-07-09 07:39:15 -05:00
|
|
|
|
[eav]: https://en.wikipedia.org/wiki/Entity%E2%80%93attribute%E2%80%93value_model
|
2015-07-11 17:36:58 -05:00
|
|
|
|
"Wikipedia: Entity–attribute–value model"
|
2019-11-18 06:31:05 -06:00
|
|
|
|
[sqlstyleguide]: https://www.sqlstyle.guide/
|
2015-07-11 17:36:58 -05:00
|
|
|
|
"SQL style guide by Simon Holywell"
|
2019-11-18 06:31:05 -06:00
|
|
|
|
[licence]: https://creativecommons.org/licenses/by-sa/4.0/
|
2015-07-13 06:23:00 -05:00
|
|
|
|
"Creative Commons Attribution-ShareAlike 4.0 International License"
|