Uniqueness for a Column or Combination

A uniqueness constraint gives the database responsibility for rejecting duplicate keys according to its rules.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Trace a slug
  2. Check null and multiple fields
  3. Try a duplicate inside and outside the intended scope
  4. Determine how the database treats null and text equality

Trace a slug

Two articles may have different identifiers and the same proposed slug. Uniqueness can reject the duplicate public path while leaving titles unconstrained. Stable address design explains why the slug has a distinct role.

Check null and multiple fields

Several SQL dialects or options treat nulls in unique keys differently. Tests should match the target database. See composite keys when the constraint covers a pair rather than one column.[1][2]

Try a duplicate inside and outside the intended scope

A tag code may be unique within a project while several projects use code blue. The unique pair (project_id, code) permits one blue tag in each. A uniqueness rule on code alone would reject the second project. See composite keys in SQL.

(1,blue) → accept
(2,blue) → accept
(1,green) → accept
Repeat (1,blue) → reject

Determine how the database treats null and text equality

Null uniqueness policy can differ by system or constraint option. Collation and normalization can also affect whether spelling variants compare equal. Test duplicate input through an actual insert, including concurrent attempts if relevant.

References

  1. PostgreSQL Global Development Group, Data Definition ↩
  2. PostgreSQL Global Development Group, SQL Language Tutorial ↩