Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a multiple column unique constraint in SQL Server

Tags:

I have a table that contains, for example, two fields that I want to make unique within the database. For example:

create table Subscriber (     ID int not null,     DataSetId int not null,     Email nvarchar(100) not null,     ... ) 

The ID column is the primary key and both DataSetId and Email are indexed.

What I want to be able to do is prevent the same Email and DataSetId combination appearing in the table or, to put it another way, the Email value must be unique for a given DataSetId.

I tried creating a unique index on the columns

CREATE UNIQUE NONCLUSTERED INDEX IX_Subscriber_Email ON Subscriber (DataSetId, Email) 

but I found that this had quite a significant impact on search times (when searching for an email address for example - there are 1.5 million rows in the table).

Is there a more efficient way of achieving this type of constraint?

like image 664
Neilski Avatar asked Aug 01 '12 07:08

Neilski


People also ask

How do I make multiple columns unique in SQL?

SQL UNIQUE constraint for 2 columns example Notice that we named the UNIQUE constraints using CONSTRAINT keyword. We can use this name to remove the UNIQUE constraint later if we want. To define a UNIQUE on multiple columns, we put a comma-separated columns list inside parenthesis that follows the UNIQUE keyword.

Can unique key created on multiple columns?

UNIQUE key does not allow duplicate values. UNIQUE key allows NULL values but does not allow NULL values multiple times. We can create multiple UNIQUE columns on one table however only one PRIMARY KEY for table. Defining primary key on a column has a UNIQUE constraint property by default.

Can unique constraints be multiple?

Both the UNIQUE and PRIMARY KEY constraints provide a guarantee for uniqueness for a column or set of columns. A PRIMARY KEY constraint automatically has a UNIQUE constraint. However, you can have many UNIQUE constraints per table, but only one PRIMARY KEY constraint per table.


1 Answers

but I found that this had quite a significant impact on search times (when searching for an email address for example

The index you defined on (DataSetId, Email) cannot be used for searches based on email. If you would create an index with the Email field at the leftmost position, it could be used:

CREATE UNIQUE NONCLUSTERED INDEX IX_Subscriber_Email    ON Subscriber (Email, DataSetId); 

This index would server both as a unique constraint enforcement and as a means to quickly search for an email. This index though cannot be used to quickly search for a specific DataSetId.

The gist of it if is that whenever you define a multikey index, it can be used only for searches in the order of the keys. An index on (A, B, C) can be used to seek values on column A, for searching values on both A and B or to search values on all three columns A, B and C. However it cannot be used to search values on B or on C alone.

like image 82
Remus Rusanu Avatar answered Oct 04 '22 16:10

Remus Rusanu