e
op
l
e
T-SQL
v
e
d
r
Coding Standards
o
f And Best
Practices
Gogula G. Aryalingam http://dbantics.wordpress.com
Rationale
Witness to totally messed up databases
Perspectives of a lot of developers regarding
system)
High priority to the client application
Back-end nature of SQL Server (the database
system)
Any more reasons why you do not like SQL
Server?
Developers:
What Do You Do with SQL
Server?
understand
Increases the fluency in identifying objects
Do you use naming conventions?
practice anymore
For identity column names use table name suffixed
with ID
Suggestions?
Insert: InsertCustomers
Update: UpdateCourses
References:
http://vyaskn.tripod.com/object_naming.htm
http://www.cms.hhs.gov/dbadmin/downloads/sqlserverstandardsan
dguildelines.pdf
Horrifying
Take a look at this piece of code:
column names
Use table aliases using
Use tabs instead white spaces
Suggestions?
ml
possible
Use Unicode data types only if it is needed
Make sure data integrity is applied
Primary keys, Foreign keys, Check, Default and
Unique constraints
Keep in mind the 8060 B row size rule
Choose
varchar(max), nvarchar(max)
text, ntext
and
image
and
varbinary(max)
over
Querying Tips
Rather than SELECT * FROM
to increase on performance
Querying Tips
Avoid cursors as much as possible
Alternatively use:
Set based approach to update or insert data from one
tables to another
Tables variable and While loop (suited for small result
sets)
prefixed
FROM [HumanResources].[Employee ]
Querying Tips
Prefix column names with table name or alias
SELECT Employee.[Name],
Contact.[Address]
FROM
Avoid using functions on columns in the WHERE
clause
WHERE UPPER([Name]) = BARBIE
Querying Tips
Avoid dynamic SQL
Try to find alternatives that do not constitute of dynamic SQL.
If at all using dynamic SQL, use sp_executesql instead of
EXECUTE (EXEC)
clause:
INSERT INTO ([Name], [Age], [Address])
Querying Tips
Place all data access tasks in SQL Server
itself.
Avoid queries and data manipulations on
the client app/business tier.
Use stored procedures
Reference:
http://blog.sqlauthority.com/2008/09/25/sql-serve
r-guidelines-and-coding-standards/
Suggestions?
Trigger Mania
Trigger Mania
Perform all referential and domain integrity rules
using constraints
Avoid using triggers for this purpose (poor performance)
Use only if cannot be implemented by constraints
custom validations
Trigger Mania
When writing triggers
Write for a recordset rather than for a single
record
Suggestions?
Q&A
Q1
You have an SQL Server 2008 database.
Q2
You use an SQL Server 2005 database. You
Q3
What alternatives can be used for cursors?
Set based operations
Table variables and WHILE loop (for small data
sets)
Q4
Name some Best Practices for triggers.
Avoid using business functionality within
triggers
Code for record sets rather than single
records
Best suited for auditing and other custom
tasks
Thank you
E-mail: gogulaa@gmail.com
Twitter: http://twitter.com/gogula
Blog:
Web:
http://dbantics.wordpress.com
http://sqlserveruniverse.com