Unique Index vs Unique Constraint
Unique Index vs Unique Constraint is a SQL statement in the Indexes category. Both enforce uniqueness. A UNIQUE CONSTRAINT creates a UNIQUE INDEX internally. Direct index gives more control. The syntax is CREATE UNIQUE INDEX idx_name ON table (col); -- vs ALTER TABLE ADD CONSTRAINT ... UNIQUE (col);. It returns unique index vs constraint. A typical example: -- Method 1: UNIQUE CONSTRAINT (declarative) ALTER TABLE users ADD CONSTRAINT uq_email UNIQUE (email); -- Creates index automatically -- Better for data integrity semantics -- Method 2: UNIQUE INDEX (direct) CREATE UNIQUE INDEX uq_email ON users (email); -- Same result, but: -- - Can add INCLUDE columns -- - Can specify index options (fillfactor, etc.) -- - Can drop without altering table definition -- Both prevent duplicate values -- Both… A close relative is CREATE INDEX, which creates an index on one or more columns to speed up query performance. A close relative is DROP INDEX, which removes an existing index from a table.