Hello! In our last lesson, we established that a primary key is essential for uniquely identifying every record in a table. We focused on primary keys made from a single attribute, like StudID. We even saw in the practice exercise that a combination like {StudFname, StudLname} could also be a unique identifier, which sets the stage perfectly for today.
This lesson tackles the question: what happens when no single attribute is enough to guarantee uniqueness? We will define what a composite primary key is and, more importantly, explore the specific situations where using one is not just an option, but a necessity. Understanding this is crucial for accurately modeling real-world scenarios in your database design.
1. Defining the Composite Primary Key
In the previous lesson, we identified candidate keys and selected one as the primary key. Sometimes, the only way to create a candidate key is by combining two or more attributes. When such a key is chosen as the primary key, it is called a composite primary key.
A composite primary key is a primary key that consists of two or more attributes that, when combined, uniquely identify an entity instance.
A critical rule for any key, including a composite one, is that it must be minimal. This means that if you remove any attribute from the key, it will no longer be unique.
Chapter 8 The Entity Relationship Data Model
Let's start with a formal definition from the "Database Design" textbook.
Read the short section titled Composite key. It provides a concise definition and a simple example.
Now, let's see this illustrated with a different, very intuitive example.
Database Keys Made Easy - Primary, Foreign, Candidate, Surrogate, & Many More
This clip from the video "Database Keys Made Easy" by Decomplexify gives a clear, practical example of why a composite key might be needed.
Watch the segment on composite keys. Notice how in the coffee options table, neither coffee type nor cup size alone can uniquely identify a price, but the combination of both can.
The combination {coffee type, cup size} is a composite primary key. It is minimal because if you only used coffee type, you wouldn't know which price to pick (small, medium, or large), and if you only used cup size, you wouldn't know which coffee it was for.
2. When is a Composite Key Necessary?
Now for the crucial part: when do you actually need to use one? There are two main scenarios you will encounter frequently in database design.
Scenario 1: No Single Attribute is Unique
Sometimes, the nature of an entity is such that no single piece of information about it is unique on its own. The combination of several pieces of information, however, is.
The following video provides an excellent example involving shipments for an order.
Entity Relationship Diagram (ERD) Tutorial - Part 2: Primary keys, foreign keys, and bridge tables
Watch this segment from the "ERD Tutorial" by Lucid Software. It walks through a scenario where a composite key is the logical solution.
Pay close attention to the example of the shipment entity from the start of the composite key section. The presenter explains why single attributes like product number or order number are not unique in this context, but combining them creates a unique identifier.
As the video demonstrates, an order can have multiple products, and a product can be in multiple orders. In the shipment table, Order Number can be repeated (if one order is split into multiple shipments) and Product ID can be repeated (if the same product is part of different orders). Therefore, the only way to uniquely identify a specific row representing one product within one specific order is to use the composite key {Order Number, Product ID}.
Scenario 2: Resolving Many-to-Many Relationships
This is one of the most important and common reasons for using a composite key. In a relational database, you cannot directly implement a many-to-many (M:N) relationship.
Consider a university database:
- A
STUDENTcan enroll in manyCOURSEs. - A
COURSEhas manySTUDENTs enrolled.
This is a classic M:N relationship. To model this, we create a third table that sits between STUDENT and COURSE. This intermediate table is often called a junction table, bridge table, or composite entity.
This junction table's primary purpose is to store the relationships. For our example, let's call it ENROLLMENT. Each row in ENROLLMENT will represent one student enrolling in one course.
How do we uniquely identify each row in ENROLLMENT? We use a composite primary key made up of the primary keys from the two tables it connects.
- The
STUDENTtable's primary key (e.g.,StudentID). - The
COURSEtable's primary key (e.g.,CourseID).
So, the primary key for the ENROLLMENT table would be the composite key {StudentID, CourseID}. This guarantees that the same student cannot be enrolled in the same course more than once.
Many to Many Relationships: A Guide to Database Design
This article from DataCamp explains the structure of junction tables clearly.
Read the subsection titled Anatomy and components. It explicitly states that the two foreign keys in a junction table together form a composite primary key and provides an enrollments table as an example.
Let's look at a complete diagram. The Order_Items table in the ERD below is a perfect example of a junction table.

Here, Order_Items resolves the M:N relationship between Orders and Products. An order can contain many products, and a product can appear in many orders. The composite key {order_id, product_id} ensures that each line item within an order is unique.
3. Practice Exercise
Let's apply this knowledge. Imagine you are designing a database for a local library. You have two main entities: BOOK and MEMBER.
- The
BOOKtable has a primary keyBookID. - The
MEMBERtable has a primary keyMemberID.
A member can borrow multiple books over time, and a single book can be borrowed by multiple members (at different times).
- What kind of relationship exists between
BOOKandMEMBER? - To model this, you'll need to create an intermediate table. Let's call it
LOAN. What attributes must thisLOANtable contain to link the two entities? - What would you choose as the primary key for the
LOANtable to uniquely identify a specific loan event? Would a single attribute work? Why or why not?
Solution (Click to expand)
- This is a many-to-many (M:N) relationship.
- The
LOANtable must contain the primary keys of the tables it connects. So, it needsBookIDandMemberIDas foreign keys. You would also want to include other relevant information about the loan itself, such asDateBorrowedandDateReturned. - A single attribute would not work.
BookIDwill repeat every time that book is borrowed.MemberIDwill repeat every time that member borrows a book. To uniquely identify a specific loan transaction, you need to know which member borrowed which book on a specific date. Therefore, a good primary key would be a composite key:{BookID, MemberID, DateBorrowed}. This combination ensures that each loan event is unique, even if the same member borrows the same book on a different date. A simpler composite key like{BookID, MemberID}would only work if a member could only borrow a specific book once in their lifetime, which is unlikely.
Conclusion
In this lesson, we moved beyond single-attribute keys to understand composite primary keys. These are essential tools for maintaining data integrity in more complex situations.
Here are the key takeaways:
- A composite primary key is a key formed by combining two or more attributes.
- Like all keys, it must be minimal—no attribute can be removed without losing uniqueness.
- Composite keys are necessary in two main scenarios:
- When no single attribute within an entity can uniquely identify its instances.
- As the primary key for a junction table (or composite entity) used to resolve a many-to-many relationship.
We have now covered both simple and composite primary keys. In our next lesson, we will focus on foreign keys, which are the mechanism used to create the links between tables—something we've already seen in the context of junction tables.