Last Modified: 2009-04-13
Back to Coding Standards list
...
Describing the rules and guidelines for all possible SQL code occurrences in web applications.
The goals of this text are the same as corresponding part of Web Development Coding Standards for PHP.
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.
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.
Using of tabs for identation is strongly advised, with tab's width set to 4 spaces.
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.
The same rules as described in corresponding section for PHP Coding Standards.
The same rules as described in corresponding section for PHP Coding Standards.
The following rules are obligatory for table names:
Incorrect
Photo -- upper cased char photocomments -- two words without any separator commentsPhotos -- camel cased
Correct
photos photo_comments
Columns are named taking into account following rules:
Incorrect
permission_group_id -- underscores permissions_permissionGroupId -- table prefix prmGrpId -- try to read this without context in year or something
Correct
permissionGroupId
Next rules should be considered for Primary Keys (PK) constraints:
id value for PK column names. That's the only case when you need to use table name as the prefix. E.g. for table users PK column is userId (singular and camel cased).PK prefixes of suffixes in column names.photo_tags table (links photos and tags table) contains two columns photoId and tagId and these two columns are composite PK.
PRIMARY indexes it's recommended to use pk_table_name format, but in the same time default one (PRIMARY) will do.Rules Foreign Keys (FK) constraints:
FK prefixes of suffixes in column names.fk_reference_table__referenced_table.
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.
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.
Incorrect
select * from table where date = now() Select * From table where date = now()
Correct
SELECT * FROM table WHERE date = NOW()
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:
SELECT clause, only use it if it's really necesseary.CROSS JOIN, only INNER and OUTER ones.SELECT clause unless you have actual reasons for this.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
There is no any strict rule for all other statements, but anyway please keep in mind #2.1 when writing them.