SQL Coding Standards
SQL Coding Standards
h
Naming
Tables:
Stored Procs:
Triggers:
Rules: TR_<TableName>_<action>
Examples: TR_Orders_UpdateProducts
Notes: The use of triggers is discouraged
Indexes:
Primary Keys:
Rules: PK_<TableName>
Examples: PK_Products
Foreign Keys:
Rules: FK_<TableName1>_<TableName2>
Example: FK_Products_Orderss
Defaults:
Rules: DF_<TableName>_<ColumnName>
Example: DF_Products_Quantity
Columns:
General Rules:
Structure
Formatting
Coding
Minimize the use of NULLs, as they often confuse front-end applications, unless the applications
are coded intelligently to eliminate NULLs or convert the NULLs into some other form.
o Any expression that deals with NULL results in a NULL output.
o The ISNULL and COALESCE functions are helpful in dealing with NULL values.
Do not use the identitycol or rowguidcol.
Avoid the use of cross joins, if possible.
When executing an UPDATE or DELETE statement, use the primary key in the WHERE condition,
if possible. This reduces error possibilities.
Avoid using TEXT or NTEXT datatypes for storing large textual data.9
o Use the maximum allowed characters of VARCHAR instead
Avoid dynamic SQL statements as much as possible.10
Access tables in the same order in your stored procedures and triggers consistently.11
Do not call functions repeatedly within your stored procedures, triggers, functions and batches.12
Default constraints must be defined at the column level.
Avoid wild-card characters at the beginning of a word while searching using the LIKE keyword,
as these results in an index scan, which defeats the purpose of an index.
Define all constraints, other than defaults, at the table level.
When a result set is not needed, use syntax that does not return a result set.13
Avoid rules, database level defaults that must be bound or user-defined data types. While these
are legitimate database constructs, opt for constraints and column defaults to hold the database
consistent for development and conversion coding.
Constraints that apply to more than one column must be defined at the table level.
Use the CHAR data type for a column only when the column is non-nullable.14
Do not use white space in identifiers.
The RETURN statement is meant for returning the execution status only, but not data.
Reference:
1) Group related table names:
Products_USA
Products_India
Products_Mexico
2) The prefix sp_ is reserved for system stored procedures that ship with SQL Server. Whenever SQL
Server encounters a procedure name starting with sp_, it first tries to locate the procedure in the
master database, then it looks for any qualifiers (database, owner) provided, then it tries dbo as the
owner. Time spent locating the stored procedure can be saved by avoiding the "sp_" prefix.
3) This improves readability and avoids unnecessary confusion. Microsoft SQL Server Books Online
states that qualifying table names with owner names helps in execution plan reuse, further boosting
performance.
4)
False code:
SELECT *
FROM Table1, Table2
WHERE Table1.d = Table2.c
True code:
SELECT *
FROM Table1
INNER JOIN Table2 ON Table1.d = Table2.c
12) You might need the length of a string variable in many places of your procedure, but don't call the
LEN function whenever it's needed. Instead, call the LEN function once and store the result in a
variable for later use.