Relational Constructs   «Prev  Next»

Lesson 11 Primary Keys
Objective Define database primary keys.

What Is a Database Primary Key?

A primary key is the chosen column or combination of columns that uniquely identifies each row in a database table. Its values must be unique and its columns must not contain NULL. A table has at most one primary-key constraint, but that key can contain several columns.

For example, CustomerID can identify a customer even when two customers have the same name. An enforced primary-key constraint rejects a duplicate key; it does not choose an arbitrary matching row.

A foreign key has a different role: it references an eligible key in another table (or the same table). Many foreign-key values may repeat unless an additional uniqueness constraint prevents this.

Entity Identifier in an ERD vs. Primary Key in a Database Table

The entity identifier in an Entity Relationship Diagram and the primary key in a database table are closely related but serve different roles at different stages of the database development process.

Entity Identifier - Conceptual Level

The entity identifier is defined during the design phase of the DBLC, within the ERD. It represents a unique attribute or combination of attributes that can distinguish each instance of an entity from every other instance. The entity identifier is an abstract, design-level concept - it describes what makes an entity instance unique without specifying how that uniqueness will be enforced in the physical database.

For a Customer entity, CustomerID can be an identifier if the business rules make it unique. FirstName and LastName are not reliable identifiers: different people can share both. Diagram conventions vary, so provide a legend for underlining, PK labels, or other symbols.

Primary Key - Physical Level

The primary key is the physical implementation of the entity identifier when the database is created. It is a concrete constraint defined in the database schema using SQL. Where the entity identifier is a design decision, the primary key is an enforceable rule built into the table structure.

A primary key in a database table has two defining requirements: uniqueness and non-nullability. Indexes are a separate implementation consideration:

The transition from ERD to database implementation maps directly: CustomerID as the entity identifier in the ERD becomes the PRIMARY KEY constraint on the CustomerID column in the Customer table. The entity identifier describes the concept; the primary key enforces it.

Database Design and Theory

Characteristics of a Good Primary Key

Separate mandatory constraints from design preferences:

  1. Unique: no two rows have the same complete key.
  2. Required: every key column has a non-NULL value.
  3. Minimal as a candidate key: no unnecessary attribute is included.
  4. Stable: prefer values that will not change. Changes may be blocked or require coordinated updates to references.
  5. Suitable representation: choose a data type with adequate capacity and consider index and foreign-key storage costs.
  6. Appropriate to the rule: a natural key, generated surrogate key, or composite key may fit. Sequential integers are common, but are not a requirement.

Characteristics of a Bad Primary Key

A person's name, address, or email can repeat or change. Sensitive identifiers also spread sensitive data into referencing tables. These properties often make such values poor choices for a widely referenced primary key.

A surrogate key avoids tying row identity to mutable descriptions, but it does not enforce business uniqueness by itself. If customer numbers must be unique, declare that rule separately even when CustomerID is the primary key.

Rules for Designating a Primary Key

Primary-key columns must be non-NULL. NULL is a marker for missing or unknown information, not zero or an empty string. Ordinary SQL equality does not treat NULL = NULL as true; use IS NULL to test for it. Do not explain the primary-key rule by claiming that NULL equals NULL.

NULL handling in a nullable UNIQUE constraint differs across DBMSs. When representing an alternate candidate key, explicitly declare both uniqueness and non-nullability.

SQL example: primary key and alternate key

CREATE TABLE Customer (
    CustomerID     INT NOT NULL PRIMARY KEY,
    CustomerNumber VARCHAR(20) NOT NULL UNIQUE,
    CustomerName   VARCHAR(100) NOT NULL
);

INSERT INTO Customer (CustomerID, CustomerNumber, CustomerName)
VALUES (101, 'C-001', 'Ava');
INSERT INTO Customer (CustomerID, CustomerNumber, CustomerName)
VALUES (102, 'C-002', 'Ben');
INSERT INTO Customer (CustomerID, CustomerNumber, CustomerName)
VALUES (103, 'C-003', 'Ava');

The repeated name Ava is allowed. Reusing CustomerID 101, inserting a NULL customer number, or reusing customer number C-001 is rejected by the respective constraint. IDs are supplied explicitly here; automatic generation syntax is DBMS-specific.

Candidate Keys

A candidate key is a minimal set of attributes that uniquely identifies every row under the business rules. Minimal means no proper subset is sufficient. Choose one candidate as the primary key; the others are alternate keys.

CustomerID is the primary key; CustomerNumber is a required unique alternate candidate key; CustomerName repeats and is not a candidate key.
CustomerID and CustomerNumber are candidate keys under the stated rules. CustomerID is chosen as primary; the SQL enforces CustomerNumber with UNIQUE and NOT NULL. Names need not be unique.

Do not infer candidate keys solely from a small sample: a column that happens to contain distinct values today may repeat tomorrow. Establish the rule first, then enforce it.

Can a primary key contain more than one column?

Yes. A concatenated or composite primary key[1] uses a combination, for example PRIMARY KEY (StudentID, CourseID) when each student enrolls once per course offering. Neither component needs to be unique by itself.

The next lesson examines surrogate keys. For a worked multi-column example, see concatenated primary keys.

Implementation reference: PostgreSQL documentation on primary, unique, and foreign-key constraints.

[1] Concatenated primary key: A primary key made up of more than one field. Also called a composite primary key.

SEMrush Software 11 SEMrush Banner 11