Relationships and Referential Integrity
Link tables so Access enforces the connections between them, and understand what referential integrity actually prevents.
By the end of this lesson you can
- Create a relationship between two tables
- Explain one-to-many and many-to-many relationships
- Enable referential integrity and describe what it enforces
- Explain what cascade update and cascade delete do
Video
Watch the lesson video, then complete the reading and challenge.
Lesson notes
Read through the key concepts before you try the challenge.
Building the relationship
Linking Appointments to Customers
Connect the two tables so Access refuses to accept an appointment for a customer who does not exist.
- 1
Open Database Tools > Relationships and add both tables.
The Relationships window is where links are defined for the whole database, not per-query. A join drawn inside one query applies only to that query; a relationship defined here is enforced everywhere, including direct table entry.
- 2
Drag CustomerID from Customers onto CustomerID in Appointments.
Drag from the 'one' side to the 'many' side: from the primary key to the foreign key. Access reads the direction to decide which table is which, and dragging the wrong way produces a relationship that does not mean what you intended.
- 3
Tick Enforce Referential Integrity before clicking Create.
This is the whole point of the exercise, and it is off by default. Without it you get a line on a diagram and no enforcement: Access will happily accept an appointment for CustomerID 4471 when no such customer exists.
- 4
Confirm Access reports the relationship as One-To-Many.
If it says One-To-One, the foreign key has a unique index on it and each customer could have only one appointment. If it says Indeterminate, neither field is a primary key or uniquely indexed; check that you dragged from the primary key. If the two fields are different data types, Access will refuse to enforce integrity until you make them match (an AutoNumber key pairs with a Number field set to Long Integer).
- 5
Test it: try to enter an appointment with a CustomerID that does not exist.
A relationship you have not tested is a relationship you are assuming. Access should refuse the record. If it accepts it, integrity is not actually on.
Result: Appointments can only reference real customers, and a customer with appointments on file cannot be deleted.
Key takeaway: Drag from the one side to the many side, and tick Enforce Referential Integrity: it is off by default, and without it the relationship is decorative.
Relationships turn tables into a database
You finish the appointment database at Harborline Builders.
Your tables are well designed but unconnected. Nothing stops someone entering an appointment for CustomerID 4471 when no such customer exists, and nothing warns anyone deleting a customer who still has twelve appointments on file. Those appointments become orphans pointing at nobody.
Your task: Define relationships so Access enforces the links your design assumes.
A relationship connects a primary key in one table to a foreign key in another. The overwhelmingly common form is one-to-many: one customer has many appointments; one estimator has many appointments. The 'one' side holds the primary key and the 'many' side holds the foreign key.
| Type | Means | Example |
|---|---|---|
| One-to-many | One record relates to many in the other table | One customer, many appointments |
| One-to-one | One record relates to exactly one | A customer and a rarely-used detail record |
| Many-to-many | Records on both sides relate to many | Jobs and crew members: requires a junction table |
Access cannot represent many-to-many directly. You create a third junction table holding a foreign key to each side, which turns one many-to-many into two one-to-many relationships. A JobCrew table linking JobID to CrewMemberID is the standard pattern.
Referential integrity is the rule Access enforces once you switch it on. It refuses to let you create an appointment for a customer who does not exist, and refuses to delete a customer who still has appointments. It converts your design assumption into something the database actually guarantees.
With referential integrity enabled and cascade delete off, what happens when you try to delete a customer who has twelve appointments on file?
Practice simulation
Practice what this lesson taught in a working copy of the app. Tasks appear under the window.
Practice simulation
Make the database enforce its links
The Customers, Estimators and Appointments tables are well designed but not connected, so nothing stops an appointment for a customer who does not exist. You create the relationships with referential integrity, then work with the linked data.
Loading the simulation...
Challenge
Apply what you've learned in this lesson.
Build the relationships your design implies, then test that they hold.
- Create Customers, Estimators, and Appointments tables from your Module 1 design, with appropriate keys.
- Open Database Tools > Relationships and create one-to-many relationships from Customers and Estimators to Appointments. Enable referential integrity on both.
- Try to enter an appointment with a CustomerID that does not exist. Record what happens.
- Try to delete a customer who has appointments. Record what happens, then explain in two sentences why you would not enable cascade delete on this database.
Finished this lesson?
Progress is saved in this browser only, so you can pick up where you left off.