title: How do I create a multiple column unique constraint in SQL Server
body: <p>I have a table that contains, for example, two fields that I want to make unique within the database.  For example:</p>
  
  <pre><code>create table Subscriber (
      ID int not null,
      DataSetId int not null,
      Email nvarchar(100) not null,
      ...
  )
  </code></pre>
  
  <p>The ID column is the primary key and both DataSetId and Email are indexed.</p>
  
  <p>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.</p>
  
  <p>I tried creating a unique index on the columns</p>
  
  <pre><code>CREATE UNIQUE NONCLUSTERED INDEX IX_Subscriber_Email
  ON Subscriber (DataSetId, Email)
  </code></pre>
  
  <p>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).</p>
  
  <p>Is there a more efficient way of achieving this type of constraint?</p>