Sub topic 8
Adding a primary key: identity alone is not unique
Assume you know basic CREATE TABLE, INSERT, and SELECT statements, and that a primary key identifies each row. This chapter focuses on an important PostgreSQL distinction: an identity column generates values, but it does not automatically make those values unique.
What an identity column guarantees
An identity column gets its values from a sequence. When an insert omits the identity column, PostgreSQL asks the sequence for the next value.
The identity property controls how values are generated and whether explicit values are accepted. It does not add a PRIMARY KEY or UNIQUE constraint.
CREATE TABLE invoices (
id bigint GENERATED ALWAYS AS IDENTITY,
detail text NOT NULL
);
INSERT INTO invoices (detail) VALUES ('first'), ('second');
SELECT id, detail
FROM invoices;The table normally contains IDs such as 1 and 2. However, PostgreSQL does not consider id unique merely because it is an identity column.
With GENERATED ALWAYS, an explicit value requires an override clause. This example deliberately inserts a duplicate ID:
INSERT INTO invoices (id, detail)
OVERRIDING SYSTEM VALUE
VALUES (1, 'duplicate ID');
SELECT id, detail
FROM invoices;The insert succeeds because invoices.id has no uniqueness constraint. The sequence also does not inspect existing rows before producing its next value.
What a primary key adds
A primary key is a table constraint that requires every value to be both unique and non-null. It gives the database a rule to check whenever a row is inserted or updated.
CREATE TABLE payments (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
amount numeric(12, 2) NOT NULL
);
INSERT INTO payments (amount)
VALUES (19.99), (42.50)
RETURNING id, amount;Here, PRIMARY KEY combines two properties:
NOT NULL: every payment must have an ID.UNIQUE: no two payments can have the same ID.
The identity property supplies ordinary IDs, while the primary key rejects collisions. These properties solve different problems.
Choosing a constraint
Use a primary key when the column is the row's main identifier. Use a UNIQUE constraint when the column must be unique but is not the table's main identifier.
| Definition | Generates values | Requires non-null | Enforces uniqueness |
|---|---|---|---|
| Identity only | Yes | No | No |
| Identity with `UNIQUE` | Yes | No | Yes |
| Identity with `PRIMARY KEY` | Yes | Yes | Yes |
An identity column can also be part of a composite primary key, although the other columns then participate in the uniqueness rule:
CREATE TABLE order_lines (
order_id bigint NOT NULL,
line_id integer GENERATED ALWAYS AS IDENTITY,
product_id bigint NOT NULL,
PRIMARY KEY (order_id, line_id)
);In this design, line_id values are generated across the whole table. The pair (order_id, line_id) identifies each line.
Exercise
Does GENERATED BY DEFAULT AS IDENTITY guarantee that a column is unique?
Show answer
No. It generates a value when one is omitted, but uniqueness requires a PRIMARY KEY or UNIQUE constraint.