SlideShare a Scribd company logo
1 of 5
Download to read offline
© http://www.SQLAuthority.com                                                        V 1.0


SQL Server Database Coding Standards and Guidelines
http://www.SQLAuthority.com

Naming

Tables:             Rules: Pascal notation; end with an ‘s’
                    Examples: Products, Customers
                    Group related table names1

Stored Procs:       Rules: sp<App Name>_[<Group Name >_]<Action><table/logical instance>
                    Examples: spOrders_GetNewOrders, spProducts_UpdateProduct

Triggers:           Rules: TR_<TableName>_<action>
                    Examples: TR_Orders_UpdateProducts
                    Notes: The use of triggers is discouraged

Indexes:            Rules: IX_<TableName>_<columns separated by _>
                    Examples: IX_Products_ProductID

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:            If a column references another table’s column, name it <table name>ID
                    Example: The Customers table has an ID column
                              The Orders table should have a CustomerID column

General Rules:      Do not use spaces in the name of database objects
                    Do not use SQL keywords as the name of database objects
                            In cases where this is necessary, surround the
                            object name with brackets, such as [Year]
                    Do not prefix stored procedures with ‘sp_’2
                    Prefix table names with the owner name3

Structure

   •   Each table must have a primary key
           o In most cases it should be an IDENTITY column named ID
   •   Normalize data to third normal form
           o Do not compromise on performance to reach third normal form. Sometimes, a little de-
               normalization results in better performance.
   •   Do not use TEXT as a data type; use the maximum allowed characters of VARCHAR instead
   •   In VARCHAR data columns, do not default to NULL; use an empty string instead
   •   Columns with default values should not allow NULLs
   •   As much as possible, create stored procedures on the same database as the main tables they
       will be accessing

© http://www.sqlauthority.com
© http://www.SQLAuthority.com                                                           V 1.0


Formatting

  •   Use upper case for all SQL keywords
          o SELECT, INSERT, UPDATE, WHERE, AND, OR, LIKE, etc.
  •   Indent code to improve readability
  •   Comment code blocks that are not easily understandable
          o Use single-line comment markers(--)
          o Reserve multi-line comments (/*.. ..*/) for blocking out sections of code
  •   Use single quote characters to delimit strings.
          o Nest single quotes to express a single quote or apostrophe within a string
                     For example, SET @sExample = 'SQL''s Authority'
  •   Use parentheses to increase readability
          o WHERE (color=’red’ AND (size = 1 OR size = 2))
  •   Use BEGIN..END blocks only when multiple statements are present within a conditional code
      segment.
  •   Use one blank line to separate code sections.
  •   Use spaces so that expressions read like sentences.
          o fillfactor = 25, not fillfactor=25
  •   Format JOIN operations using indents
          o Also, use ANSI Joins instead of old style joins4
  •   Place SET statements before any executing code in the procedure.

Coding

  •   Optimize queries using the tools provided by SQL Server5
  •   Do not use SELECT *
  •   Return multiple result sets from one stored procedure to avoid trips from the application server
      to SQL server
  •   Avoid unnecessary use of temporary tables
          o Use 'Derived tables' or CTE (Common Table Expressions) wherever possible, as they
              perform better6
  •   Avoid using <> as a comparison operator
          o Use ID IN(1,3,4,5) instead of ID <> 2
  •   Use SET NOCOUNT ON at the beginning of stored procedures7
  •   Do not use cursors or application loops to do inserts8
          o Instead, use INSERT INTO
  •   Fully qualify tables and column names in JOINs
  •   Fully qualify all stored procedure and table references in stored procedures.
  •   Do not define default values for parameters.
          o If a default is needed, the front end will supply the value.
  •   Do not use the RECOMPILE option for stored procedures.
  •   Place all DECLARE statements before any other code in the procedure.
  •   Do not use column numbers in the ORDER BY clause.
  •   Do not use GOTO.
  •   Check the global variable @@ERROR immediately after executing a data manipulation statement
      (like INSERT/UPDATE/DELETE), so that you can rollback the transaction if an error occurs
          o Or use TRY/CATCH
  •   Do basic validations in the front-end itself during data entry
  •   Off-load tasks, like string manipulations, concatenations, row numbering, case conversions, type
      conversions etc., to the front-end applications if these operations are going to consume more
      CPU cycles on the database server
  •   Always use a column list in your INSERT statements.
          o This helps avoid problems when the table structure changes (like adding or dropping a
              column).

