Composite Key
A composite key uses two or more columns together to uniquely identify a row. It is useful when no single column is sufficient to establish row identity.
A composite key is a database key made from multiple columns. The database enforces uniqueness on the combination, so two rows may share a value in one component column as long as their complete combinations differ.
For example, an `order_items` table might identify an item by `(order_id, product_id)`. The same product can appear in many orders, and an order can contain many products, but the same product should not appear twice for the same order.
A composite primary key also implies that the participating columns are part of the table's primary-key constraint. Primary-key columns cannot contain `NULL`, and the complete key value must be unique.
In Java backend applications, composite keys commonly appear when mapping relational tables with JPA/Hibernate. `@EmbeddedId` is one standard approach; `@IdClass` is another. The database constraint remains the authoritative rule for uniqueness regardless of the Java mapping style.
Key points
- A composite key contains multiple columns.
- Uniqueness applies to the complete column combination.
- A composite primary key cannot contain `NULL` values.
- Common examples include junction tables and order-line tables.
- JPA/Hibernate can map composite identifiers with `@EmbeddedId` or `@IdClass`.
Interview questions
What is a composite key?
A composite key is a key made from multiple columns. The combination of their values uniquely identifies a row. Each individual column can contain duplicate values, provided the complete combination is unique. A common example is `(order_id, product_id)` in an order-items table.
Why would you use a composite primary key for an order-items table?
An order-items table often represents which products belong to which orders. `order_id` alone is not unique because an order can contain multiple products, and `product_id` alone is not unique because a product can occur in many orders. The pair `(order_id, product_id)` can identify the relationship uniquely. This also prevents the same product from being added twice to the same order when that is prohibited by the data model.
Can a composite primary key contain NULL values?
No. A primary key cannot contain `NULL` values, including when the primary key consists of multiple columns. Every component of the composite primary key must therefore have a value. This allows the complete key to reliably identify a row.
How can JPA/Hibernate map a composite primary key?
JPA provides approaches such as `@EmbeddedId` and `@IdClass` for composite identifiers. With `@EmbeddedId`, the key fields are typically grouped into a separate embeddable key class. With `@IdClass`, the entity declares its identifier fields directly while a separate ID class represents the combined identifier. The key class needs appropriate equality semantics so identity comparisons behave correctly.
What are the trade-offs of a composite key compared with a surrogate key?
A composite key can express natural domain identity directly and can eliminate the need for an additional identifier. However, wider keys can make foreign keys and joins more cumbersome, and changing a natural component can be more disruptive. A surrogate key simplifies references because other tables need to store only one identifier, but uniqueness of the underlying business data may still require a separate constraint. The appropriate choice depends on the domain and how the identifier is used.