Junior — Middle
Have you had experience working with databases that used multiple natural keys?
sobes.tech AI
Answer from AI
Yes, composite (multi) natural keys are sometimes used in databases, consisting of multiple fields that reflect unique business attributes.
For example, in an orders table, uniqueness can be defined by a combination of order_number and store_id, if order numbers are repeated in different stores.
Working with such keys requires careful construction of queries and indexes, and in ORM — correct description of the composite key.
Example in SQL:
CREATE TABLE orders (
order_number INT,
store_id INT,
order_date DATE,
PRIMARY KEY (order_number, store_id)
);
In Java with JPA, a separate class with the @Embeddable annotation is usually created for the composite key and used in the entity with @EmbeddedId.