Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
56 views

Database Programming Standards

The document provides database programming standards and guidelines for naming conventions, structures, formatting, coding practices and performance tips. Key points include: - Tables, columns, indexes and other database objects should be named following specific conventions with prefixes identifying the type and module. - Data should be normalized to third normal form and data types like TEXT avoided when possible. - Code should be properly formatted and indented for readability with comments used. - Coding best practices like parameterizing queries, avoiding cursors and temporary tables, and basic validation in the frontend should be followed. - Performance can be optimized by using appropriate joins over subqueries, analyzing execution plans, leveraging derived tables and limiting transactions.

Uploaded by

Sabari Nathan
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

Database Programming Standards

The document provides database programming standards and guidelines for naming conventions, structures, formatting, coding practices and performance tips. Key points include: - Tables, columns, indexes and other database objects should be named following specific conventions with prefixes identifying the type and module. - Data should be normalized to third normal form and data types like TEXT avoided when possible. - Code should be properly formatted and indented for readability with comments used. - Coding best practices like parameterizing queries, avoiding cursors and temporary tables, and basic validation in the frontend should be followed. - Performance can be optimized by using appropriate joins over subqueries, analyzing execution plans, leveraging derived tables and limiting transactions.

Uploaded by

Sabari Nathan
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 10

DATABASE PROGRAMMING STANDARDS

DATABASE PROGRAMMING STANDARDS


Naming Conventions:
Tables:
Each table will have a 3-letter Module nae under!core 3 letter t"#e $M!t %or Ma!ter& Trn %or Tran!action& T#
%or Te#orar" and Stc %or Static'& which will be uni(ue to identi%" the Module and table t"#e)

For example:
*or +lient Ma!ter the table nae will be Gen,M!t,+lient
The %ir!t 3 character! -Gen. i! to identi%" Module nae and M!t u!ed to identi%" table t"#e)
I% the #ro/ect0#roduct ha! !in1le odule then directl" !tart with table t"#e)
For example:
*or +lient Ma!ter the table nae will be M!t,+lient
Columns (fiels!:
*ield nae !hould re%lect the content to be !tored)
For example2
3) +lient+ode
4) 5nit+ode

+lient+ode i! the %ield nae to re%er client6! code toward! all the odule! and table!)
Ine"es:
The %ollowin1 rule !hould be %ollowed %or nain1 an inde7
Rule2 ID8,9TableNae:,9colun! !e#arated b" ,:
For examples:
Id7,Product!,ProductID
P#ima#$ %e$s:
The %ollowin1 rule ha! to be %ollowed %or nain1 a #riar" ;e"
Rule2 P<,9TableNae:

For examples:
P;,Gen,M!t,+lient
&o#eign %e$s: The %ollowin1 rule ha! to be %ollowed %or nain1 %orei1n ;e"
Rule2 *<,9TableNae3:,9TableNae4:

For example:
*;,+lient,Account!
'a#iables:
=ariable! !hould be #re%i7ed with >v,6
For example:
v,Sli#
Constants:
*or con!tant! whole word !hould be in ca#! e7ce#t #re%i7 c,)
For example:

c,S?IP
Cu#so#s:
+ur!or! !hould be #re%i7ed with @ cur, @
For example:

+ur,+ontract
Pa#amete#s:
Paraeter! !hould be #re%i7ed with @ #, @
For example:

#,+lient
P#o(eu#es:
Procedure! !hould be #re%i7ed with @9Module Nae:,Pr, @
For example:
Gen,Pr,Branch,*etch
Ahere Gen indicate! Module nae and Pr re%er! to #rocedure)

