Skip to content

Commit 323446f

Browse files
authored
Add uniqueness documentation for database identifiers
Added a rule stating that identifiers must be uniquely named within a database schema or object, including an example demonstrating naming conflicts. I can't find any other documentation on this, so it would be helpful (assuming I'm understanding correctly) to have this available. I ran into a naming conflict and didn't realize there's a bit more specificity to naming.
1 parent c7c00b3 commit 323446f

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,42 @@ The names of variables, functions, and stored procedures must comply with the fo
123123

124124
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.
125125

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+
126162
1. Embedded spaces or special characters aren't allowed.
127163

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

0 commit comments

Comments
 (0)