© http://www.sqlauthority.com
© http://www.SQLAuthority.com                                                             V 1.0


   •   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



© http://www.sqlauthority.com
© http://www.SQLAuthority.com                                                                V 1.0


5) Use the graphical execution plan in Query Analyzer or SHOWPLAN_TEXT or SHOWPLAN_ALL
commands to analyze your queries. Make sure your queries do an "Index seek" instead of an "Index
scan" or a "Table scan." A table scan or an index scan is a highly undesirable and should be avoided
where possible.

6) Consider the following query to find the second highest offer price from the Items table:

SELECT MAX(Price)
FROM Products
WHERE ID IN
(
SELECT TOP 2 ID
FROM Products
ORDER BY Price Desc
)

The same query can be re-written using a derived table, as shown below, and it performs generally
twice as fast as the above query:

SELECT MAX(Price)
FROM
(
SELECT TOP 2 Price
FROM Products
ORDER BY Price DESC
)

7) This suppresses messages like '(1 row(s) affected)' after executing INSERT, UPDATE, DELETE and
SELECT statements. Performance is improved due to the reduction of network traffic.

8) Try to avoid server side cursors as much as possible. Always stick to a 'set-based approach' instead
of a 'procedural approach' for accessing and manipulating data. Cursors can often be avoided by using
SELECT statements instead. If a cursor is unavoidable, use a WHILE loop instead. For a WHILE loop to
replace a cursor, however, you need a column (primary key or unique key) to identify each row
uniquely.

9) You cannot directly write or update text data using the INSERT or UPDATE statements. Instead, you
have to use special statements like READTEXT, WRITETEXT and UPDATETEXT. So, if you don't have to
store more than 8KB of text, use the CHAR(8000) or VARCHAR(8000) datatype instead.

10) Dynamic SQL tends to be slower than static SQL, as SQL Server must generate an execution plan at
runtime. IF and CASE statements come in handy to avoid dynamic SQL.

11) This helps to avoid deadlocks. Other things to keep in mind to avoid deadlocks are:

   •   Keep transactions as short as possible.
   •   Touch the minimum amount of data possible during a transaction.
   •   Never wait for user input in the middle of a transaction.
   •   Do not use higher level locking hints or restrictive isolation levels unless they are absolutely
       needed.

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.

© http://www.sqlauthority.com
© http://www.SQLAuthority.com                                                        V 1.0



13) IF EXISTS (SELECT 1 FROM Products WHERE ID = 50)
    Instead Of:
    IF EXISTS (SELECT COUNT(ID) FROM Products WHERE ID = 50)

14) CHAR(100), when NULL, will consume 100 bytes, resulting in space wastage. Preferably, use
VARCHAR(100) in this situation. Variable-length columns have very little processing overhead
compared with fixed-length columns.




© http://www.sqlauthority.com

More Related Content

What's hot

Subqueries, Backups, Users and Privileges
Subqueries, Backups, Users and PrivilegesSubqueries, Backups, Users and Privileges
Subqueries, Backups, Users and Privileges
Ashwin Dinoriya
 
DDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and JoinsDDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and Joins
Ashwin Dinoriya
 
Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007
paulguerin
 

What's hot (20)

New T-SQL Features in SQL Server 2012
New T-SQL Features in SQL Server 2012 New T-SQL Features in SQL Server 2012
New T-SQL Features in SQL Server 2012
 
Module 3
Module 3Module 3
Module 3
 
Sql
SqlSql
Sql
 
Subqueries, Backups, Users and Privileges
Subqueries, Backups, Users and PrivilegesSubqueries, Backups, Users and Privileges
Subqueries, Backups, Users and Privileges
 