&un(tions:
*unction! !hould be #re%i7ed with @ 9Module Nae:,*n,@

For example:

Der,*n,+alculate,Bro;era1e
T#igge#s:
Tri11er! !hould be #re%i7ed with @ 9Module Nae:,T1,@
For example:
Der,T1,In!ert,Trade
Pa()age:
Pac;a1e !hould be #re%i7ed with @9Module Nae:,P;, @
For example:
Der,P;1,Buer",?oo;u#
'ie*s:
=iew! !hould be #re%i7ed with @ 9Module Nae:,=w,@
For example:
Der,=w,+ontract,Detail!
Mate#iali+e 'ie*s:
MaterialiCed =iew! !hould be #re%i7ed with @ 9Module Nae:,Mv,@
For example:
Der,Mv,+ontract,Detail!
Se,uen(es:
Se(uence! !hould be #re%i7ed with >9Module Nae:,Se(,6
For example:
Der,Se(,Ban;,+ode
S$non$ms:
S"non"! !hould be #re%i7ed with @ 9Module Nae:,S"n, @
For example:
Der,S"n,Aor;!;ill
Summa#$:
OB-ECT E.AMP/E
Table! M!t,+lient
+olun! +lient+ode
Inde7e! Id7,Product!,ProductID
Priar" <e"! P;,Gen,M!t,+lient
*orei1n <e"! *;,+lient,Account!
=ariable! v,Sli#
+on!tant! c,S?IP
+ur!or! +ur,+ontract
Paraeter! #,+lient
Procedure! Gen,Pr,Branch,*etch
*unction! Der,*n,+alculate,Bro;era1e
Tri11er! Der,T1,In!ert,Trade
Pac;a1e! Der,P;1,Buer",?oo;u#
=iew! Der,=w,+ontract,Detail!
MaterialiCed =iew! Der,Mv,+ontract,Detail!
Se(uence! Der,Se(,Ban;,+ode
S"non"! Der,S"n,Aor;!;ill
Gene#al Stana#s to be follo*e u#ing atabase 0#og#amming:
Do not u!e !#ace! in the nae o% databa!e ob/ect!
Do not u!e SB? ;e"word! a! the nae o% databa!e ob/ect!
In ca!e! where thi! i! nece!!ar"& !urround the ob/ect nae with brac;et!& !uch a! DEearF
Pre%i7 table nae! with the owner nae $e)1) dbo)9Table Nae:'

St#u(tu#e
Normalize data to third normal form (Only depending on the project requirement).
Do not use TEXT as a data type use the ma!imum allo"ed characters of #$%&'$% instead
"hene(er it is possi)le.
*n #$%&'$% data columns+ do not default to N,-- use an empty string instead as much as
possi)le+ create stored procedures on the same data)ase as the main ta)les they "ill )e
accessing.

