---
layout: default
---
Overview
You can use this set of guidelines, fork 'em or make your own - the
key here is that you pick a style and stick to it. To suggest changes
or fix bugs please open an issue or pull request on Git Hub.
General
Avoid the use of
- CamelCase—it is difficult to scan quickly
- Descriptive prefixes or Hungarian notation such as
sp_
or tbl
- Plurals—use the more natural collective term instead
- Quoted identifiers—if you must use them then stick to SQL92 double quotes for portability
{% highlight sql %}{% include sql/naming_conventions.sql %}{% endhighlight %}
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.
{% highlight sql %}{% include sql/syntax_reserved_keywords.sql %}{% endhighlight %}
Indentation
Use four (4) spaces all the way through all the time. This makes the most sense where there is a sub query.
{% highlight sql %}{% include sql/syntax_indentation.sql %}{% endhighlight %}
White space
To make the code easier to read it is important that the correct compliment of spaces is used. Do not crowd code or remove natural spaces.
Always include spaces (this is not an exhaustive list):
- before and after equals (
=
)
- after commas (
,
)
- surrounding apostrophes (
'
) where not within parentheses or with a trailing comma or semicolon
Always include newlines/vertical space:
- after semicolons to separate queries for easier reading
- after each keyword definition
- after the comma of each column in the list
- before
AND
or OR
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.
{% highlight sql %}{% include sql/syntax_spacing.sql %}{% endhighlight %}
Naming conventions
General
- Ensure the name is unique and does not exist as a reserved keyword
- Keep the length to a maximum of 30 bytes—in practice this is 30 characters unless you are using multibyte charset
- 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
- Use underscores where you would naturally include a space in name (first name becomes
first_name
) -
- Avoid abbreviations and if you have to use them make sure they are commonly understood
{% highlight sql %}{% include sql/naming_conventions.sql %}{% endhighlight %}
Tables
- Use a collective name or, less ideally, a plural form. For example (in order of preference) staff and employees.
- 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
- Avoid, where possible, concatenating two table names together to create the name of a relationship table. Rather than
car_mechanic
prefer service
{% highlight sql %}{% include sql/naming_conventions.sql %}{% endhighlight %}
Columns
- Always use the singular name
- Avoid simply using
id
as the primary identifier for the table
- Do not add a column with the same name as its table
- Always use lowercase except where it may make sense not to such as proper nouns
{% highlight sql %}{% include sql/naming_conventions.sql %}{% endhighlight %}
Aliasing or correlations
- Should relate in some way to the object or expression they are aliasing
- As rule of thumb the correlation name should be the first letter of each word in the object's name
- If there is already a correlation with same name then append a number
- 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
{% highlight sql %}{% include sql/naming_correlations.sql %}{% endhighlight %}
Stored procedures
- The name must contain a verb
- Do not prefix with
sp_
or any other such descriptive prefix or Hungarian notation
{% highlight sql %}{% include sql/naming_conventions.sql %}{% endhighlight %}
Uniform postfixes
_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
_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
{% highlight sql %}{% include sql/naming_conventions.sql %}{% endhighlight %}
Appendices
Reserved keyword reference
A list of ANSI SQL 92, ANSI SQL 99, ANSI SQL 2003, MySQL 3.23.x, MySQL 4.x, MySQL 5.x, PostGreSQL 8.1, MS SQL Server 2000, MS ODBC, Oracle 10.2 reserved keywords.
{% highlight SQL %}{% include sql/appendices_reserved_keywords.sql %}{% endhighlight %}
body of the site is here
{% highlight sql %}{% include sql/test.sql %}{% endhighlight %}