Database
Coding Standard and Guideline
Author's note: A surprising number of people still looking at this document. Very Cool! Thanks! There is a considerable amount of updated information available on this subject at www.bwunder.com There is also a bit of stuff below I never really agreed with. The point was to show an example of a coding standard that was created by the team. This is such an example. A long one at that. One of my goals for 2008 is to update the standard for SQL Server 2008 and eliminate all the stuff I do not agree with. The update will be posted on www.bwunder.com. Let me know what you'd like to see in there! (email). Also note that there are over 100 articles and many times that number of scripts for SQL Server posted on this site - ?bill
Summary
This
document will provide a framework to aid in optimal usability of Microsoft SQL
Server 7.0 schema, scripts and stored procedures developed for applications by defining a reasonable, consistent
and effective coding style. The qualities of usability as used here include
readability by other database technicians, ease of implementation,
maintainability, and consistency. The framework will serve to improve the
application without unnecessary impact on development and unnecessary controls
on personal coding preferences. For these reasons the framework will focus on
identifier naming conventions that are intended to be used by all developers,
general style guidelines indicating the preferred format and usage of SQL
language components, and a definition of the database development methodology.
The
identifier segment of the standard will formalize naming conventions. All
schema, scripts and stored procedures will conform to all elements of this area
of the document.
The
general guideline will include SQL statement formatting and outlines for
solutions to more complex components within scripts and stored procedures. This
instrument is intended as a ‘best practice’ model consistent with the identified
architecture and requirements. It may be necessary to adapt the guideline to
specific solutions within the application. While the guidelines are not
mandatory, adherence will aid in the ultimate success of the application and
ease of maintenance. All developers will be expected to reasonably defend any
variance from the guideline.
The
combination of conformance to standards and development within the stated
guidelines will be measured and assessed in the context of the methodology. The
methodology will present structure and consistency through clearly defined
requirement specifications, change control procedures, code review, testing,
controlled iterative development cycles, and regular developer evaluations.
There
is a perceptive risk in not defining a ubiquitous and mandatory coding
standard. Stored procedure and script development may become sloppy and
unreadable resulting in diminished usability. In actuality, this is a
deficiency in the developer and/or the development methodology not the framework.
Without the controls of testing, review and feedback to support a high quality
development team any standard will not assure usability. Defining most issues
of style and layout within the context of guidelines rather than dogma frees
the developer to produce robust and creative solutions. This latitude in the
framework is intended to balance mechanics and creativity. Clear definition and
execution of the controls will assure the purpose of this or any coding
standard.
Identifiers
|
Object type |
Prefix |
Example |
|
Primary key Clustered |
pkc_ |
pkc_MY_TABLE__Column |
|
Primary key Nonclustered |
pkn_ |
pkn_TB_TABLE__Column_List |
|
Index Clustered |
ixc_ |
ixc_TS2_TABLE__Column |
|
Index Nonclustered |
ixn_ |
ixn_TB_TABLE__Column_List |
|
Foreign key |
fk_ |
fk_THIS_TABLE__ColumnB__to__TB_PKEY_TABLE__ColumnA |
|
Unique Constraint |
unq_ |
unq_TB_TABLE__Column_List |
|
Check Constraint |
chk_ |
chk_TB_TABLE__Column |
|
Column Default |
dft_ |
dft_TB_TABLE_Column_List |
|
Passed Parameter |
@p |
@pPassedVariableName |
|
Local Variable |
@ |
@VariableName |
|
Table |
TB_, *_ |
TB_TABLE_NAME (see detail below) |
|
View |
VW_ |
VW_NET_ACTIVE_UNITS |
|
User Defined Scalar
Function |
ufs_ |
ufs_return_value_name |
|
User Defined Table
Function |
uft_ |
uft_TB_TABLE_NAME |
|
Stored Procedure |
* |
Eds_Def (see detail below) |
|
Script type |
Prefix |
Example |
|
Stored procedure script |
proc_ |
proc_Calendar.sql |
|
Schema script |
def_ |
def_Calender.sql |
|
Conversion script |
conv_ |
conv_Schedule.sql |
|
Rollback script |
rbk_ |
rbk_Schedule.sql |
|
Object
Type |
Suffix |
Example |
|
Account |
_Acct |
Process_Corp_Acct |
|
Address |
_Addr |
Contact_Addr |
|
Amount |
_Amt |
Total_Credit_Amt |
|
Balance |
_Bal |
Available_Bal |
|
Date or Datetime |
_Dt |
Active_Dt,
@Archive_Dt |
|
Description |
_Desc |
Product_Desc |
|
Date of Birth |
_DOB |
Alternate_DOB |
|
Indicator |
_Ind |
Net_Gross_Ind |
|
Line(n) |
_Ln(n) |
Address_Ln2,
@pOrderLn |
|
Number |
_Nbr |
Bank_Nbr |
|
Record identifier/identity |
_Id |
Entity_Id,
@pEntityId |
|
Tax
Id Number |
_TIN |
Merchant_TIN |
|
Card
Transactions |
_Trans |
Daily_Nbr_Trans |
|
Zip Code |
_Zip |
Recipient_Zip |
Tables
create table {database name}.{table owner}.{table
name}
([{table
name}_Id {data type} [identity(begin, step),]]
{column
name} {data type} {null | not null},
{column
name} {data type} {null | not null}
constraint
dft_{table name}__{column name}
default
({default value}),
{column
name} {data type} {null | not null}[,
constraint
pk{u | n}_{table name}__{pkey column}
Primary
key {clustered | non-clustered} ({pkey column})][,
constraint fk_{this table}__{column
[list]}__to__{other table}__{column [list]}
foreign key {column
[list]}
references {other
table}({column [list]})][,
constraint unq_{table name}__{column [list]}
unique key ({column
[list]}][,
constraint chk_{table name}__{column [list]}
check
({check expression}),])
Example:
create table School.dbo.TB_COURSE
(Course_Id int identity(1,
1) not null,
Educator_Id int not null,
School_Id int not null,
Course_Name varchar(50)
not null,
Start_Date datetime
Constraint
dft_TB_COURSE__Start_Date
default (getdate()),
End_Date datetime null,
constraint pkc_TB_COURSE__Course_Id
primary key clustered
(Course_Id),
constraint
fk_TB_COURSE__Educator_Id__to__TB_EDUCATOR__User_Id
foreign key (Educator_Id)
references
TB_EDUCATOR(User_Id),
constraint
fk_TB_COURSE__Course_Id__to__TB_SCHOOL__School_Id
foreign key (Course_Id)
references
TB_SCHOOL(School_Id),
constraint
chk_TB_COURSE__Start_Date__End_Date
check (Start_Date <=
End_Date))
TSYS_REC_30, EDS_REC_40, PVS_ACCOUNT, etc.
TB_CALLING_CARD not
TB_CALLING_CARDS
fk_TB_COURSE__Educator_Id__to__TB_USER__Id
If
(objectProperty(object_id('{constraint name}'),
'IsConstraint') is not
null)
alter table {table name}
drop constraint
{constraint name}
If
(objectProperty(object_id('{constraint name}'),
'IsConstraint') is null)
alter table {fully
qualified table name}
add constraint {constraint
name}
default {constraint value}
[for {column name}]
If
(ColumnProperty(object_id('{table name}')
,{column name},
'AllowsNull') is not null)
alter table {fully
qualified table name}
drop {column name}
If
(ColumnProperty(object_id('{table name}')
,{column name},
'AllowsNull') is null)
alter table {fully
qualified table name}
add {column name} {data type} {null | not
null}
[constraint {default name}
default ({default value})]
If (ColumnProperty(object_id('{table
name}')
,{column name},
'AllowsNull') is not null)
alter table {fully
qualified table name} [with nocheck]
alter column {column name}
{new compatible data type}
If
(columnProperty(object_id('{table name}’),
{column name},
'AllowsNull') is not null)
alter table
Plans.dbo.TB_DESIGNS
drop column Color_Id
or