Composite keys should never be considered in "new" applications. They were used in the past by people who are used to thinking that “business keys” are better than “surrogate keys”.
Edit: As Chris said, I am expanding my answer.
Let me start by stating that I understand this question as “composite primary keys” and “surrogate keys”.
In addition, I acknowledge that there is one use case for a complex key: in crosstab lookup tables, also called “link tables”. They are used in many tables and consist of only two fields: both foreign keys, which form the primary key for the xref table. For example, the UserRole table contains user_id and role_id , nothing more. There is no class representation in Java, for example, for a table like this. This is usually @ManyToMany , with Collection on both sides.
I shared my views on the Natural Keys and Surrogate Keys keys in another answer ( Hibernate: views in Composite PK vs Surrogate PK ), and I believe that composite keys share some of the disadvantages of the Natural Key, without bringing any real benefits.
The problem with compound keys is that you need two values to uniquely identify the record. This becomes a problem when you start having tables that reference records in this first table. The second table requires two columns in order to be able to refer to one record. And if this second table uses a composite key consisting of one value + foreign key, now you have three columns to uniquely identify one record. And the third table will require these three additional columns only to refer to one record in the second table. This is actually a snow globe.
Another disadvantage is that the requirements are changing. All the time. So, what today seems like a good composite key is not a key today. That is why we have surrogate keys: to be future.
Composite keys are mainly used to make entries in a table unique based on a set of columns. For example, if you have a Customers table, you can have NationalId + Country as a unique value, which means that two users cannot use the same SSN if their country is USA. But it is possible to have the same number for two records if they are not in the same country. If you like compound keys, this would be a good candidate for this. But, as I hinted earlier, you can use a surrogate key and apply a unique constraint. You will have the benefits of a composite key plus surrogate key security.
jpkrohling
source share