SQLMentor // glossary

Composite Key

A composite key is a primary or unique key made up of two or more columns together, used when no single column uniquely identifies a row on its own.

The classic case is a many-to-many join table: an order_items table linking orders to products might have neither order_id nor product_id unique alone, but the pair (order_id, product_id) together always is.

Queries and foreign keys that reference a composite key need to reference every column in it together — you can't use just part of a composite key as if it were a complete, independent identifier.

Example

CREATE TABLE order_items (
  order_id   INT,
  product_id INT,
  quantity   INT,
  PRIMARY KEY (order_id, product_id)
);