SQL Views
SQL ViewsSQL Views
SQL Views
 
Lab1 select statement
Lab1 select statementLab1 select statement
Lab1 select statement
 
Ch05
Ch05Ch05
Ch05
 
Ch04
Ch04Ch04
Ch04
 
SQL- Introduction to PL/SQL
SQL- Introduction to  PL/SQLSQL- Introduction to  PL/SQL
SQL- Introduction to PL/SQL
 
Partitioning Tables and Indexing Them --- Article
Partitioning Tables and Indexing Them --- ArticlePartitioning Tables and Indexing Them --- Article
Partitioning Tables and Indexing Them --- Article
 
DDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and JoinsDDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and Joins
 
Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007
 
Oracle SQL Advanced
Oracle SQL AdvancedOracle SQL Advanced
Oracle SQL Advanced
 
Mysql cheatsheet
Mysql cheatsheetMysql cheatsheet
Mysql cheatsheet
 
Partitioning tables and indexing them
Partitioning tables and indexing them Partitioning tables and indexing them
Partitioning tables and indexing them
 
Query
QueryQuery
Query
 
SQL
SQLSQL
SQL
 
Database Management System 1
Database Management System 1Database Management System 1
Database Management System 1
 
Hira
HiraHira
Hira
 
Oracle SQL Basics
Oracle SQL BasicsOracle SQL Basics
Oracle SQL Basics
 

Viewers also liked (7)

Webinar: Top 3 Reasons To Start Using Containers For MS-SQL
Webinar: Top 3 Reasons To Start Using Containers For MS-SQLWebinar: Top 3 Reasons To Start Using Containers For MS-SQL
Webinar: Top 3 Reasons To Start Using Containers For MS-SQL
 
Review of SQL
Review of SQLReview of SQL
Review of SQL
 
Nitish 007
Nitish 007Nitish 007
Nitish 007
 
Stored procedures
Stored proceduresStored procedures
Stored procedures
 
Introduction to ArcGIS 10.1
Introduction to ArcGIS 10.1Introduction to ArcGIS 10.1
Introduction to ArcGIS 10.1
 
SQL Basics
SQL BasicsSQL Basics
SQL Basics
 
Sql ppt
Sql pptSql ppt
Sql ppt
 

Similar to Sql coding-standard-sqlserver

Database development coding standards
Database development coding standardsDatabase development coding standards
Database development coding standards
Alessandro Baratella
 
Mohan Testing
Mohan TestingMohan Testing
Mohan Testing
smittal81
 
SQL.pptx for the begineers and good know
SQL.pptx for the begineers and good knowSQL.pptx for the begineers and good know
SQL.pptx for the begineers and good know
PavithSingh
 
02 database oprimization - improving sql performance - ent-db
02  database oprimization - improving sql performance - ent-db02  database oprimization - improving sql performance - ent-db
02 database oprimization - improving sql performance - ent-db
uncleRhyme
 
SQL: Structured Query Language
SQL: Structured Query LanguageSQL: Structured Query Language
SQL: Structured Query Language
Rohit Bisht
 

Similar to Sql coding-standard-sqlserver (20)

Database development coding standards
Database development coding standardsDatabase development coding standards
Database development coding standards
 
Pl sql best practices document
Pl sql best practices documentPl sql best practices document
Pl sql best practices document
 
MS SQL Server
MS SQL ServerMS SQL Server
MS SQL Server
 
Mohan Testing
Mohan TestingMohan Testing
Mohan Testing
 
Basics on SQL queries
Basics on SQL queriesBasics on SQL queries
Basics on SQL queries
 
SQL.pptx for the begineers and good know
SQL.pptx for the begineers and good knowSQL.pptx for the begineers and good know
SQL.pptx for the begineers and good know
 
02 database oprimization - improving sql performance - ent-db
02  database oprimization - improving sql performance - ent-db02  database oprimization - improving sql performance - ent-db
02 database oprimization - improving sql performance - ent-db
 
Oracle notes
Oracle notesOracle notes
Oracle notes
 
