--- 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

{% 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):

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.

{% highlight sql %}{% include sql/syntax_spacing.sql %}{% endhighlight %}

Naming conventions

General

{% highlight sql %}{% include sql/naming_conventions.sql %}{% endhighlight %}

Tables

{% highlight sql %}{% include sql/naming_conventions.sql %}{% endhighlight %}

Columns

{% highlight sql %}{% include sql/naming_conventions.sql %}{% endhighlight %}

Aliasing or correlations

{% highlight sql %}{% include sql/naming_correlations.sql %}{% endhighlight %}

Stored procedures

{% highlight sql %}{% include sql/naming_conventions.sql %}{% endhighlight %}

Uniform postfixes

{% 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 %}