Skip to content

Commit 045d98e

Browse files
Merge branch 'patch-1' of https://github.com/justlilith/sql-docs into 20260330-database-identifiers
2 parents 06bebc2 + 308728e commit 045d98e

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

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

Lines changed: 37 additions & 0 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]

0 commit comments

Comments
 (0)