liveBook · Manning

Unique constraint

As explained in Just Use Postgres!

A unique constraint requires that values in a specified column or set of columns be distinct within a table.

A unique constraint protects data integrity by requiring values in a specified column or group of columns to remain distinct within a table. section 2.3.1

A unique constraint supports relational integrity by:

  • Preventing duplicate values in the constrained column or column group. section 2.3.1
  • Allowing a foreign key to reference a column that is not a primary key, provided that column has an associated unique constraint. section 2.3.2
  • Supporting fast foreign-key verification through the index created for unique columns. section 2.3.2

Using a primary key as the unique reference

The products.catalog table uses id as its primary key, so id is unique by definition and can be referenced by products.reviews.product_id. A foreign-key definition would fail if the referenced catalog column had no matching unique constraint. section 2.3.2

How Just Use Postgres! covers it

How deep it goes
Defines uniqueness and explains its foreign-key and index implications, but does not demonstrate creating a standalone unique constraint.
Examples
Uses products.catalog.id as a primary-key example and shows the error caused by referencing a non-unique column.
What it assumes
Assumes familiarity with tables and columns while distinguishing unique constraints from primary keys and foreign keys.
What you can do after
Evaluate whether a column can serve as a foreign-key target by checking for primary-key or unique-column support.
How it connects
Connects primary keys, foreign keys, indexes, and database integrity.

Also explained in

Compare how 7 books explain unique constraint →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Just Use Postgres!. The section links point to the parts of the book it draws on.