Dbms
DbmsDbms
Dbms
 
SQL: Structured Query Language
SQL: Structured Query LanguageSQL: Structured Query Language
SQL: Structured Query Language
 
SQL Commands
SQL Commands SQL Commands
SQL Commands
 
Sql performance vesl technologies
Sql performance vesl technologiesSql performance vesl technologies
Sql performance vesl technologies
 
How to Fine-Tune Performance Using Amazon Redshift
How to Fine-Tune Performance Using Amazon RedshiftHow to Fine-Tune Performance Using Amazon Redshift
How to Fine-Tune Performance Using Amazon Redshift
 
UNIT2.ppt
UNIT2.pptUNIT2.ppt
UNIT2.ppt
 
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
 
Sql db optimization
Sql db optimizationSql db optimization
Sql db optimization
 
Top 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tipsTop 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tips
 
Sql commands
Sql commandsSql commands
Sql commands
 
SQL Inteoduction to SQL manipulating of data
SQL Inteoduction to SQL manipulating of data   SQL Inteoduction to SQL manipulating of data
SQL Inteoduction to SQL manipulating of data
 
SQL Fundamentals - Lecture 2
SQL Fundamentals - Lecture 2SQL Fundamentals - Lecture 2
SQL Fundamentals - Lecture 2
 

Recently uploaded

Famous Kala Jadu, Black magic expert in UK and Kala ilam expert in Saudi Arab...
Famous Kala Jadu, Black magic expert in UK and Kala ilam expert in Saudi Arab...Famous Kala Jadu, Black magic expert in UK and Kala ilam expert in Saudi Arab...
Famous Kala Jadu, Black magic expert in UK and Kala ilam expert in Saudi Arab...
baharayali
 
Famous Kala Jadu, Kala ilam specialist in USA and Bangali Amil baba in Saudi ...
Famous Kala Jadu, Kala ilam specialist in USA and Bangali Amil baba in Saudi ...Famous Kala Jadu, Kala ilam specialist in USA and Bangali Amil baba in Saudi ...
Famous Kala Jadu, Kala ilam specialist in USA and Bangali Amil baba in Saudi ...
baharayali
 
Real Kala Jadu, Black magic specialist in Lahore and Kala ilam expert in kara...
Real Kala Jadu, Black magic specialist in Lahore and Kala ilam expert in kara...Real Kala Jadu, Black magic specialist in Lahore and Kala ilam expert in kara...
Real Kala Jadu, Black magic specialist in Lahore and Kala ilam expert in kara...
baharayali
 
Professional Amil baba, Kala jadu specialist in Multan and Kala ilam speciali...
Professional Amil baba, Kala jadu specialist in Multan and Kala ilam speciali...Professional Amil baba, Kala jadu specialist in Multan and Kala ilam speciali...
Professional Amil baba, Kala jadu specialist in Multan and Kala ilam speciali...
makhmalhalaaay
 
Professional Amil baba, Black magic specialist in Lahore and Kala ilam expert...
Professional Amil baba, Black magic specialist in Lahore and Kala ilam expert...Professional Amil baba, Black magic specialist in Lahore and Kala ilam expert...
Professional Amil baba, Black magic specialist in Lahore and Kala ilam expert...
makhmalhalaaay
 
Popular Kala Jadu, Black magic specialist in Sialkot and Kala ilam specialist...
Popular Kala Jadu, Black magic specialist in Sialkot and Kala ilam specialist...Popular Kala Jadu, Black magic specialist in Sialkot and Kala ilam specialist...
Popular Kala Jadu, Black magic specialist in Sialkot and Kala ilam specialist...
baharayali
 

Recently uploaded (20)

Genesis 1:10 || Meditate the Scripture daily verse by verse
Genesis 1:10  ||  Meditate the Scripture daily verse by verseGenesis 1:10  ||  Meditate the Scripture daily verse by verse
Genesis 1:10 || Meditate the Scripture daily verse by verse
 
