SQL Style Guide
Tables Name
Use a collective name or, less ideally, a plural form. For example (in order of preference)
staff
andemployees
.Do not prefix with
tbl
or any other such descriptive prefix or Hungarian notation.Never give a table the same name as one of its columns and vice versa.
Avoid, where possible, concatenating two table names together to create the name of a relationship table. Rather than
cars_mechanics
preferservices
.
Columns
Always use the singular name.
Where possible avoid simply using id as the primary identifier for the table.
Do not add a column with the same name as its table and vice versa.
Always use lowercase except where it may make sense not to such as proper nouns.
Reserved Words
Always use UPPERCASE for reserved keywords.
Eg: SELECT, AS, FROM, WHERE, LIKE, OR, ORDER BY, ASC, DESC, etc...
.
Aliasing
Always include the AS keyword when aliasing a variable, it's easier to read when explicit.
Left Align Root Keywords and Code Blocks
Root keywords should all start on the same character boundary and on their own line. This is counter to the common "rivers" pattern described here.
Do not include multiple arguments in one line.
Line Spacing
AND
and OR
should always be at the beginning of the line. For example:
Parentheses
If parentheses span multiple lines:
The opening parenthesis should terminate the line.
The closing parenthesis should be lined up under the first character of the line that starts the multi-line construct.
The contents of the parentheses should be indented one level.
JOIN Queries
Nested Queries
Avoid use nested queries. Instead, use common table expressions to improve readability.
References
Last updated
Was this helpful?