What is a unique index?
SQLLevel: IntermediateDatabase Design
What is a unique index?
Hints
- How is lookup performed without the structure and what changes with it?
- Check selectivity, key order and the cost of data updates.
Answer
A unique index enforces that all values in the indexed column(s) are distinct. It's both a performance tool and a data integrity constraint.
CREATE UNIQUE INDEX idx_users_email ON users(email);
-- or via constraint:
ALTER TABLE users ADD CONSTRAINT uq_email UNIQUE (email);
Primary keys automatically create a unique index.
Source and licence: SQL Interview Questions — Arda Kocadoru · MIT