The_Chronological_Life_of_Christ_Part_99_Words_and_Works
The_Chronological_Life_of_Christ_Part_99_Words_and_WorksThe_Chronological_Life_of_Christ_Part_99_Words_and_Works
The_Chronological_Life_of_Christ_Part_99_Words_and_Works
 
Famous Kala Jadu, Black magic expert in UK and Kala ilam expert in Saudi Arab...
Famous Kala Jadu, Black magic expert in UK and Kala ilam expert in Saudi Arab...Famous Kala Jadu, Black magic expert in UK and Kala ilam expert in Saudi Arab...
Famous Kala Jadu, Black magic expert in UK and Kala ilam expert in Saudi Arab...
 
Human Design Gates Cheat Sheet | Kabastro.com
Human Design Gates Cheat Sheet | Kabastro.comHuman Design Gates Cheat Sheet | Kabastro.com
Human Design Gates Cheat Sheet | Kabastro.com
 
"The Magnificent Surah Rahman: PDF Version"
"The Magnificent Surah Rahman: PDF Version""The Magnificent Surah Rahman: PDF Version"
"The Magnificent Surah Rahman: PDF Version"
 
Famous Kala Jadu, Kala ilam specialist in USA and Bangali Amil baba in Saudi ...
Famous Kala Jadu, Kala ilam specialist in USA and Bangali Amil baba in Saudi ...Famous Kala Jadu, Kala ilam specialist in USA and Bangali Amil baba in Saudi ...
Famous Kala Jadu, Kala ilam specialist in USA and Bangali Amil baba in Saudi ...
 
Legends of the Light v2.pdf xxxxxxxxxxxxx
Legends of the Light v2.pdf xxxxxxxxxxxxxLegends of the Light v2.pdf xxxxxxxxxxxxx
Legends of the Light v2.pdf xxxxxxxxxxxxx
 
St. Louise de Marillac and Poor Children
St. Louise de Marillac and Poor ChildrenSt. Louise de Marillac and Poor Children
St. Louise de Marillac and Poor Children
 
Lesson 6 - Our Spiritual Weapons - SBS.pptx
Lesson 6 - Our Spiritual Weapons - SBS.pptxLesson 6 - Our Spiritual Weapons - SBS.pptx
Lesson 6 - Our Spiritual Weapons - SBS.pptx
 
Real Kala Jadu, Black magic specialist in Lahore and Kala ilam expert in kara...
Real Kala Jadu, Black magic specialist in Lahore and Kala ilam expert in kara...Real Kala Jadu, Black magic specialist in Lahore and Kala ilam expert in kara...
Real Kala Jadu, Black magic specialist in Lahore and Kala ilam expert in kara...
 
St. Louise de Marillac and Abandoned Children
St. Louise de Marillac and Abandoned ChildrenSt. Louise de Marillac and Abandoned Children
St. Louise de Marillac and Abandoned Children
 
famous No 1 astrologer / Best No 1 Amil baba in UK, Australia, Germany, USA, ...
famous No 1 astrologer / Best No 1 Amil baba in UK, Australia, Germany, USA, ...famous No 1 astrologer / Best No 1 Amil baba in UK, Australia, Germany, USA, ...
famous No 1 astrologer / Best No 1 Amil baba in UK, Australia, Germany, USA, ...
 
A Spiritual Guide To Truth v10.pdf xxxxxxx
A Spiritual Guide To Truth v10.pdf xxxxxxxA Spiritual Guide To Truth v10.pdf xxxxxxx
A Spiritual Guide To Truth v10.pdf xxxxxxx
 
Professional Amil baba, Kala jadu specialist in Multan and Kala ilam speciali...
Professional Amil baba, Kala jadu specialist in Multan and Kala ilam speciali...Professional Amil baba, Kala jadu specialist in Multan and Kala ilam speciali...
Professional Amil baba, Kala jadu specialist in Multan and Kala ilam speciali...
 
Professional Amil baba, Black magic specialist in Lahore and Kala ilam expert...
Professional Amil baba, Black magic specialist in Lahore and Kala ilam expert...Professional Amil baba, Black magic specialist in Lahore and Kala ilam expert...
Professional Amil baba, Black magic specialist in Lahore and Kala ilam expert...
 
