Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2018 May SQL Server Coding Standards: About Object Identifiers

SQL Server Coding Standards: About Object Identifiers

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

Prepared by Bihag Thaker

There are various types of objects in SQL Server like Tables, Views, Stored Procedures, User Defined Functions, Triggers, Constraints, Indexes, Schemas and Variables and so on. These objects should be assigned appropriate identifiers with which they are referenced in the database. Object Identifiers are of two types in SQL Server: (1) Regular Identifiers and (2) Delimited Identifiers

Regular Identifiers are the one that does comply with the format of identifiers in SQL Server. Delimited Identifiers are the one which does not comply with the format of identifiers.

Delimited identifiers must be delimited or enclosed by double quotation marks (“) or a pair of square brackets ([ ]).

For example, a delimited identifier not complying with the rules of the format of regular identifiers can be a T-SQL Reserved Word or can contain space within or any character which is not generally allowed in regular identifiers.

For example, even if ORDER is the reserved word in SQL Server, it can be assigned as an identifier to an object if it is delimited either by double quotation mark (“) or a pair of square brackets ([ ]) in the following manner in its definition

1
2
3
4
5
6
7
8
9
10
11
CREATE TABLE [Order]
(
OrderID INT
)
 
--OR
 
CREATE TABLE "Order"
(
OrderID INT
)

Delimiting identifiers with a double quotation mark (“) depends on SET QUOTED_IDENTIFIER setting of SQL Server.

If SET QUOTED_IDENTIFIER is set to OFF then all literal strings enclosed with a double quotation mark (“) are interpreted as string constants and not as identifiers. Due to this, delimiting identifiers with a double quotation mark (“) should never be used.

Following is an example of regular identifier assigned to a table which is not delimited:

1
2
3
4
CREATE TABLE tblCustomerOrders
(
OrderID INT
)

Even if, SQL Server allows the use of delimited identifiers, its use is strongly discouraged and should notbe used. Use of regular identifiers to identify the objects should be practiced.

Following are some of the common guild lines along with best practices which follow the rules of regular identifiers and should be practiced while assigning identifiers to various objects:

Maximum length of identifiers should not exceed 128 characters and 116 characters for local
temporary tables.

First character must be a letter which can be any from either a-z or A-Z.

Even if SQL Server allows underscore (_), at sign (@) and number sign (#) as the first character, they should not be used as the first character in identifiers. Some of these characters have special meaning in SQL Server.

For example, an identifier that starts with a number sign (#) denotes a local temporary table, one that starts with ‘double number sign’ (##) denotes a global temporary table and one that starts with ‘at sign’ (@) denotes a variable name or parameter name.

Some of the functions in SQL Server starts with double at signs (@@). Thus, these special characters should be avoided while specifying identifiers.

The subsequent characters can be any letter or at sign (@), a dollar sign ($) or an underscore (_) character. However, use of these special characters in identifiers should be avoided. Underscore (_) character may be used for word separation.

Space should not be used in identifiers. For word separation, either an underscore (_) or Pascal Casing should be used. Wherever possible, usage of underscore (_) should be minimized by using Pascal Casing in identifiers.

For example, an identifier should be specified as ‘CustomerAddress’ instead of ‘Customer_Address’. It achieves the same readability by use of Pascal Casing and also reduces the length of identifiers.

Do not use T-SQL Keywords as object identifiers

Identifiers should be self-explanatory rather than being abbreviations and acronyms and they should indicate their purpose. This helps the developer in understanding the code written by someone else even if comments are not present.

For example, rather than using variable names @A or @i for ‘Sum’ and ‘Loop Counter’ respectively, they should be defined as @Sum and @Counter.

May 16, 2018Anvesh Patel
SQL Puzzle: Generate Calendar Data for 19th CenturySQL Server Coding Standards: About General Naming Conventions

Leave a Reply Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel

Database Engineer

May 16, 2018 SQL Server, SQL Server Coding Standardsbasic sql commands, basic sql queries, coding best practices, SQL, sql basics, sql coding best practices, sql commands, sql database, sql formatter, sql language, SQL Programming, sql queries, sql queries for practice, sql query formatter, sql server format, sqlcode
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....