Chapter 06 · Domains, Constraints, and Integrity

Entity Integrity, Keys, and Uniqueness

Understand entity integrity, primary and alternate keys, uniqueness, null semantics, and the constraints required to keep each stored entity unambiguously identifiable.

Beginner55–75 minutesIntegrity + key constraintsLast reviewed: August 2026

Learning outcomes

Entity integrity ensures that every row representing an entity is uniquely and reliably identifiable. Primary keys are central, but complete integrity also requires alternate-key uniqueness, non-null identifiers, and an understanding of how SQL treats nulls in unique constraints.

01

Explain why primary-key values must be unique and non-null.

02

Preserve alternate candidate keys with unique constraints.

03

Understand composite uniqueness and scope.

04

Recognize duplicate business entities hidden behind unique surrogate IDs.

The primary key identifies each row

For a relation representing Asset:

sql · example
PRIMARY KEY (asset_id)

the database guarantees that no two rows share the same asset_id and that the key is not null. This provides row identity for references and updates.

A surrogate key does not prove business uniqueness

This table allows duplicate real-world assets if no business uniqueness rule is present:

sql · example
CREATE TABLE asset (    asset_id       INTEGER PRIMARY KEY,    manufacturer_id INTEGER,    serial_number  TEXT);

Rows 100 and 101 can have identical manufacturer and serial number. The database considers them distinct because asset_id differs.

Integrity rule

Primary-key uniqueness prevents duplicate keys. Business-key uniqueness prevents duplicate business identities. They are different constraints.

Preserve alternate candidate keys

If manufacturer + serial number is guaranteed unique:

model · example
UNIQUE (manufacturer_id, serial_number)

This preserves the candidate key even when asset_id is the chosen primary key.

Composite uniqueness expresses scope

Suppose usernames are unique per tenant rather than globally:

model · example
UNIQUE (tenant_id, username)

This directly encodes the uniqueness scope. A global UNIQUE(username) would impose a stronger rule than the business requires.

Nulls and uniqueness require care

SQL implementations differ in details, but many databases permit multiple nulls under a standard unique constraint because null means unknown rather than equal to another null. If the business says “when serial_number is present it must be unique within manufacturer,” a partial/filtered unique index or database-specific strategy may be appropriate.

Required candidate keys

If an alternate candidate key truly identifies every entity instance, its columns should normally be NOT NULL in addition to unique. A nullable “candidate key” cannot identify rows where the value is absent.

Primary key stability

Primary keys should be stable because many foreign keys may reference them. Surrogate keys are often chosen specifically to avoid propagating changes in business identifiers. However, stability does not remove the need to validate business keys.

Do not use mutable names as keys

Company names, product descriptions, and person names can change and may not be unique. Using them as primary keys causes cascading updates and semantic ambiguity.

Generated IDs and import duplicates

Consider an external import:

sql · example
INSERT INTO customer (customer_id, external_customer_code, legal_name)VALUES (...)

If external_customer_code is not constrained uniquely within its source system, replaying the import can create duplicate logical customers. A surrogate primary key alone does not make ingestion idempotent.

Source-system identifiers

For multi-source integration, use scoped uniqueness:

model · example
UNIQUE (source_system_id, external_customer_id)

The same external ID may legitimately appear in two different source systems.

Association entity integrity

For ArticleTag, the pair itself may be the primary key:

sql · example
PRIMARY KEY (article_id, tag_id)

This prevents the same tag from being attached to the same article twice.

For WorkOrderAssignment, repeated assignments may be valid, so the key must match the row grain:

model · example
UNIQUE (work_order_id, technician_id, started_at)

Natural-key corrections

Suppose a serial number was entered incorrectly. Correcting it should not change asset_id if the physical asset remains the same entity. This illustrates why internal identity and business identifiers can be separate.

Duplicate detection before constraints

Adding a unique constraint to existing data may fail because duplicates already exist. Migration steps often include:

  1. identify duplicates;
  2. decide which records represent the same entity;
  3. merge or correct references;
  4. add the uniqueness constraint;
  5. monitor future violations.

WorkshopHub key design

EntityPrimary keyAdditional uniqueness
Customercustomer_idDepends on account policy/source identifiers.
Techniciantechnician_idemployee_number UNIQUE.
Partpart_idsku UNIQUE.
Assetasset_id(manufacturer_id, serial_number) when guaranteed.
WorkOrderwork_order_idHuman work-order number may be UNIQUE.

Practice: identify missing integrity

Find the problems

sql · example
CREATE TABLE user_account (    id INTEGER PRIMARY KEY,    tenant_id INTEGER NOT NULL,    username TEXT,    email TEXT);

The requirements say username is required and unique within tenant; verified email is unique globally when present. What constraints are missing?

Review answer

Add NOT NULL to username, UNIQUE(tenant_id, username), and a database-appropriate unique rule for non-null verified emails. If email verification state matters, uniqueness may belong on normalized verified email rather than every raw entered value.

Summary and next lesson

Entity integrity depends on stable row identity and preserved business uniqueness. Surrogate IDs make references convenient but do not replace candidate keys. The next lesson connects entities together safely through referential integrity and deliberate cascading actions.

References

  • E. F. Codd, “A Relational Model of Data for Large Shared Data Banks,” 1970.
  • C. J. Date, Database Design and Relational Theory.
  • PostgreSQL and SQLite documentation for primary and unique constraints.

Keep knowledge open

Help the academy stay free and grow.

If these tutorials save you time, a small donation supports new lessons, technical review, diagrams, examples, and long-term maintenance.

ETHEthereum / ERC-20 only
0x716c4Ab160C4B66F31a28AE2448BfF68fc3a2ef0

Send only assets compatible with the Ethereum/ERC-20 network. Do not send TRC-20/TRON assets.