Database Design

Jump to: navigation, search
Categories » Databases » Database Design

Contents

Guidelines and Coding Standards

Naming

Example:
UserDetail
Email
Example:
page_ UserDetail
page_ Email

Stored Procedures

Syntax
usp<application_name>_<group_name>_<action_type><table_name>
Arguments
<application_name>
Name of the application that the stored procedure is for
<group_name>
In a situation where there a lot of stored procedures use a group name to separate them
<action_type>
Action is: Get, Delete, Update, Write, Archive, Insert… i.e. verb
<table_name>
Name of the table or logical instance
Examples
uspApplicationName_GetUserDetails
uspApplicationName_UpdateEmails

Columns

I like my columns to describe a little what they are about. I try to avoid prefixing with the name of the table they are in. If they are in that table then that means they relate to that table somehow.

ID Column

Naming Scheme: <tablename>ID

Example:
UserDetailID (UserDetail Table)
UserID (User Table)


Triggers

Naming Scheme: TR_<TableName>_<action><description>

Example:
TR_Emails_LogEmailChanges
TR_UserDetails_UpdateUserName

Indexes

Naming Scheme: IX_<tablename>_<columns separated by_>

Example:
IX_UserDetail_UserID

Primary Key

Naming Scheme: PK_<tablename>

Example:
PK_UserDetail
PK_ Email

Foreign Key

Naming Scheme:: FK_<tablename_1>_<tablename_2>

Example:
FK_UserDetail_Email

Default

Naming Scheme: DF__<column name>
Example:
DF_ UserDetail_UserName
Example:
SELECT Username, Password
FROM UserDetail

Example: Wrong Format

SELECT Username, Password FROM UserDetail ud INNER JOIN Employee e ON e.EmpID = ud.UserID
Example: Correct Format
SELECT Username, Password
FROM UserDetail ud
INNER JOIN Employee e ON e.EmpID = ud.UserID
Example:
SELECT, UPDATE, INSERT, WHERE, INNER JOIN, AND, OR, LIKE.

Column of Another Table : <OneTableName> ID Example: If User table references Employee table than the column name used in reference should be UserID where User is table name and ID primary column of User table and UserID is reference column of Employee table.

INSERT INTO TABLE A (column1, column2)
SELECT column1, column2
FROM TABLE B
WHERE ….

Example: [Order Details]

Example:

SET @sExample = ‘SQL’’s Authority’
IF ((SELECT 1
FROM TableName
WHERE 1=2) ISNULL)
IF EXISTS   (SELECT 1
FROM UserDetail
WHERE UserID = 50)
Rather than,
IF EXISTS  (SELECT COUNT (UserID)
FROM UserDetail
WHERE UserID = 50)

Example:

SP<App Name>_ [<Group Name >_] <Action>
Example: Wrong Statement
SELECT UserID, UserName, Password
FROM UserDetail
ORDER BY 2
Example: Correct Statement
SELECT UserID, UserName, Password
FROM UserDetail
ORDER BY UserName

Example:

SELECT EmpNo, EmpName
FROM EmployeeRecord
WHERE Salary > 1000 AND Salary
NOT IN (SELECT Salary
FROM EmployeeRecord
WHERE Salary > 2000);
(Recomended)
SELECT EmpNo, EmpName
FROM EmployeeRecord
WHERE Salery > 1000
EXCEPT
SELECT EmpNo, EmpName
FROM EmployeeRecord
WHERE Salery > 2000
ORDER BY EmpName;

More Information

Personal tools
Namespaces
Variants
Actions
Navigation
Categories
Toolbox