Linking Tables with Relationships
Define relationships between tables in Access using primary and foreign keys: connecting your data so that appointments always link to real customers and estimators.
Lesson notes
Read through the key concepts before you try the challenge.
Real-World Scenario
Understanding Relationships in Access
A relationship in Access is a formal link between two tables, defined by a matching field: the primary key in one table (the 'one' side) and a foreign key in another table (the 'many' side):
- One-to-Many: one customer can have many appointments, but each appointment belongs to exactly one customer. The Customers table (CustomerID is the primary key) is the 'one' side. The Appointments table (CustomerID is the foreign key) is the 'many' side. This is the most common relationship type in a construction office database.
- Foreign key setup: the CustomerID field in the Appointments table must have the same data type as the CustomerID primary key in the Customers table. Both should be Number (Long Integer) if CustomerID is an AutoNumber in Customers. The field names do not need to be identical, but they typically are for clarity.
- To open the Relationships view: click Database Tools > Relationships. This shows a visual diagram of all relationships in your database. Add each table to the diagram (double-click it in the list of tables Access shows), then drag the primary key field from the 'one' table to the foreign key field in the 'many' table to create the relationship line.
Referential Integrity
Referential integrity is the rule that enforces valid relationships: it prevents orphaned records and ensures every foreign key value matches a real primary key value in the related table:
- Enabling referential integrity: when you create a relationship line in the Relationships view, Access shows a dialog. Check the 'Enforce Referential Integrity' checkbox. With this checked, Access will: (1) prevent creating an appointment record with a CustomerID that does not exist in the Customers table, and (2) prevent deleting a customer record that still has related appointment records.
- Cascade Update Related Fields: if enabled, changing a customer's CustomerID in the Customers table automatically updates all matching CustomerID values in the Appointments table. For AutoNumber primary keys, IDs never change, so this option is rarely needed, but useful for natural keys that might change.
- Cascade Delete Related Records: if enabled, deleting a customer record automatically deletes all related appointment records. This is powerful but dangerous: with cascade delete on, one accidental customer deletion removes their whole appointment and job history. Leave it unchecked in a customer database and manage deletions by hand, with authorization.
Using Relationships in Queries
Once relationships are defined, you can create queries that pull data from multiple related tables simultaneously: answering questions that span the entire database:
- A multi-table query in Access automatically recognizes the relationship and joins the tables correctly: when you add both the Customers table and the Appointments table to a Query Design, Access draws the relationship line between them. You can then select fields from both tables: customer name from Customers and appointment date from Appointments. The query returns one row per appointment, with the correct customer name filled in from the Customers table.
- Inner joins return only records that have matches in both tables: the default join type. A query joining Customers and Appointments with an inner join returns only customers who have at least one appointment, and only appointments that are linked to a real customer. Customers with no appointments are excluded.
- Left outer joins return all records from the left table even if there is no match in the right table: useful for finding customers who have not had any appointments (the appointment fields show as blank for those customers). Change the join type by double-clicking the relationship line in Query Design view.
Responsible Use
AI Assist
Knowledge Check
Referential integrity is enforced between the Customers and Appointments tables. A staff member tries to enter an appointment for CustomerID 9999, but no customer with that ID exists. What happens?
Practice simulation
Practice what this lesson taught in a working copy of the app. Tasks appear under the window.
Practice simulation
Link customers to their appointments
Right now nothing stops someone from entering an appointment for CustomerID 9999, and nothing connects the tables so you can list Maria Rodriguez's visits. You create the relationships and a query that uses them.
Loading the simulation...
Challenge
Apply what you've learned in this lesson.
Define and test the relationships between your Customers and Appointments tables.
- Open Database Tools > Relationships. Add both the Customers and Appointments tables to the diagram. Drag CustomerID from the Customers table to CustomerID in the Appointments table. In the relationship dialog, check 'Enforce Referential Integrity' (do NOT check cascade delete). Click Create. Screenshot the completed Relationships diagram showing the relationship line with 1 and infinity symbols.
- Test referential integrity: in Datasheet View, try to enter an appointment with a CustomerID that does not exist (e.g., 99999). Screenshot the error message Access displays.
- Test deletion restriction: try to delete a customer record that has related appointment records. Screenshot the error message.
- Create a simple select query using Query Design that joins both tables and returns: CustomerID, LastName, FirstName (from Customers) and AppointmentDate, VisitType (from Appointments). Run the query and screenshot the result showing customer names next to their appointments.
Finished this lesson?
Progress is saved in this browser only, so you can pick up where you left off.