The name of a database object is known as its identifier. Everything in Microsoft® SQL Server™ can have an identifier. Servers, databases, and database objects such as tables, views, columns, indexes, triggers, procedures, constraints, rules, and so on can have identifiers. Most objects are required to have an identifier; however, identifiers are optional for some objects, such as constraints.
An object's identifier is created when the object is defined. You then use the identifier to reference the object. For example, this statement creates a table with the identifier TableX, and two columns with the identifiers KeyCol and Description:
CREATE TABLE TableX
(KeyCol INT PRIMARY KEY, Description NVARCHAR(80))
This table also has an unnamed constraint. The PRIMARY KEY constraint has no identifier.
There are two classes of identifiers:
SELECT *
FROM TableX
WHERE KeyCol = 124
SELECT *
FROM [TableX] --Delimiter is optional.
WHERE [KeyCol] = 124 --Delimiter is optional.
SELECT *
FROM [My Table] --Identifier contains a space.
WHERE [order] = 10 --Identifier is a reserved keyword.
Both regular and delimited identifiers must contain from 1 to 128 characters. For local temporary tables, the identifier cannot exceed 116 characters.
The rules for the format of regular identifiers are dependent on the database compatibility level, which can be set with sp_dbcmptlevel. For more information, see sp_dbcmptlevel. When the compatibility level is 70, the rules are:
Certain symbols at the beginning of an identifier have special meaning in SQL Server. An identifier beginning with @ denotes a local variable or parameter. An identifier beginning with # denotes a temporary table or procedure. An identifier beginning with double number signs (##) denotes a global temporary object.
Some Transact-SQL functions have names that start with double at signs (@@). To avoid confusion with these functions, it is recommended that you do not use names that start with @@.
Any identifier that does not comply with all these rules must always be delimited by double quotation marks or brackets when used in Transact-SQL statements.
ALTER TABLE | CREATE VIEW |
CREATE DATABASE | DECLARE @local_variable |
CREATE DEFAULT | DELETE |
CREATE PROCEDURE | Delimited Identifiers |
CREATE RULE | INSERT |
CREATE TABLE | SELECT |
CREATE TRIGGER | UPDATE |