One-to-One Relationships with a Unique Reference

A foreign key with a uniqueness constraint can limit the number of rows pointing at a parent to at most one.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Trace optional profile data
  2. Choose the row layout from use
  3. Show what the unique foreign key prevents
  4. Check both directions of the claimed cardinality

Trace optional profile data

An account may have one profile settings row identified by its account ID. An account can still have none unless another rule ensures existence. See foreign keys and uniqueness constraints.

Choose the row layout from use

If the fields always exist and share one lifecycle, they may fit in one table. A separate table can model optional data with its own visibility or operational boundary.[1][2]

Show what the unique foreign key prevents

A profile extension can reference an account and have a unique constraint on account ID. A second extension for the same account fails. An account can still have zero extensions unless the model separately requires extension existence. See unique fields and combinations.

Account 17 exists
First non-null extension for 17 → accepted
Second extension for 17 → rejected
Account 18 can still have no extension under this schema

Check both directions of the claimed cardinality

The presence of one reference provides only the guarantees actually declared. If extension must be exactly one, consider creation and deletion transaction rules. Test empty, one and duplicate cases.

References

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