Skip to main content

Unique Index

Editorial

Introduction

A unique index serves a dual purpose: it provides the performance benefits of a regular index AND enforces a business rule — no two rows can have the same value in the indexed column(s). It is one of the most practical index types you will use.

In this tutorial, you will learn:

  • What a unique index is and how it differs from a regular index
  • The relationship between unique indexes and constraints
  • How to create unique indexes
  • How unique indexes handle NULL values
  • When and why to use unique indexes

What is a Unique Index?

A unique index guarantees that no two rows in the table can have the same value (or combination of values) in the indexed column(s). If someone tries to insert or update a row that would create a duplicate, the database rejects the operation with an error.

Generated visual representation from text diagram for What is a Unique Index?.

Unique Index vs UNIQUE Constraint

There are two ways to enforce uniqueness in SQL Server. They are functionally almost identical:

Method 1: CREATE UNIQUE INDEX

-- Using CREATE UNIQUE INDEX
CREATE UNIQUE NONCLUSTERED INDEX UX_Customers_Email
ON customers (email);

Method 2: UNIQUE Constraint

-- Using a UNIQUE CONSTRAINT (creates a unique index behind the scenes)
ALTER TABLE customers
ADD CONSTRAINT UQ_Customers_Email UNIQUE (email);

Comparison

FeatureUNIQUE INDEXUNIQUE CONSTRAINT
Enforces uniquenessYesYes
Creates an indexYes (you define it)Yes (auto-created behind the scenes)
INCLUDE columnsYesNo
FilteredYes (WHERE clause)No
Referenced by FKNoYes
Shows in constraintsNoYes
FlexibilityMore optionsSimpler syntax

When to Use Which

ScenarioRecommendation
Need a foreign key referenceUse UNIQUE CONSTRAINT
Need INCLUDE columns or filteringUse UNIQUE INDEX
Simple uniqueness enforcementEither works
Need the index to be self-documenting as a constraintUse UNIQUE CONSTRAINT
Diagram showing how unique indexes enforce uniqueness and the difference between unique index and unique constraint

Creating Unique Indexes

Single Column Unique Index

-- Ensure each customer has a unique email
CREATE UNIQUE NONCLUSTERED INDEX UX_Customers_Email
ON customers (email);

Composite Unique Index

-- Ensure each product appears only once per order
CREATE UNIQUE NONCLUSTERED INDEX UX_OrderItems_Order_Product
ON order_items (order_id, product_id);

Unique Index with INCLUDE

-- Unique on email, but also include name for covering
CREATE UNIQUE NONCLUSTERED INDEX UX_Customers_Email_Cover
ON customers (email)
INCLUDE (customer_city, customer_state);

Unique Clustered Index

-- A unique clustered index (this is what PRIMARY KEY creates by default)
CREATE UNIQUE CLUSTERED INDEX UCX_Orders_OrderID
ON orders (order_id);

NULL Handling in Unique Indexes

In SQL Server, a unique index treats NULL as a value for uniqueness purposes. This means only one NULL is allowed in a unique index column.

Generated visual representation from text diagram for NULL Handling in Unique Indexes.

Workaround: Allow Multiple NULLs

If you need to allow multiple NULL values while enforcing uniqueness for non-NULL values, use a filtered unique index:

-- Allow multiple NULLs, but enforce uniqueness for non-NULL values
CREATE UNIQUE NONCLUSTERED INDEX UX_Customers_Phone
ON customers (phone_number)
WHERE phone_number IS NOT NULL;

Summary

Here is what you should remember about unique indexes:

ConceptKey Takeaway
WhatAn index that prevents duplicate values in the indexed column(s)
Dual purposePerformance improvement + data integrity enforcement
NULL handlingOnly one NULL allowed (in SQL Server)
vs ConstraintMore flexible (supports INCLUDE, WHERE) but not FK-referenceable
Primary KeyAutomatically creates a unique clustered index
Multiple NULLsUse a filtered unique index to allow them

Common Use Cases

ColumnWhy Unique
Email addressEach customer must have a distinct email
UsernameNo two users should share a username
Order + Product comboEach product appears once per order
Employee IDNatural business identifier
Social Security NumberGovernment-issued unique identifier

In the next tutorial, you will learn about Filtered Indexes — indexes that only cover a subset of rows.