Sub topic 1
Why Tables Need Generated IDs
Prerequisites
This chapter assumes that you know basic SQL, including CREATE TABLE, INSERT, and SELECT. You should also know that a primary key identifies each row and that PostgreSQL has integer types such as smallint, integer, and bigint.
The problem with identifying rows
A table needs a reliable way to distinguish one row from every other row. For example, an orders table might contain several orders from the same customer:
CREATE TABLE orders (
customer_name text NOT NULL,
amount numeric(10, 2) NOT NULL
);Neither customer_name nor amount identifies an order. A customer can place multiple orders, and two orders can have the same amount. Searching with these values could match more than one row.
A primary key provides a stable identifier. It must be unique and should not change when other information about the row changes.
Natural keys and surrogate keys
A natural key uses information that already describes the real-world object. An email address can identify a user if the application guarantees that every address is unique and never changes.
A surrogate key is an artificial identifier created for the database. It has no meaning outside the table. A generated numeric ID is a common surrogate key:
CREATE TABLE orders (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
customer_name text NOT NULL,
amount numeric(10, 2) NOT NULL
);
INSERT INTO orders (customer_name, amount)
VALUES ('Ada', 25.00), ('Ada', 40.00);PostgreSQL generates a different id for each inserted row. The application does not need to find an unused number or coordinate ID assignment between several database clients.
| Key choice | Main benefit | Common concern |
|---|---|---|
| Natural key | Represents a real business value | The value may change or fail to be unique |
| Generated numeric key | Stable and simple to reference | The number has no business meaning |
| UUID key | Can be generated across systems | Larger and less readable than an integer |
Why generated IDs help relationships
Other tables can refer to an order through its ID. This reference is called a foreign key. It avoids copying several descriptive columns into every related row.
CREATE TABLE order_items (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
order_id bigint NOT NULL REFERENCES orders(id),
product text NOT NULL,
quantity integer NOT NULL
);The order_id column says which order owns an item. If the customer's name changes, the relationship still points to the same order because it uses the stable orders.id value.
Generated IDs also make updates and deletes precise:
UPDATE orders
SET amount = 30.00
WHERE id = 1;The primary key constraint rejects a second row with the same ID. The identity feature generates values, while the primary key enforces uniqueness. These are separate responsibilities.
Choosing a key
Use a generated numeric ID when rows need a compact, stable database identifier and the database owns the data. Use a natural key only when its uniqueness and stability are genuine business guarantees. Choose a UUID when records must be created independently across databases or exposed without predictable numbering.
The next chapters examine how PostgreSQL generates identity values and how to choose its generation rules.
Exercise
Why is customer_name a poor primary key for the orders table, and what does orders.id provide instead?
Show answer
A customer can place multiple orders, so customer_name is not unique. orders.id provides a stable, unique identifier for each order.