Sub topic 6
Primary keys: identifying each row
Assume you know how tabular data works and can read basic SQL. This chapter focuses on how a relational database identifies one row from all other rows. You will also see how primary keys support foreign keys and relationships.
What a primary key is
A primary key is a column, or a group of columns, whose value uniquely identifies each row in a table. “Uniquely” means no two rows can have the same key value. A primary key also cannot be NULL, which represents a missing or unknown value.
For example, a customers table might contain several people with the same name. Their customer_id values distinguish them reliably.
| customer_id | name | |
|---|---|---|
| 1 | Ada Lovelace | [email protected] |
| 2 | Grace Hopper | [email protected] |
The database uses customer_id, not name, to identify a customer. Names can repeat or change. A good primary key remains stable.
Defining a primary key
You declare a primary key in the table definition. The following PostgreSQL example creates IDs automatically:
CREATE TABLE customers (
customer_id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE
);
INSERT INTO customers (name, email)
VALUES
('Ada Lovelace', '[email protected]'),
('Grace Hopper', '[email protected]');
SELECT customer_id, name, email
FROM customers
ORDER BY customer_id;PRIMARY KEY creates a uniqueness rule and a non-NULL rule. It also commonly causes the database to create an index, a separate lookup structure, for the key. The index helps the database find a row quickly by its ID.
A table has one primary key constraint, but that constraint can contain one column or several columns.
Choosing a key
A single generated number is called a surrogate key. It has no business meaning. It is often a practical choice because it is short, stable, and easy for other tables to reference.
A natural key uses existing business data, such as an ISBN for a book or a government-issued identifier for a person. Natural keys can be useful when the value is guaranteed to be unique and never changes. However, many real-world values can be corrected, reused, or unavailable, so they may create maintenance problems.
| Key choice | Example | Main consideration |
|---|---|---|
| Surrogate key | `customer_id = 42` | Stable and simple to reference |
| Natural key | `isbn = '978...'` | Meaningful, but may change or be invalid |
| Composite key | `(order_id, product_id)` | Represents a unique combination |
Composite primary keys
A composite primary key combines multiple columns. Each individual value may repeat, but the combination must be unique. This is useful for a junction table that records which products belong to which orders.
CREATE TABLE order_items (
order_id INTEGER NOT NULL,
product_id INTEGER NOT NULL,
quantity INTEGER NOT NULL CHECK (quantity > 0),
PRIMARY KEY (order_id, product_id)
);An order can contain many products, and a product can appear in many orders. The pair (order_id, product_id) prevents the same product from being listed twice for one order.
How primary keys connect tables
A foreign key stores a primary-key value from another table. For example, orders.customer_id can refer to customers.customer_id. The database can then reject an order that points to a customer that does not exist.
Exercise
Why is name usually a poor primary key for a customer table?
Show answer
Names may repeat, change, or contain inconsistent spelling. A stable key such as a generated customer_id identifies each customer without depending on mutable business data.