Purpose of this post is to standardize naming convention for SQL Server database objects so everyone can easily sense the type and purpose of the Object. A set of rules and guidelines are provided to follow while naming Database Objects.
Name is a sequence of characters that identifies a database object.
Naming Convention Rules for all database objects:
Name object meaningful, simple and shorter; avoid abbreviations where possible and use whole words
- Name object with mixed case and initial capitalized
- Avoid numbers and special characters
- Avoid SQL Server reserved keywords for name and alias
- Avoid spaces in names even allowed in SQL Server
- Avoid underscores in names as much as you can; except where defined in the document
User-defined Tables: Use proper case for each word in name. Group related table names like Users, UserRoles, UserAddresses
Examples:
- PSSRules
- PSSAssignedRules
Table Columns: If a column is referencing another table for foreign key use <table name>ID
Examples:
- UserID
- UserAddressID
User-defined Views: vw_<description or nature of the view >
Examples:
- vw_Users
- vw_UsersCallHistory
User-defined Functions: fn_<description or nature of the fucntion >
Examples:
- fn_Splittoken
- fn_GetFormatedDate
User-defined Stored Procedures: <GroupName>_<Action< description or nature of procedure >
Where action is: Get, Delete, Update, Write, Archive, Insert ... i.e.
Examples:
- PSS_GetAllRules
- PSS_DeletedRuleReason
Note: Do NOT use prefix “sp_” as SQL Server first consider it as system stored procedure
Primary Key Constraints: PK_<TableName>_<ColumnName(s)>
Examples:
- PK_Users_UsersID
- PK_UserGroup_GroupID
Foreign Key Constraints: FK_<ForeignTableName>_<ColumnName(s)>
Examples:
- FK_UserRules_RuleID
Unique Key Constraints: uk_<column names separated with underscore (_) >
Examples:
- UK_UserID_AccNo
- UK_FirstName_LastName
Check Constraints: chk_<description or nature of the constraint>
Examples:
- chk_SSN
- chk_DuplicateAccount
Default Constraints: DF_<column name>
Examples:
- DF_CellNo
User-defined Table Types: udtt_<description or nature of the table >
Examples:
- udtt_Users
- udtt_MessageSetting
Triggers: trg_TableName_<Trigger type (Instead Of/After)>_< Operation(Insert/Update/Delete) >
Examples:
- trg_Users_After_Update
- trg_UserDocuments_After_Delete
Note: Use of triggers is always discouraged
Indexes: IX_<column names separated with underscore (_)>
Examples:
IX_UserID_active
IX_UserAccountNumber
Object-level Backup: <DatabaseName>_<TableName>_BCK<Date>
Make a centralized database to store backups for recovery
This centralized setup will be easy to clean and maintain
Examples:
UsesrData_AccountHeader_BCK011619
UsersData_Documents_BCK011619
Object Name
|
Rule |
Examples |
Tables
|
|
|
Table Columns
|
|
|
Views
|
vw_<description or nature of the view > |
|
Functions
|
fn_<description or nature of the fucntion > |
|
Stored Procedures
|
<GroupName>_<Action< description or nature of procedure > Where action is: Get, Delete, Update, Write, Archive, Insert ... i.e.
Note: Do NOT use prefix “sp_” as SQL Server first consider it as system stored procedure
|
|
Primary Key Constraints
|
PK_<TableName>_<ColumnName(s)>
|
|
Foreign Key Constraints | FK_<ForeignTableName>_<ColumnName(s)> |
|
Unique Key Constraints
|
UK_<column names separated with underscore (_) >
|
|
Check Constraints
|
chk_<description or nature of the constraint> |
|
Default Constraints
|
DF_<column name> |
|
User-defined Table Types
|
udtt_<description or nature of the table >
|
|
Triggers
|
trg_TableName_<Trigger type (Instead Of/After)>_< Operation(Insert/Update/Delete) >
Note: Use of triggers is always discouraged
|
|
Indexes
|
IX_<column names separated with underscore (_)> |
|
Backup Object for recovery |
|
|