Monday, August 24, 2009

Naming Conventions for SQL Server 2005

Simple rules for naming Objects in SQL Server 2005:
  1. Do not use any spaces in object names: With SQL Server it is possible to have such names by enclosing these in square brackets [and], but are not recommend as you may miss them out and end up in failure quite often.
  2. Avoid using reserved words: Even if the word you use doesn't cause an error, it will still cause confusion when someone is editing code and Query Analyzer applies color highlighting to table or column names.
  3. Start object names with a letter: Several applications like Enterprise Manager, Query Analyzer, DTS, VBScript, Visual Studio may cause problems when accessing objects with name starting from numbers.
  4. Keep names short but meaningful: This is self-explanatory and fairly logical, as there may be table name either ridiculously long, making it cumbersome both to type and to commit to memory, or abbreviated to the point of utter confusion. "SlQ" is too short. "SalesFiguresForCompanyByFiscalQuarter" is just silly.
  5. Avoid Hungarian notation*: The name of the object should make it pretty obvious what type of data it contains, and if for some reason it does not, then there is always the metadata tables and/or the documentation you should have written when designing the system. Using datatype-style prefixes for columns like IContactID (integer) and VEmail (varchar) not only make the column names harder to read, they also make them less flexible.
  6. Use CamelCasing instead**: Using a standard convention like CameCase for naming all the objects can help in maintaining consistency in object names.
  7. Use the dbo. Prefix: When you are logged in as a non-dbo user, and you create a table without giving it an owner prefix, other users won't see it, because it is stored in the system as you.objectName instead of dbo.objectName. If you consistently use the dbo. prefix, you will eliminate the possibility of creating the same object name twice, with different owner names.

*Hungarian notation:
Hungarian notation is a naming convention in computer programming, in which the name of a variable indicates its type or intended use. In Hungarian notation, a variable name starts with one or more lower-case letters which are mnemonics for the type or purpose of that variable, followed by whatever the name the programmer has chosen

**CamelCasing:

CamelCase is a writing convention in which words are combined by taking out the spacing in between and capitalizing the first letter of each word, such as CamelCase. The name is derived from the idea of the capital letters being reminiscent of humps on a camel's back. The CamelCase convention is used in computer systems in which naming require words to be contiguous. With CamelCase, words are readily distinguished and names more easily read.

Naming Conventions for SQL 2005:


*Do not use prefix sp_ for stored procedures. SQL Server searches for system stored procedures first when it encounters the sp_ prefix. Another reason- if Microsoft decides to rename all system stored procedures using the prefix to identify the system stored procedures, yours too would get renamed (since it has the same prefix) causing your application to fail.

No comments:

Post a Comment