You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The database object name is referred to as its identifier.
30
+
The database object name is its identifier.
30
31
31
32
Servers, databases, and database objects, such as tables, views, columns, indexes, triggers, procedures, constraints, and rules, can have identifiers. Most objects require identifiers, but some objects, such as constraints, make them optional.
32
33
33
34
You create an object identifier when you define the object. Use the identifier 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`:
34
35
35
36
```sql
36
-
CREATETABLETableX (
37
+
CREATETABLETableX
38
+
(
37
39
KeyCol INTPRIMARY KEY,
38
-
Description NVARCHAR(80)
40
+
Description NVARCHAR(80)
39
41
);
40
42
```
41
43
42
-
This table also has an unnamed constraint. The primary key constraint has no identifier, and so would be assigned a system-generated name like `PK__TableX__D7CB9CCCEEF0806C`, which you could observe in system metadata views like `sys.key_constraints`.
44
+
This table has an unnamed constraint. The primary key constraint has no user-specified identifier, so the system assigns it a generated name like `PK__TableX__D7CB9CCCEEF0806C`. You can see this name in system metadata views like [sys.key_constraints](../system-catalog-views/sys-key-constraints-transact-sql.md).
43
45
44
46
Constraint names and other schema-scoped objects must be unique within a database schema. For example, two primary key constraints can't share a name. However, column names only need to be unique within each table, not within the schema.
45
47
46
-
The collation of an identifier depends on the level at which you define it.
48
+
The collation of an identifier depends on the level at which you define it.
49
+
50
+
- The default collation of the instance is assigned to identifiers of instance-level objects, such as logins and database names.
47
51
48
-
- The default collation of the instance is assigned to identifiers of instance-level objects, such as logins and database names.
49
52
- The default collation of the database is assigned to identifiers of objects in a database, such as tables, views, and column names. For example, you can create two tables with names that differ only in case in a database that has case-sensitive collation, but you can't create them in a database that has case-insensitive collation.
50
53
51
54
> [!NOTE]
52
-
> The names of variables, or the parameters of functions and stored procedures must comply with the rules for [!INCLUDE [tsql](../../includes/tsql-md.md)] identifiers.
55
+
> The names of variables, or the parameters of functions and stored procedures, must comply with the rules for [!INCLUDE [tsql](../../includes/tsql-md.md)] identifiers.
53
56
54
57
## Classes of identifiers
55
58
56
59
There are two classes of identifiers:
57
60
58
-
-**Regular identifiers** comply with the rules for the format of identifiers. Regular identifiers aren't delimited when they're used in [!INCLUDE [tsql](../../includes/tsql-md.md)] statements.
61
+
-**Regular identifiers** comply with the format rules for identifiers. They aren't delimited when used in [!INCLUDE [tsql](../../includes/tsql-md.md)] statements. Regular identifiers must follow the [rules for regular identifiers](#rules-for-regular-identifiers): they can only contain letters, digits, and certain symbols (`_`, `@`, `#`, `$`), must start with a letter or one of `_`, `@`, `#`, and can't be a reserved word.
59
62
60
63
```sql
61
-
USE AdventureWorks2022;
64
+
USE AdventureWorks2025;
62
65
GO
63
66
64
67
SELECT*
65
68
FROMHumanResources.Employee
66
69
WHERE NationalIDNumber =153479919;
67
70
```
68
71
69
-
-**Delimited identifiers** are enclosed in double quotation marks (`"`) or brackets (`[` and `]`). Identifiers that comply with the rules for the format of identifiers might not be delimited. For example:
72
+
-**Delimited identifiers** are enclosed in double quotation marks (`"`) or brackets (`[` and `]`). Delimiters allow you to use names that would otherwise be invalid as regular identifiers, such as reserved keywords, names with spaces, or names with special characters. Identifiers that already comply with the regular identifier rules can also be delimited, but the delimiters are optional in that case. For more information, see [Rules for delimited identifiers](#rules-for-delimited-identifiers).
70
73
71
74
```sql
72
-
USE AdventureWorks2022;
75
+
USE AdventureWorks2025;
73
76
GO
74
77
75
78
SELECT*
76
79
FROM [HumanResources].[Employee] --Delimiter is optional.
77
-
WHERE [NationalIDNumber] =153479919--Delimiter is optional.
80
+
WHERE [NationalIDNumber] =153479919;--Delimiter is optional.
78
81
```
79
82
80
-
Identifiers that don't comply with all the rules for identifiers must be delimited in a [!INCLUDE [tsql](../../includes/tsql-md.md)] statement. For example:
83
+
Identifiers that don't comply with the rules for regular identifiers must be delimited in a [!INCLUDE [tsql](../../includes/tsql-md.md)] statement. For example:
81
84
82
85
```sql
83
-
USE AdventureWorks2022;
86
+
USE AdventureWorks2025;
84
87
GO
85
88
86
89
--Identifier contains a space and uses a reserved keyword.
@@ -106,6 +111,67 @@ WHERE [Order] = 10; --Identifier is a reserved keyword.
106
111
107
112
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.
108
113
114
+
## Rules for delimited identifiers
115
+
116
+
Delimited identifiers are either enclosed in brackets (`[` and `]`) or double quotation marks (`"`). They can contain any combination of characters, including spaces, reserved keywords, and special characters that aren't allowed in regular identifiers.
117
+
118
+
### Bracket-delimited identifiers
119
+
120
+
Bracket-delimited identifiers are enclosed in square brackets (`[` and `]`). If the identifier itself contains a right bracket (`]`), escape it by doubling it (`]]`). A left bracket (`[`) doesn't require escaping.
121
+
122
+
For example, to create and query a table whose name contains brackets:
123
+
124
+
```sql
125
+
-- Create a table with a ] character in its name.
126
+
CREATE TABLE [My]]Table]
127
+
(
128
+
ID INTPRIMARY KEY
129
+
);
130
+
GO
131
+
132
+
-- Reference the table in a query.
133
+
SELECT*
134
+
FROM [My]]Table];
135
+
GO
136
+
```
137
+
138
+
The `QUOTENAME` function returns a valid bracket-delimited identifier for a given string and handles the escaping automatically:
139
+
140
+
```sql
141
+
SELECT QUOTENAME('abc[]def');
142
+
```
143
+
144
+
The previous example returns `[abc[]]def]`.
145
+
146
+
### Double-quote-delimited identifiers
147
+
148
+
Double-quote-delimited identifiers are enclosed in double quotation marks (`"`). If the identifier itself contains a double quotation mark, escape it by doubling it (`""`).
149
+
150
+
Double-quote delimiters require `SET QUOTED_IDENTIFIER ON` (the default for most connections). When `QUOTED_IDENTIFIER` is `OFF`, the [!INCLUDE [ssde-md](../../includes/ssde-md.md)] treats double-quoted strings as string literals instead of identifiers. For more information, see [SET QUOTED_IDENTIFIER](../../t-sql/statements/set-quoted-identifier-transact-sql.md).
151
+
152
+
For example, to create and query a table that uses reserved keywords as identifiers:
153
+
154
+
```sql
155
+
SET QUOTED_IDENTIFIER ON;
156
+
GO
157
+
158
+
-- Create a table using double-quote delimiters.
159
+
CREATE TABLE "My Table"
160
+
(
161
+
"Order"INTNOT NULL,
162
+
"Description" NVARCHAR (100)
163
+
);
164
+
GO
165
+
166
+
SELECT"Order",
167
+
"Description"
168
+
FROM"My Table";
169
+
GO
170
+
```
171
+
172
+
> [!NOTE]
173
+
> `SET QUOTED_IDENTIFIER` doesn't affect bracket-delimited identifiers. Bracket delimiters always work regardless of the `QUOTED_IDENTIFIER` setting.
174
+
109
175
## Rules for regular identifiers
110
176
111
177
The names of variables, functions, and stored procedures must follow these rules for [!INCLUDE [tsql](../../includes/tsql-md.md)] identifiers.
@@ -116,7 +182,7 @@ The names of variables, functions, and stored procedures must follow these rules
116
182
117
183
- The underscore (`_`), at sign (`@`), or number sign (`#`).
118
184
119
-
Certain symbols at the beginning of an identifier have special meaning in [!INCLUDE [ssNoVersion](../../includes/ssnoversion-md.md)]. A regular identifier that starts with the at sign always denotes a local variable or parameter and can't be used as the name of any other type of object. An identifier that starts with a number sign denotes a temporary table or procedure. An identifier that starts with double number signs (`##`) denotes a global temporary object. Although the number sign or double number sign characters can be used to begin the names of other types of objects, we don't recommend this practice.
185
+
Certain symbols at the beginning of an identifier have special meaning in the [!INCLUDE [ssde-md](../../includes/ssde-md.md)]. A regular identifier that starts with the at sign always denotes a local variable or parameter and can't be used as the name of any other type of object. An identifier that starts with a number sign denotes a temporary table or procedure. An identifier that starts with double number signs (`##`) denotes a global temporary object. Although the number sign or double number sign characters can be used to begin the names of other types of objects, you should avoid this practice.
120
186
121
187
Some [!INCLUDE [tsql](../../includes/tsql-md.md)] functions have names that start with double at signs (`@@`). To avoid confusion with these functions, don't use names that start with `@@`.
122
188
@@ -128,26 +194,26 @@ The names of variables, functions, and stored procedures must follow these rules
128
194
129
195
- The at sign (`@`), dollar sign (`$`), number sign (`#`), or underscore (`_`).
130
196
131
-
1. The identifier must not be a [!INCLUDE [tsql](../../includes/tsql-md.md)] reserved word. [!INCLUDE [ssNoVersion](../../includes/ssnoversion-md.md)] reserves both the uppercase and lowercase versions of reserved words. When you use identifiers in [!INCLUDE [tsql](../../includes/tsql-md.md)] statements, delimit identifiers that don't comply with these rules by using double quotation marks or brackets. The words that are reserved depend on the database compatibility level. Set the database compatibility level by using the [ALTER DATABASE compatibility level](../../t-sql/statements/alter-database-transact-sql-compatibility-level.md) statement.
197
+
1. The identifier must not be a [!INCLUDE [tsql](../../includes/tsql-md.md)] reserved word. The [!INCLUDE [ssde-md](../../includes/ssde-md.md)] reserves both the uppercase and lowercase versions of reserved words. When you use identifiers in [!INCLUDE [tsql](../../includes/tsql-md.md)] statements, delimit identifiers that don't comply with these rules by using double quotation marks or brackets. The words that are reserved depend on the database compatibility level. Set the database compatibility level by using the [ALTER DATABASE compatibility level](../../t-sql/statements/alter-database-transact-sql-compatibility-level.md) statement.
132
198
133
199
1. Don't use embedded spaces or special characters.
134
200
135
201
1. Don't use [Supplementary characters](../collations/collation-and-unicode-support.md#Supplementary_Characters).
136
202
137
-
When you use identifiers in [!INCLUDE [tsql](../../includes/tsql-md.md)] statements, delimit identifiers that don't comply with these rules by using double quotation marks or brackets.
138
-
139
-
Some rules for the format of regular identifiers depend on the database compatibility level.
203
+
When you use identifiers in [!INCLUDE [tsql](../../includes/tsql-md.md)] statements, delimit identifiers that don't comply with these rules by using double quotation marks or brackets. Some of these rules vary depending on the database [compatibility level](../../t-sql/statements/alter-database-transact-sql-compatibility-level.md).
140
204
141
205
## Catalog collation in Azure SQL Database
142
206
143
-
You can't change or set the logical server collation on Azure SQL Database. However, you can configure each database's collations separately for data in the database and for catalog. The catalog collation determines the collation for system metadata, such as object identifiers. You can specify both collations independently when you [create the database in the Azure portal](/azure/azure-sql/database/single-database-create-quickstart?view=azuresql&preserve-view=true&tabs=azure-portal#create-a-single-database), in T-SQL with [CREATE DATABASE](../../t-sql/statements/create-database-transact-sql.md?view=azuresqldb-current&preserve-view=true#collation_name), or in PowerShell with [New-AzSqlDatabase](/powershell/module/az.sql/new-azsqldatabase).
207
+
You can't change or set the logical server collation on Azure SQL Database. However, you can configure each database's collations separately for data in the database and for catalog. The catalog collation determines the collation for system metadata, such as object identifiers. You can specify both collations independently when you [create the database in the Azure portal](/azure/azure-sql/database/single-database-create-quickstart?view=azuresql&preserve-view=true&tabs=azure-portal#create-a-single-database), in Transact-SQL (T-SQL) with [CREATE DATABASE](../../t-sql/statements/create-database-transact-sql.md?view=azuresqldb-current&preserve-view=true#collation_name), or in PowerShell with [New-AzSqlDatabase](/powershell/module/az.sql/new-azsqldatabase).
144
208
145
209
For details and examples, see [CREATE DATABASE](../../t-sql/statements/create-database-transact-sql.md?view=azuresqldb-current&preserve-view=true#collation_name). Specify a collation for the database (`COLLATE`) and a catalog collation for system metadata and object identifiers (`CATALOG_COLLATION`).
146
210
147
211
## Catalog collation in SQL database in Microsoft Fabric
148
212
149
-
Currently, by default the collation of a SQL database in Fabric is `SQL_Latin1_General_CP1_CI_AS`, but this can be configured when deploying. The collation can't be updated after deployment. Collations on individual columns are supported. For more information on deployment options, see [Options to create a SQL database in Fabric](/fabric/database/sql/create-options).
213
+
The default collation of a SQL database in Fabric is `SQL_Latin1_General_CP1_CI_AS`. You can configure a different collation at deployment time, but you can't change it after the database is created. Individual columns can use their own collations. For more information on deployment options, see [Options to create a SQL database in Fabric](/fabric/database/sql/create-options).
0 commit comments