Sunday 14 July 2013

Naming Convention for Database

Standard Naming Convention for Database
In programming language naming convention have great benefits to reduce the effort needed to read and understand source code. It provides better understanding in case of reuse code after a long interval of time. It is an initial step for beginner to learn any programming language. It is a very important element.

There exist so many different naming conventions for database objects, none of them is wrong. It's more of a personal preference of the person who designed the naming convention. However, in an organization, should have one naming convention standard and everybody should follow it. Here I have explained some criteria about naming convention for database.

Tables 
  • It should be in Pascal Case
  • It should not have Spaces
  • Multiple words should be split with Underscore
  • It should be Plural (more than one in number) - Example: Students Table, rather than Student.  If it contains multiple words only last word should be plural.  Example: Students_Photos
  • If your database deals with different logical functions and you want to group your tables according to the logical group they belong to, it won't hurt prefixing your table name with a two or three character prefix that can identify the group. For example, your database has tables which store information about Sales and Human resource departments, you could name all your tables related to Sales department as: SL_Customes, SL_Sales, SL_Orderss
You could name all your tables related to Human resources department as shown below:
HR_Candidates
HR_PremierInstitutes
HR_InterviewSchedules
  • However, if your database deals with only one logical group of tables, you need not use this naming convention.
Fields/Columns
It should not have Spaces
Multiple words should be split with Underscore
It should be Singular - Example: Student_ID column name, rather than Student s_ID or Student_IDS


Views
  • Views not always represent a single entity. A view can be a combination of two or more tables based on a join condition, thus, effectively representing two entities. In this case, consider combining the names of both the base tables. Example: A view combining two tables 'Students' and 'Addresses', name the view as ''StudentsAddresses”
  • Try to avoid using spaces in view name.
Stored Procedures
  • Never prefix your stored procedures with 'sp_'. If you use it then MS SQL Server first searches the SP in master database, if not found then search current database.
  • Procedure name should be defined as TableName_ProcedureFunctionalityName.  Example: Students_SelectAll,  Students_Insert, Students_Update, Students_Delete.  If table name is too long, it is also better to use short name of table rather than full tablename prefix, Example: Emp_SelectAll, Emp_Insert.  If table name contains multiple words like Student_Locations then it is better to give name like SL_SelectAll, SL_Insert.  If short name are getting duplicate, then you can change of one of short name to avoid duplication or confusion.
  • If you are creating procedure which is general in nature or combines 2 or more tables or mainly business logic which cannot be associated with any table, then it is better to use as BusinessLogicName_ProcedureFunctionalityName.  Example:  procedure for students quarterly sales report should be named something like Reports_Std_Quaterly_Sales.
  • Always try to use such a name which describes the whole functionality of the procedure.
Functions
  • Function name are mostly generic utilities, but incase if they are associated with table, then follow procedure naming convention, else use meaningful name.  Example:  CelsiusToFahrenheit  - If you pass Celsius temperature it will return Fahrenheit temperature.
Triggers
  • Trigger always depend on base table. So try to use table name with trigger name.
  • Triggers are associated with one or more of the following operations: Insert, Update, Delete. So, the name of the trigger should reflect its nature. Example: students_instrg, students_updtrg, students_deltrg
  • If you have a single trigger for more than one action (same trigger for insert and update or update and delete or any such combination), use the words 'ins', 'upd', 'del' together in the name of the trigger. Here's an example. 
Example: students_InsUpdtrg

Indexes
  • Index name should be name with prefix idx_ColumnName. Example: Idx_Student_ID
Primary Keys
  • Primary key should be name as PK_TableName.  Example:  PK_Students.
Foreign Keys
  • Foreign key should be name as FK_PrimaryTableName_ForeignTableName. Example:  PK_Students_Departments.  


EmoticonEmoticon