This document serves as the complete definition of Prosa's coding standards for source code in the SQL Programming Language.
Example code in this document is non-normative. That is, while the examples are in Prosa Style, they may not illustrate the only stylish way to represent the code. Optional formatting choices made in examples should not be enforced as rules.
Braces follow the Kernighan and Ritchie style ("Egyptian brackets")
Each time a new block or block-like construct is opened, the indent increases by 2 spaces. When the block ends, the indent returns to the previous indent level. The indent level applies to both code and comments throughout the block. (See the example in Section 4.1.2, K & R Style.)
SQL code has a column limit of 120 characters
Any line break may be preceded by arbitrary whitespace followed by an implementation comment. Such a comment renders the line non-blank.
Block comments are indented at the same level as the surrounding code. They may be in
/* ... */ style or
-- ... style.
Comments are not enclosed in boxes drawn with asterisks or other characters.
Optional grouping parentheses are omitted only when author and reviewer agree that there is no reasonable chance the code will be misinterpreted without them, nor would they have made the code easier to read. It is not reasonable to assume that every reader has the entire SQL operator precedence table memorized.
The expressions following the SELECT statement are either all in the SELECT line or each in a new line, indented by 2 spaces.
Examples:
// Correct
SELECT col1, col2, col3
// Correct
SELECT
col1,
col2,
col3
// Wrong
SELECT col1,
col2,
col3
The FROM keyword is aligned vertically with the SELECT statement.
Examples:
// Correct
SELECT *
FROM tbl
// Wrong
SELECT *
FROM tbl
The JOIN keyword always start in a new line and is indented by 2 spaces with respect of the FROM keyword.
The ON keyword is in the same line of the respective JOIN. Additional conditions must begin in a new line and be indented by at least 4 spaces with respect of the JOIN keyword.
When a JOIN is present, all tables must have an alias.
Examples:
// Correct
SELECT *
FROM tbl t
JOIN tbl1 t1 ON t.tbl1_id = t1.id
AND tbl1.code = 1
// Wrong
SELECT *
FROM tbl t
JOIN tbl1 t1 ON t.tbl1_id = t1.id AND tbl1.code = 1
// Wrong
SELECT *
FROM tbl
JOIN tbl1 ON tbl.tbl1_id = tbl1.id
The WHERE keyword is aligned vertically with the SELECT and FROM keywords.
All conditions must be either in the same line of the WHERE keyword or each in one line, with the first one in the same line of the WHERE, and others indented by 2 spaces.
Examples:
// Correct
SELECT *
FROM tbl t
WHERE t.code > 1 AND t.code < 10
// Correct
SELECT *
FROM tbl t
WHERE t.code > 1
AND t.code < 10
OR t.code = 5
Conditions wrapped in parenthesis are indented by 2 spaces.
Examples:
// Correct
SELECT *
FROM tbl t
WHERE t.code > 1
AND (
t.code = 1
OR t.code = 2
)
// Wrong
SELECT *
FROM tbl t
WHERE t.code > 1
AND (
t.code = 1
OR t.code = 2
)
Identifiers use only ASCII letters, digits and underscores.
Thus each valid identifier name is matched by the regular expression
\w+ .
Identifiers are written in snake_case.
Table names are in plural form.
Foreign key columns must end with _id.
A column that is not a foreign key cannot end with _id.
The column with the record creation date must be called created_at.
The column with the record update date must be called updated_at.