SQL Coding Standards

Last Modified: 2009-04-13

Table of Contents

Back to Coding Standards list

1. Overview

1.1. Introduction

...

1.2. Scope

Describing the rules and guidelines for all possible SQL code occurrences in web applications.

1.3. Goals

The goals of this text are the same as corresponding part of Web Development Coding Standards for PHP.

1.4. Assumptions

Current CS was written with intention to be as more abstract from specific SQL engine as possible. But of course we have to admit that MySQL is the #1 engine for most PHP Web developers. So, please take into account this fact (or at least author's assumtion) while reading the text below.

2. Formatting

2.1. General

The following common rules must be considered while writing any SQL statement:

These rules will be explained with examples below in the Coding Style section.

2.2. Indentation

Using of tabs for identation is strongly advised, with tab's width set to 4 spaces.

2.3. Maximum Line Length

The target line length is 80 characters, i.e. developers should aim keep code as close to the 80-column boundary as is practical. However, longer lines are acceptable.

3. Naming Conventions

3.1 Talkative names

The same rules as described in corresponding section for PHP Coding Standards.

3.2. No Upper Case Abbreviations

The same rules as described in corresponding section for PHP Coding Standards.

3.3. Tables

The following rules are obligatory for table names:

Example

Incorrect

Photo           -- upper cased char 
photocomments   -- two words without any separator
commentsPhotos  -- camel cased

Correct

photos 
photo_comments

3.4. Columns

Columns are named taking into account following rules:

Example

Incorrect

permission_group_id            -- underscores
permissions_permissionGroupId  -- table prefix 
prmGrpId                       -- try to read this without context in year or something 

Correct

permissionGroupId

3.5. Constraints

Next rules should be considered for Primary Keys (PK) constraints:

Rules Foreign Keys (FK) constraints:

3.6. Indexes

For indexes please use idx_column format. You can also use idx_table_name__column format, but it's really not necesseary, since indexes are table level objects.

For unque constraints approach is the same as for indexes but with uniq_ prefix.

4. Coding Style

4.1. SQL statements and functions

It is strongly advised to use upper case notation for all SQL reserved words and functions. In first case it's a strict rule, and a guideline in second one.

Example

Incorrect

select * from table where date = now()

Select * 
From table
where date = now()

Correct

SELECT * 
FROM table
WHERE date = NOW()

4.2. SELECT queries

Taking into account general formatting rules, the correct SELECT query will be formatted like this:

SELECT a.field1, a.field2, b.field3...
FROM table1 a
    INNER JOIN table2 b
        ON a.fieldId = b.fieldId
        AND a.field1 = b.field1
WHERE a.someOtherField = 'value'
    AND b.anotherCondition = 1
    AND b.field4 IN (
        SELECT c.field4
        FROM table3 c
	)
ORDER BY a.field1 

Some comments to the example above:

4.3. INSERT/UPDATE/DELETE queries

Examples of correct data modification queries:

INSERT INTO table1 
    (field1, field2)
VALUES
    (1, 'some data')

UPDATE table1 SET
    field2 = 'new data'
WHERE field1 = 1

DELETE FROM table1
WHERE field1 = 1

4.4. Other queries

There is no any strict rule for all other statements, but anyway please keep in mind #2.1 when writing them.