Flores de Mayo-history and origin we need to understand
Flores de Mayo-history and origin we need to understandFlores de Mayo-history and origin we need to understand
Flores de Mayo-history and origin we need to understand
 
Hire Best Next Js Developer For Your Project
Hire Best Next Js Developer For Your ProjectHire Best Next Js Developer For Your Project
Hire Best Next Js Developer For Your Project
 
Popular Kala Jadu, Black magic specialist in Sialkot and Kala ilam specialist...
Popular Kala Jadu, Black magic specialist in Sialkot and Kala ilam specialist...Popular Kala Jadu, Black magic specialist in Sialkot and Kala ilam specialist...
Popular Kala Jadu, Black magic specialist in Sialkot and Kala ilam specialist...
 
St. Louise de Marillac and Care of the Sick Poor
St. Louise de Marillac and Care of the Sick PoorSt. Louise de Marillac and Care of the Sick Poor
St. Louise de Marillac and Care of the Sick Poor
 
Amil baba in Lahore /Amil baba in Karachi /Amil baba in Pakistan
Amil baba in Lahore /Amil baba in Karachi /Amil baba in PakistanAmil baba in Lahore /Amil baba in Karachi /Amil baba in Pakistan
Amil baba in Lahore /Amil baba in Karachi /Amil baba in Pakistan
 

