Relational Design and ER Diagrams
Connect tables using foreign keys, understand one-to-one, one-to-many and many-to-many relationships, and learn how to draw and read entity-relationship diagrams.
Splitting data into separate tables only works if those tables can be connected. A foreign key is the bridge between tables - it is how a student record knows which course they are on, and how a course record knows which teacher runs it.
A foreign key is a field in one table whose value matches the primary key of another table. It creates a reference - a way for the database to follow a connection from one table to another.
Students
| StudentID PK | Name | CourseID FK |
|---|---|---|
| 1001 | Alice Smith | 201 |
| 1002 | Bob Brown | 201 |
| 1003 | Clara Davis | 202 |
Courses
| CourseID PK | CourseName | TeacherName |
|---|---|---|
| 201 | GCSE Maths | Mr Jones |
| 202 | GCSE CS | Ms Patel |
Students.CourseID Courses.CourseID - the foreign key points to the primary key of the other tableAlice and Bob both have CourseID = 201. Following that ID into the Courses table tells us both are on GCSE Maths with Mr Jones. The course data is stored once only.
A common exam question asks you to identify the primary key and foreign key in a given table. The foreign key is always in the "many" side of the relationship - in this example, many students can have the same CourseID, but each CourseID exists only once in the Courses table.
If asked to implement a many-to-many relationship in a table structure, create a junction table. For Students and Courses, create a StudentCourse table with two fields: StudentID (FK) and CourseID (FK). Each row in this table records one enrolment - one student on one course.
An ER diagram shows all the entities in a database, their attributes, and how they connect. You need to be able to draw them from a description and interpret them from a diagram.
ER diagram conventions (GCSE level)
The diagram above shows a many-to-many relationship between Student and Course - indicated by M on both sides of the relationship diamond. This tells a database designer they need a junction table.
When asked to draw an ER diagram, always: label the entities (rectangles), name the relationship (diamond), and add multiplicity labels (1 or M). If asked to describe a relationship from a diagram, state both sides: "One teacher teaches many students, but each student (in that class) has one teacher."
Test yourself
1. A library system has a Books table and a Members table. A member can borrow many books, and a book can be borrowed by many members over time. What type of relationship is this?
2. In a Students table, the field CourseID matches the primary key of the Courses table. What type of key is CourseID in the Students table?
3. On an ER diagram, what shape is used to represent an entity (table)?
A school database needs to store which students attend which after-school clubs, and on which days. A student can attend multiple clubs, and a club can have multiple students. Describe the table structure you would use, including all keys.
Practice what you have learned
Three levels of worksheet for this lesson. Download, print and complete offline.