What Is a Database Primary Key? Reliably Identifying Rows
A primary key is a column or combination of columns designated to identify each row in a table uniquely. Its constraint disallows duplicate and NULL key values, giving queries, updates, deletes, and references from other tables a stable target. An automatically increasing number is not a primary key just because it increases; the constraint must enforce uniqueness.
What problem does a primary key solve?
Business attributes such as a name or email can change or be shared by multiple people. A row’s apparent position is not a stable identifier either. A primary key gives programs and related tables an explicit identifier instead of relying on “the fourth row” or on a value that only happens to be unique.
CREATE TABLE orders (
order_id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
customer_id bigint NOT NULL,
order_date date NOT NULL,
status text NOT NULL
);
SELECT order_id, status
FROM orders
WHERE order_id = 1042;Here, order_id is the primary key, so the query can target one order. In PostgreSQL, an identity column generates values, while PRIMARY KEY enforces uniqueness and rejects NULL. The generated value and the key constraint serve different purposes; identity generation alone does not guarantee uniqueness. This example is illustrative and was not executed.
How do single-column and composite keys differ?
A single-column primary key identifies a row with one value, such as a customer or order ID. A composite primary key uses a combination of two or more columns. In an order-item table, for example, order_id and line_no together can identify an item. An order may have its own item number 1, but that number must not repeat within the same order.
CREATE TABLE order_items (
order_id bigint NOT NULL REFERENCES orders(order_id),
line_no integer NOT NULL,
product_id bigint NOT NULL,
quantity integer NOT NULL CHECK (quantity > 0),
PRIMARY KEY (order_id, line_no)
);order_id alone cannot distinguish the many items in one order. line_no alone cannot distinguish the same number across different orders. The pair identifies the row; neither column needs to be unique by itself.
How is a primary key different from UNIQUE?
Both prevent duplicates, but they have different roles. In PostgreSQL, a primary key also prohibits NULL and a table can have at most one. A table may have multiple UNIQUE constraints for other business rules. For example, customer_id can be the primary key while a separate UNIQUE rule prevents duplicate email addresses. The treatment of NULL in a UNIQUE constraint can vary between database products, so check when portability matters.
A primary key can also be the default target of foreign keys, making relationships between tables explicit. Changing a key value can affect referencing data, so keys are usually easier to maintain when kept stable and changeable business attributes are stored separately.
Should you choose a natural key or a surrogate key?
A natural key uses business data as the identifier, such as a country code or a code that is unique in its domain. Its meaning may be obvious and it can avoid a separate ID, but changes to business rules or externally edited/reused values can cascade to references in several tables. If a key contains personal information, consider whether it could be exposed in logs, URLs, or related tables.
A surrogate key, such as a database-assigned ID, has no business meaning. It can remain stable when business attributes change, but business uniqueness rules still need separate constraints such as UNIQUE. Neither approach is always better. Consider stability, meaning, exposure, reference structure, and the database’s generation features.
A practical design and review sequence
Define what one row in the table represents.
Choose the value or combination that identifies one such row.
Confirm that the values are always present and unique. PostgreSQL enforces both through the primary-key constraint.
If using a generated key, distinguish generation from uniqueness and express any business identity rule separately.
Check that foreign keys and application code refer to the key consistently.
Before adding a primary key to an existing table, resolve NULLs and duplicates. Check product- and version-specific documentation for lock, index-building, and deployment effects.
A primary key is a contract for identifying rows, not a promise of consecutive numbering or insertion order. Deletes, failed transactions, and sequence behavior can leave gaps in generated numbers. If a business process truly requires gapless numbers, design that as a separate business rule rather than relying on primary-key generation.
Sources and scope
[PostgreSQL 18: Constraints and primary keys](https://www.postgresql.org/docs/18/ddl-constraints.html)
[PostgreSQL 18: Identity columns](https://www.postgresql.org/docs/18/ddl-identity-columns.html)
[PostgreSQL 18: Table basics](https://www.postgresql.org/docs/18/ddl-basics.html)
The PostgreSQL 18 documentation was checked on 2026-11-22. Specific behavior for primary keys, UNIQUE, identity, and foreign keys is described for PostgreSQL. The SQL examples are illustrative and were not executed. For other database products, check their documentation for NULL treatment, key generation, and migration behavior.