Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the Sql Server Unique Key also an Index?

I've got a column in a table (eg. UserName) which I want to make sure is unique. So I create a unique key for that column and call it IX_Users_UserName.

Now, if I do lots of searching for users based on their username I want to make sure there is an index for that field.

Do I need to create a separate index, or is the unique key also considered an index, just like the primary key is a clustered unique key?

like image 708
Pure.Krome Avatar asked Dec 14 '08 04:12

Pure.Krome


People also ask

Is Unique Key also an index?

Unique keys are indexes. If your values are guaranteed to be unique, this is the best choice. Unique Key: Unique Key enforces uniqueness of the column on which they are defined. Unique Key creates a non-clustered index on the column.

Does unique key create index SQL Server?

We know that the unique constraint in SQL Server creates a unique SQL Server index as well. SQL Server allows us to disable an index as well without dropping it.

Is unique an index in SQL?

Provided that the data in each column is unique, you can create both a unique clustered index and multiple unique nonclustered indexes on the same table. Unique indexes ensure the data integrity of the defined columns.

Is a unique constraint the same as an index?

A unique index ensures that the values in the index key columns are unique. A unique constraint also guarantees that no duplicate values can be inserted into the column(s) on which the constraint is created. When a unique constraint is created a corresponding unique index is automatically created on the column(s).


1 Answers

Unique Key: Unique Key enforces uniqueness of the column on which they are defined. Unique Key creates a non-clustered index on the column. Unique Key allows only one NULL Value.

Alter table to add unique constraint to column:

ALTER TABLE Authors ADD CONSTRAINT IX_Authors_Name UNIQUE(Name) GO

Source

More information from MSDN.

FWIW -- if your constraint doesn't create an index, I would avoid naming it IX_ as that would typically be assumed to be associated with one (IX = Index).

like image 62
tvanfosson Avatar answered Nov 07 '22 02:11

tvanfosson