Sub topic 3
Primary keys and identity columns
This chapter assumes you know basic CREATE TABLE, INSERT, and SELECT statements, and that you understand tables contain rows and columns.
A primary key is a column, or group of columns, that uniquely identifies each row in a table. PostgreSQL enforces two rules for a primary key:
- No two rows can have the same key value.
- The key value cannot be
NULL.
Use a stable identifier for a row instead of using a value such as a book title. Titles can change, and different books can share a title. An identifier should remain the same even when descriptive data changes.
Defining an identity primary key
An identity column asks PostgreSQL to generate a value when you insert a row without specifying one. GENERATED ALWAYS AS IDENTITY is the strict form: applications normally cannot provide their own value unless they explicitly override the rule.
CREATE TABLE authors (
author_id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
name text NOT NULL
);
CREATE TABLE books (
book_id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
title text NOT NULL
);
INSERT INTO authors (name)
VALUES ('Octavia Butler')
RETURNING author_id, name;
INSERT INTO books (title)
VALUES ('Kindred')
RETURNING book_id, title;
SELECT book_id, title
FROM books
ORDER BY book_id;The RETURNING clause gives you the generated key immediately. You can use that key when inserting related rows, such as a row in a book-author join table.
bigint provides a large range of integer values. The identity mechanism uses an internal sequence to generate values. The generated values are usually increasing, but they are not guaranteed to be consecutive. A rolled-back insert can consume a value, and concurrent transactions can reserve values in an order different from the order in which rows become visible.
Choosing between generated options
PostgreSQL also supports GENERATED BY DEFAULT AS IDENTITY. It generates a value when you omit the column, but it permits an explicitly supplied value. This can help with controlled data imports, but it gives callers more control than ALWAYS.
Older schemas often use serial or bigserial. These create an integer column and a separate sequence through shorthand behavior. Identity columns express the relationship more clearly and follow the SQL standard, so prefer identity columns for new PostgreSQL tables.
| Definition | Omitted value | Explicit value |
|---|---|---|
| `GENERATED ALWAYS AS IDENTITY` | Generated | Rejected by default |
| `GENERATED BY DEFAULT AS IDENTITY` | Generated | Accepted |
| `bigserial` | Generated through a sequence | Usually accepted |
How the key connects tables
A later table can store a book's book_id as a foreign key. The foreign key points to the primary key and prevents references to a book that does not exist.
Exercise
Why should a book title usually not be the primary key?
Show answer
A title can change and may not be unique, while a generated identifier remains stable and uniquely identifies one row.