Why is IDENTITY UNIQUE allowed in SQL Server?

Why is this

CREATE TABLE ORDERS
(
   OrderID INT IDENTITY(1, 1) UNIQUE,
   BuyerID INT,

   PRIMARY KEY(OrderID, BuyerID)
);

allowed in SQL Server?

I mean, why is IDENTITY(1, 1) UNIQUE allowed?

Doesn't IDENTITY already mean that the values are going to be unique?

It's like static const in C# - const is already static (static const isn't allowed in C#).

Upvotes: 1

Views: 2114

Answers (1)

Dale K
Dale K

Reputation: 27226

No, IDENTITY doesn't mean the values will be unique. You can reset the seed of the identity column which will give you duplicates. Or you can enable IDENTITY_INSERT which will allow you to put your own values in.

And IDENTITY is referring to how the values are generated, whereas UNIQUE is adding a constraint. 2 different concepts.

Upvotes: 5

Related Questions