Composite keys: identify rows with more than one column
Learn when a combination of columns should identify a row, how it differs from a multicolumn index, and what to consider for references and query performance.
Composite keys: identify rows with more than one column
A composite key uses two or more columns together to identify a row uniquely. Each column may repeat on its own, while the combination must not. For example, (order_id, product_id) can identify an order line if a product may appear only once in each order. If the same product can legitimately appear on several lines, that pair is the wrong key.
Composite key, composite UNIQUE, and composite index
A composite primary key makes the combination the table's main identity and requires every component to be unique as a set and non-null. A composite UNIQUE constraint prevents duplicate combinations but does not designate them as the table's primary identity. A multicolumn index supports searches or ordering across columns; it does not necessarily express a business uniqueness rule. In PostgreSQL, primary-key and UNIQUE constraints create unique B-tree indexes.
Decide separately which combination must be unique and which query patterns need an index. An index with several columns is not automatically a composite key.
Example: what identifies an order line?
Orders and products have a many-to-many relationship, so a junction table can record their association. If one order may contain a product only once, with a quantity on that line, (order_id, product_id) is a reasonable primary key:
CREATE TABLE order_items (
order_id bigint NOT NULL REFERENCES orders(order_id),
product_id bigint NOT NULL REFERENCES products(product_id),
quantity integer NOT NULL CHECK (quantity > 0),
PRIMARY KEY (order_id, product_id)
);The foreign keys require each line to refer to an existing order and product; the primary key rejects duplicate pairs. But if the same product can appear on separate lines because discounts, delivery dates, or terms differ, that key is too restrictive. Consider (order_id, line_number) as the primary key, or an internal order_item_id plus a UNIQUE rule for (order_id, line_number). The key should reflect the actual identity rule, not simply bundle more columns.
Choosing a key and ordering an index
A composite key fits when only the combination identifies a row, such as one enrollment per student per course session. A single surrogate primary key may simplify references when component values change often, are long, or must be repeated across many child tables. If the original combination must still be unique, preserve it with a separate UNIQUE constraint.
In PostgreSQL 18, a primary key makes all its columns unique together and NOT NULL; a multicolumn foreign key must match the referenced column count and types, and the referenced set must be unique. Check NULL behavior for UNIQUE in your DBMS; use NOT NULL where every component is required for identity.
Choose index order from actual queries. PostgreSQL's multicolumn B-tree is most effective when conditions constrain leading columns. PostgreSQL 18 also documents skip scan, which can help for some data distributions, so do not treat “leftmost columns only” as an absolute rule. Check real plans and data. More index columns also consume storage and add write work.
For example, PRIMARY KEY (course_session_id, student_id) prevents duplicate enrollment for a student in one session. A separate (student_id, course_session_id) index may help student-first lookups, but it creates no new uniqueness rule and should be justified by workload. The SQL is illustrative PostgreSQL 18 syntax and was not executed.
Before deciding, ask which combination defines a row, whether all its values are required, what happens to references if a value changes, and which leading-column patterns queries use.
Sources
[PostgreSQL 18: Constraints](https://www.postgresql.org/docs/18/ddl-constraints.html)
[PostgreSQL 18: Multicolumn indexes](https://www.postgresql.org/docs/18/indexes-multicolumn.html)
Documentation checked 2026-11-23. Index benefits depend on data and query plans.