Database Identifiers

The database object name is referred to 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, and rules, can have identifiers. Identifiers are required for most objects, but are optional for some objects such as constraints.

An object identifier is created when the object is defined. The identifier is then used to reference the object. For example, the following 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.

The collation of an identifier depends on the level at which it is defined. Identifiers of instance-level objects, such as logins and database names, are assigned the default collation of the instance. Identifiers of objects in a database, such as tables, views, and column names, are assigned the default collation of the database. For example, two tables with names that differ only in case can be created in a database that has case-sensitive collation, but cannot be created in a database that has case-insensitive collation.

Note

The names of variables, functions, and stored procedures must comply with the rules for Transact-SQL identifiers.

Classes of Identifiers

There are two classes of identifiers:

  • Regular identifiers
    Comply with the rules for the format of identifiers. Regular identifiers are not delimited when they are used in Transact-SQL statements.

    SELECT *
    FROM TableX
    WHERE KeyCol = 124
    
  • Delimited identifiers
    Are enclosed in double quotation marks (") or brackets ([ ]). Identifiers that comply with the rules for the format of identifiers might not be delimited. For example:

    SELECT *
    FROM [TableX]         --Delimiter is optional.
    WHERE [KeyCol] = 124  --Delimiter is optional.
    

    Identifiers that do not comply with all the rules for identifiers must be delimited in a Transact-SQL statement. For example:

    SELECT *
    FROM [My Table]      --Identifier contains a space and uses a reserved keyword.
    WHERE [order] = 10   --Identifier is a reserved keyword.
    

Both regular and delimited identifiers must contain from 1 through 128 characters. For local temporary tables, the identifier can have a maximum of 116 characters.

Reserved Words

The identifier must not be a Transact-SQL reserved word. SQL Server reserves both the uppercase and lowercase versions of reserved words. When identifiers are used in Transact-SQL statements, the identifiers that do not comply with these rules must be delimited by double quotation marks or brackets. The words that are reserved depend on the database compatibility level. This level can be set by using the ALTER DATABASE statement.

See Also

Reference

ALTER TABLE (Transact-SQL)

CREATE DATABASE (Transact-SQL)

CREATE DEFAULT (Transact-SQL)

CREATE PROCEDURE (Transact-SQL)

CREATE RULE (Transact-SQL)

CREATE TABLE (Transact-SQL)

CREATE TRIGGER (Transact-SQL)

CREATE VIEW (Transact-SQL)

DECLARE @local\_variable (Transact-SQL)

DELETE (Transact-SQL)

INSERT (Transact-SQL)

Reserved Keywords (Transact-SQL)

SELECT (Transact-SQL)

UPDATE (Transact-SQL)