Sql coding-standard-sqlserver

  • 1. © http://www.SQLAuthority.com V 1.0 SQL Server Database Coding Standards and Guidelines http://www.SQLAuthority.com Naming Tables: Rules: Pascal notation; end with an ‘s’ Examples: Products, Customers Group related table names1 Stored Procs: Rules: sp<App Name>_[<Group Name >_]<Action><table/logical instance> Examples: spOrders_GetNewOrders, spProducts_UpdateProduct Triggers: Rules: TR_<TableName>_<action> Examples: TR_Orders_UpdateProducts Notes: The use of triggers is discouraged Indexes: Rules: IX_<TableName>_<columns separated by _> Examples: IX_Products_ProductID 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: If a column references another table’s column, name it <table name>ID Example: The Customers table has an ID column The Orders table should have a CustomerID column General Rules: Do not use spaces in the name of database objects Do not use SQL keywords as the name of database objects In cases where this is necessary, surround the object name with brackets, such as [Year] Do not prefix stored procedures with ‘sp_’2 Prefix table names with the owner name3 Structure • Each table must have a primary key o In most cases it should be an IDENTITY column named ID • Normalize data to third normal form o Do not compromise on performance to reach third normal form. Sometimes, a little de- normalization results in better performance. • Do not use TEXT as a data type; use the maximum allowed characters of VARCHAR instead • In VARCHAR data columns, do not default to NULL; use an empty string instead • Columns with default values should not allow NULLs • As much as possible, create stored procedures on the same database as the main tables they will be accessing © http://www.sqlauthority.com
  • 2. © http://www.SQLAuthority.com V 1.0 Formatting • Use upper case for all SQL keywords o SELECT, INSERT, UPDATE, WHERE, AND, OR, LIKE, etc. • Indent code to improve readability • Comment code blocks that are not easily understandable o Use single-line comment markers(--) o Reserve multi-line comments (/*.. ..*/) for blocking out sections of code • Use single quote characters to delimit strings. o Nest single quotes to express a single quote or apostrophe within a string For example, SET @sExample = 'SQL''s Authority' • Use parentheses to increase readability o WHERE (color=’red’ AND (size = 1 OR size = 2)) • Use BEGIN..END blocks only when multiple statements are present within a conditional code segment. • Use one blank line to separate code sections. • Use spaces so that expressions read like sentences. o fillfactor = 25, not fillfactor=25 • Format JOIN operations using indents o Also, use ANSI Joins instead of old style joins4 • Place SET statements before any executing code in the procedure. Coding • Optimize queries using the tools provided by SQL Server5 • Do not use SELECT * • Return multiple result sets from one stored procedure to avoid trips from the application server to SQL server • Avoid unnecessary use of temporary tables o Use 'Derived tables' or CTE (Common Table Expressions) wherever possible, as they perform better6 • Avoid using <> as a comparison operator o Use ID IN(1,3,4,5) instead of ID <> 2 • Use SET NOCOUNT ON at the beginning of stored procedures7 • Do not use cursors or application loops to do inserts8 o Instead, use INSERT INTO • Fully qualify tables and column names in JOINs • Fully qualify all stored procedure and table references in stored procedures. • Do not define default values for parameters. o If a default is needed, the front end will supply the value. • Do not use the RECOMPILE option for stored procedures. • Place all DECLARE statements before any other code in the procedure. • Do not use column numbers in the ORDER BY clause. • Do not use GOTO. • Check the global variable @@ERROR immediately after executing a data manipulation statement (like INSERT/UPDATE/DELETE), so that you can rollback the transaction if an error occurs o Or use TRY/CATCH • Do basic validations in the front-end itself during data entry • Off-load tasks, like string manipulations, concatenations, row numbering, case conversions, type conversions etc., to the front-end applications if these operations are going to consume more CPU cycles on the database server • Always use a column list in your INSERT statements. o This helps avoid problems when the table structure changes (like adding or dropping a column). © http://www.sqlauthority.com
  • 3. © http://www.SQLAuthority.com V 1.0 • 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 © http://www.sqlauthority.com
  • 4. © http://www.SQLAuthority.com V 1.0 5) Use the graphical execution plan in Query Analyzer or SHOWPLAN_TEXT or SHOWPLAN_ALL commands to analyze your queries. Make sure your queries do an "Index seek" instead of an "Index scan" or a "Table scan." A table scan or an index scan is a highly undesirable and should be avoided where possible. 6) Consider the following query to find the second highest offer price from the Items table: SELECT MAX(Price) FROM Products WHERE ID IN ( SELECT TOP 2 ID FROM Products ORDER BY Price Desc ) The same query can be re-written using a derived table, as shown below, and it performs generally twice as fast as the above query: SELECT MAX(Price) FROM ( SELECT TOP 2 Price FROM Products ORDER BY Price DESC ) 7) This suppresses messages like '(1 row(s) affected)' after executing INSERT, UPDATE, DELETE and SELECT statements. Performance is improved due to the reduction of network traffic. 8) Try to avoid server side cursors as much as possible. Always stick to a 'set-based approach' instead of a 'procedural approach' for accessing and manipulating data. Cursors can often be avoided by using SELECT statements instead. If a cursor is unavoidable, use a WHILE loop instead. For a WHILE loop to replace a cursor, however, you need a column (primary key or unique key) to identify each row uniquely. 9) You cannot directly write or update text data using the INSERT or UPDATE statements. Instead, you have to use special statements like READTEXT, WRITETEXT and UPDATETEXT. So, if you don't have to store more than 8KB of text, use the CHAR(8000) or VARCHAR(8000) datatype instead. 10) Dynamic SQL tends to be slower than static SQL, as SQL Server must generate an execution plan at runtime. IF and CASE statements come in handy to avoid dynamic SQL. 11) This helps to avoid deadlocks. Other things to keep in mind to avoid deadlocks are: • Keep transactions as short as possible. • Touch the minimum amount of data possible during a transaction. • Never wait for user input in the middle of a transaction. • Do not use higher level locking hints or restrictive isolation levels unless they are absolutely needed. 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. © http://www.sqlauthority.com
  • 5. © http://www.SQLAuthority.com V 1.0 13) IF EXISTS (SELECT 1 FROM Products WHERE ID = 50) Instead Of: IF EXISTS (SELECT COUNT(ID) FROM Products WHERE ID = 50) 14) CHAR(100), when NULL, will consume 100 bytes, resulting in space wastage. Preferably, use VARCHAR(100) in this situation. Variable-length columns have very little processing overhead compared with fixed-length columns. © http://www.sqlauthority.com