&o#matting
,se upper case for all ./- 0ey"ords
o .E-E&T+ *N.E%T+ ,1D$TE+ 2'E%E+ $ND+ O%+ -*3E+ etc.
*ndent code to impro(e reada)ility
&omment code )loc0s that are not easily understanda)le
o ,se single4line comment mar0ers(44)
o %eser(e multi4line comments (56.. ..65) for )loc0ing out sections of code
,se single quote characters to delimit strings.
o Nest single quotes to e!press a single quote or apostrophe "ithin a string
,se parentheses to increase reada)ility
o 2'E%E (color78red8 $ND (size 7 9 O% size 7 :))
,se ;E<*N..END )loc0s only "hen multiple statements are present "ithin a conditional code
segment.
,se one )lan0 line to separate code sections.
,se spaces so that e!pressions read li0e sentences.
=ormat >O*N operations using indents
1lace .ET statements )efore any e!ecuting code in the procedure.
Coing:
Optimize queries using the tools pro(ided )y ./- .er(er "hene(er necessary
Do not use .E-E&T 6
%eturn multiple result sets from one stored procedure to a(oid trips from the
application ser(er to ./- ser(er
$(oid unnecessary use of temporary ta)les
o ,se ?Deri(ed ta)les? or &TE (&ommon Ta)le E!pressions) "here(er possi)le+ as they
perform )etter
$(oid using @A as a comparison operator
,se .ET NO&O,NT ON at the )eginning of stored procedures
$(oid using cursors or application loops to do inserts
o *nstead+ use *N.E%T *NTO
=ully qualify ta)les and column names in >O*Ns
=ully qualify all stored procedure and ta)le references in stored procedures. ,se appropriate
aliases needed
Do not define default (alues for parameters.
o *f a default is needed+ the front end "ill supply the (alue.
Do not use the %E&OB1*-E option for stored procedures.
1lace all DE&-$%E statements )efore any other code in the procedure.
Do not use column num)ers in the O%DE% ;C clause.
Do not use <OTO.
&hec0 the glo)al (aria)le DDE%%O% immediately after e!ecuting a data manipulation statement
(li0e *N.E%T5,1D$TE5DE-ETE)+ so that you can roll)ac0 the transaction if an error occurs
o Or use T%C5&$T&'
Do )asic (alidations in the front4end itself during data entry
Off4load tas0s+ li0e string manipulations+ concatenations+ ro" num)ering+ case con(ersions+ type
con(ersions etc.+ to the front4end applications if these operations are going to consume more &1,
cycles on the data)ase ser(er
$l"ays use a column list in your *N.E%T statements.
o This helps a(oid pro)lems "hen the ta)le structure changes (li0e adding or
dropping a column).
Binimize the use of N,--s+ as they often confuse front4end applications+ unless the applications
are coded intelligently to eliminate N,--s or con(ert the N,--s into some other form.
o $ny e!pression that deals "ith N,-- results in a N,-- output.
o The *.N,-- and &O$-E.&E functions are helpful in dealing "ith N,--
(alues.
Do not use the identitycol or ro"guidcol unless it is una(oida)le.
$(oid the use of cross joins+ if possi)le.
2hen e!ecuting an ,1D$TE or DE-ETE statement+ use the primary 0ey in the
2'E%E condition+ if possi)le. This reduces error possi)ilities.
$(oid dynamic ./- statements as much as possi)le.
$ccess ta)les in the same order in your stored procedures and triggers consistently
Do not call functions repeatedly "ithin your stored procedures+ triggers+ functions and )atches.
$(oid "ild4card characters at the )eginning of a "ord "hile searching using the -*3E 0ey"ord+
as these results in an inde! scan+ "hich defeats the purpose of an inde!. ,se the &'$% data type
for a column only "hen the column is non4nulla)le.
Do not use "hite space in identifiers.
The %ET,%N statement is meant for returning the e!ecution status only+ )ut not data.
Pe#fo#man(e Ti0s:
Ba0e use of joins rather than su) queries+ union or intersection in DB- statements.
,se the graphical e!ecution plan in /uery $nalyzer or .'O21-$NETEXT or .'O21-$NE$--
commands to analyze your queries. Ba0e sure your queries do an F*nde! see0F instead of an
F*nde! scanF or a FTa)le scanF. Ta)le scan or an inde! scan is highly undesira)le and should )e
a(oided "here possi)le.
&onsider the follo"ing query to find the second highest offer price from the *tems ta)leG
.E-E&T B$X (1rice)
=%OB 1roducts
2'E%E *D *N
(
.E-E&T TO1 : *D
=%OB 1roducts
O%DE% ;C 1rice Desc
)
The same query can )e re4"ritten using a deri(ed ta)le+ as sho"n )elo"+ and it performs
generally t"ice as fast as the a)o(e queryG
.E-E&T B$X (1rice)
=%OB (
.E-E&T TO1 : 1rice
=%OB 1roducts
O%DE% ;C 1rice DE.& )
This suppresses messages li0e ?(9 ro"(s) affected)? after e!ecuting *N.E%T+ ,1D$TE+ DE-ETE
and .E-E&T statements. 1erformance is impro(ed due to the reduction of net"or0 traffic.
Cou cannot directly "rite or update te!t data using the *N.E%T or ,1D$TE statements. *nstead+
you ha(e to use special statements li0e %E$DTEXT+ 2%*TETEXT and ,1D$TETEXT. .o+ if you don?t
ha(e to store more than H3; of te!t+ use the &'$% (HIII) or #$%&'$% (HIII) data type
instead.
Dynamic ./- tends to )e slo"er than static ./-+ as ./- .er(er must generate an e!ecution
plan at runtime. *= and &$.E statements come in handy to a(oid dynamic ./-. This helps to a(oid
deadloc0s.
3eep transactions as short as possi)le.
Touch the minimum amount of data possi)le during a transaction.
Ne(er "ait for user input in the middle of a transaction.
Do not use higher le(el loc0ing hints or restricti(e isolation le(els unless they are a)solutely
needed.
Cou might need the length of a string (aria)le in many places of your procedure+ )ut don?t call
the -EN function "hene(er it?s needed. *nstead+ call the -EN function once and store the result in a
(aria)le for later use.
*= EX*.T. (.E-E&T 9 =%OB 1roducts 2'E%E *D 7 JI)
*nstead OfG
*= EX*.T. (.E-E&T &O,NT(*D) =%OB 1roducts 2'E%E *D 7 JI)
&'$%(9II)+ "hen N,--+ "ill consume 9II )ytes+ resulting in space "astage. 1refera)ly+ use
#$%&'$%(9II) in this situation. #aria)le4length columns ha(e (ery little processing o(erhead
compared "ith fi!ed4length columns.
$l"ays head the procedures+ pac0ages+ functions and Triggers "ith the )elo" details
/*
*
* Created By :
* Created Date :
* Purpose :
* Modification History
* ------------------------------------------------------------------
* Name Date Description
* ------------------------------------------------------------------
*
*
*/
%emo(e unused codes and o)jects regularly.
Bo(e static e!pressions outside of loops and ./- statements.
&atch all e!ceptions and con(ert to meaningful return codes )efore returning to non4./- host
programs.
Do not o(erload an e!ception "ith multiple errors.
$(oid hard4codes.
%eturn message should )e standardized for all forms.
,se incremental &OBB*Ts to a(oid roll)ac0 segment errors "hen changing large num)ers of
ro"s.
Ne(er use a cursor =O% loop to fetch just one ro".
,se ;,-3 &O--E&T to impro(e performance of multi4ro" queries
Encapsulate dynamic ./- parsing to impro(e error detection and cleanup.

You might also like