Skip to content

Commit 308728e

Browse files
authored
Add copilot su
Added explanation about unique identifiers for constraints and provided SQL examples to illustrate primary key naming conflicts.
1 parent 323446f commit 308728e

1 file changed

Lines changed: 37 additions & 36 deletions

File tree

docs/relational-databases/databases/database-identifiers.md

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,43 @@ CREATE TABLE TableX (
3939

4040
This table also has an unnamed constraint. The `PRIMARY KEY` constraint has no identifier.
4141

42+
Some identifiers, such as constraint names and other schema-scoped objects, must be unique within a database schema. For example, two primary key constraints in the same schema can't share a name, so the second table creation statement isn't allowed:
43+
44+
```sql
45+
USE AdventureWorks2022;
46+
GO
47+
48+
CREATE TABLE [SalesOrderGeneral Table] (
49+
[Order] INT NOT NULL,
50+
[SalesOrderID] INT IDENTITY(1, 1) NOT NULL,
51+
[SalesOrderDetailID] INT NOT NULL,
52+
[ModifiedDate] DATETIME NOT NULL,
53+
CONSTRAINT [PK_SalesOrder] PRIMARY KEY CLUSTERED (
54+
[Order] ASC,
55+
[SalesOrderID] ASC
56+
)
57+
);
58+
GO
59+
60+
-- Primary key identifier conflicts with existing primary key, and so will result in an error.
61+
CREATE TABLE [SalesOrderDetail Table] (
62+
[Order] INT NOT NULL,
63+
[SalesOrderDetailID] INT IDENTITY(1, 1) NOT NULL,
64+
[OrderQty] SMALLINT NOT NULL,
65+
[ProductID] INT NOT NULL,
66+
[UnitPrice] MONEY NOT NULL,
67+
[UnitPriceDiscount] MONEY NOT NULL,
68+
[ModifiedDate] DATETIME NOT NULL,
69+
CONSTRAINT [PK_SalesOrder] PRIMARY KEY CLUSTERED (
70+
[Order] ASC,
71+
[SalesOrderDetailID] ASC
72+
)
73+
);
74+
GO
75+
```
76+
77+
However, each table can contain its own column named `Order`, because column names only need to be unique within each table, not within the schema.
78+
4279
The collation of an identifier depends on the level at which it's 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 can't be created in a database that has case-insensitive collation.
4380

4481
> [!NOTE]
@@ -123,42 +160,6 @@ The names of variables, functions, and stored procedures must comply with the fo
123160

124161
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 identifiers are used in [!INCLUDE [tsql](../../includes/tsql-md.md)] statements, the identifiers that don't 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 compatibility level](../../t-sql/statements/alter-database-transact-sql-compatibility-level.md) statement.
125162

126-
1. Identifiers must be named uniquely within a database schema or database object. For example, two keys in the same schema may not share a name, so the second table creation statement would not be allowed:
127-
```sql
128-
USE AdventureWorks2022;
129-
GO
130-
131-
CREATE TABLE [SalesOrderGeneral Table] (
132-
[Order] INT NOT NULL,
133-
[SalesOrderID] INT IDENTITY(1, 1) NOT NULL,
134-
[SalesOrderDetailId] INT NOT NULL,
135-
[ModifiedDate] DATETIME NOT NULL,
136-
CONSTRAINT [PK_SalesOrder] PRIMARY KEY CLUSTERED (
137-
[Order] ASC,
138-
[SalesOrderID] ASC
139-
)
140-
);
141-
GO
142-
143-
--Primary key identifier conflicts with existing primary key, and so will result in an error
144-
CREATE TABLE [SalesOrderDetail Table] (
145-
[Order] INT NOT NULL,
146-
[SalesOrderDetailID] INT IDENTITY(1, 1) NOT NULL,
147-
[OrderQty] SMALLINT NOT NULL,
148-
[ProductID] INT NOT NULL,
149-
[UnitPrice] MONEY NOT NULL,
150-
[UnitPriceDiscount] MONEY NOT NULL,
151-
[ModifiedDate] DATETIME NOT NULL,
152-
CONSTRAINT [PK_SalesOrder] PRIMARY KEY CLUSTERED (
153-
[Order] ASC,
154-
[SalesOrderDetailID] ASC
155-
)
156-
);
157-
GO
158-
```
159-
160-
However, each table may contain its own column named `Order`, as the column name is unique among columns within the table.
161-
162163
1. Embedded spaces or special characters aren't allowed.
163164

164165
1. [Supplementary characters](../../relational-databases/collations/collation-and-unicode-support.md#Supplementary_Characters) aren't allowed.

0 commit comments

Comments
 (0)