Learn SQL
-- constraints.composite_partial_unique

Composite, Partial & Expression UNIQUE in SQL

Scoped uniqueness: live rows, lowered emails, pair keys

--concept

PRIMARY KEY is singular and never NULL; UNIQUE repeats across columns and (in PG/standard) allows multiple NULLs. Composite keys identify by combination (order_id, line_no; tenant_id, email). Partial unique (WHERE deleted_at IS NULL) scopes to live rows β€” the soft-delete answer. Expression unique (LOWER(email)) normalizes before comparing. Deferrable (INITIALLY DEFERRED) checks at COMMIT for cyclic inserts.

--how it works
1

Composite identifies pairs

PK (order_id, line_no); UNIQUE (tenant_id, email) β€” combination carries identity.

2

Partial scopes liveness

WHERE deleted_at IS NULL β€” archived dupes allowed, live enforced.

3

Expression normalizes

LOWER(email) β€” case-insensitive without app discipline.

4

Defer cycles to COMMIT

DEFERRABLE INITIALLY DEFERRED β€” A↔B inserts in one txn; sparingly (late errors).

--visualization
ux_active_email: LOWER(email) WHERE deleted_at IS NULL
caseverdict
live Ada vs ADAconflict
archived reuseallowed
partial: live only expression: lowered
Stage 1 / 1
Composite PK (student,course) identifies pairs. DEFERRABLE checks cycles at COMMIT.
--example
query.sql
1CREATE UNIQUE INDEX ux_active_email ON users (LOWER(email)) WHERE deleted_at IS NULL;
Result
tooljob
partiallive rows only
expressioncase-insensitive

Ada@x.com and ADA@x.com conflict (lowered); archived duplicates (deleted_at set) do not. Triggers lose to this for clarity and concurrency β€” the index is the validator.

--variants

Enrollment junction

Two FKs + composite PK + attribute.

query.sql
1CREATE TABLE enrollments (
2 student_id BIGINT REFERENCES students(id) ON DELETE CASCADE,
3 course_id BIGINT REFERENCES courses(id) ON DELETE CASCADE,
4 enrolled_at DATE NOT NULL DEFAULT CURRENT_DATE,
5 PRIMARY KEY (student_id, course_id)
6);
Result
elementrole
PK(student_id, course_id)
attrsenrolled_at rides along
1Pair is unique; single ids repeat freely.
2Composite indexes serve both directions (add reverse too).
3Junction gains attributes when the relation matters.

Live-only lowered email

Conditional + normalized in one index.

query.sql
1CREATE UNIQUE INDEX ux_active_email ON users (LOWER(email)) WHERE deleted_at IS NULL;
Result
caseverdict
live Ada vs ADAconflict
archived dupesallowed
1Lowering collapses case variants before compare.
2Partial skips soft-deleted rows.
3Beats triggers for clarity + concurrency.
--key facts
Compositepair identifies β€” junctions, lines
Partialconditions β€” soft-delete scoping
Expressionnormalization β€” case-insensitive email
--use cases
Multi-tenant uniqueness + soft-delete reuse
Line-items, enrollments, effective-dated keys
--under the hood
Unique enforced via unique B-tree β€” probes on write; partial keeps it small; expression stores computed keys.
Deferrable checks at COMMIT delay attribution β€” SET CONSTRAINTS toggles per txn.

Recommended resourcesAffiliate links Β· may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice β€” pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from β€” great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.