Excellent question! This is a fundamental and powerful concept in database design.
What you are describing is called a Composite Primary Key.
This specific scenario, where the composite key is made up of two foreign keys, is the classic implementation of a Junction Table (also known as a Linking Table, Bridge Table, or Associative Entity).
In-Depth Explanation
1. Composite Primary Key
A Composite Primary Key (or Compound Key) is a primary key that consists of two or more columns. The combination of values in these columns must be unique for each record in the table, even though the individual values might be repeated.
2. The Junction Table (The "Why")
A junction table is used to resolve a Many-to-Many (M:N) relationship between two other tables.
Let's use a classic example: Students and Courses.
- A
Student can enroll in many Courses.
- A
Course can have many Students.
You cannot put the CourseID in the Students table, because a student can have many courses. You also can't put the StudentID in the Courses table, because a course has many students.
The solution is to create a third table in the middle—the junction table.
Example: Students, Courses, and Enrollments
Let's imagine you have these two tables:
tblStudents
| StudentID (PK) | StudentName |
|---|
| 101 | Alice |
| 102 | Bob |
tblCourses
| CourseID (PK) | CourseName |
|---|
| CS101 | Intro to CS |
| MTH203 | Calculus II |
To link them, you create the junction table, tblEnrollments:
tblEnrollments
| StudentID (FK) | CourseID (FK) | Grade |
|---|
| 101 | CS101 | A |
| 101 | MTH203 | B |
| 102 | CS101 | B |
In this tblEnrollments table:
StudentID is a Foreign Key that links to tblStudents.
CourseID is a Foreign Key that links to tblCourses.
- The combination of (
StudentID, CourseID) is the Composite Primary Key.
Why this works:
- A student (like Alice, ID 101) can appear multiple times.
- A course (like CS101) can appear multiple times.
- However, the combination of a specific student and a specific course can only appear once. Alice cannot enroll in CS101 twice in the same semester. The pair
(101, CS101) is unique and serves as the primary key for that enrollment record.
How to Create it in Access
- Open the table in Design View.
- Hold down the Ctrl key and click on the row selectors for both columns (e.g.,
StudentID and CourseID).
- With both rows selected, click the Primary Key button (the key icon) in the "Table Design" tab on the Ribbon.
- You will see a small key icon appear next to both column names, indicating they form a composite primary key.
In summary, the technical term for the key itself is a Composite Primary Key, and the table structure it's used in is called a Junction Table, which is essential for modeling many-to-many relationships.