42

Consider this unique constraint:

ALTER TABLE Posts
ADD CONSTRAINT UQ_Posts_Name
UNIQUE (Name);

Does it automatically create index on Name column?

4 Answers 4

36

yes it does. See this

Sign up to request clarification or add additional context in comments.

1 Comment

Worth adding that it's a non-clustered index (as explained in the link).
4

Yes, just to add.. creating primary key automatically creates clustered index.

EDIT: I was wrong... after Lieven's comment:

When you create a PRIMARY KEY constraint, a unique clustered index on the column or columns is automatically created if a clustered index on the table does not already exist and you do not specify a unique nonclustered index. The primary key column cannot allow NULL values.

3 Comments

Creating a primary key does not automatically creates a clustered index.
you are right - When you create a PRIMARY KEY constraint, a unique clustered index on the column or columns is automatically created if a clustered index on the table does not already exist and you do not specify a unique nonclustered index. The primary key column cannot allow NULL values.
I wouldn't say you were "wrong", just that the answer should have been more nuanced. Maybe replace the word "automatically" with "by default", as in this answer.
2

You can create a separate index on it as well. this article http://msdn.microsoft.com/en-us/library/aa224827(SQL.80).aspx describes the differences very well.

Comments

1

Yes, absolutely. A unique constraint creates a unique index.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.