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 patients and providers.

📘 Reading Lesson

Lesson Notes

Read through the key concepts before you try the challenge.

Real-World Scenario

In your Access database, the Patients table has patient records identified by PatientID. The Appointments table has appointment records, each with a PatientID field that should refer to a real patient. But right now, nothing prevents a staff member from entering PatientID 9999 in an appointment record when no patient with that ID exists. And nothing connects the two tables so you can ask 'show me all appointments for Maria Rodriguez.' This lesson creates those connections and the rules that enforce them.

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 patient can have many appointments, but each appointment belongs to exactly one patient. The Patients table (PatientID is the primary key) is the 'one' side. The Appointments table (PatientID is the foreign key) is the 'many' side. This is the most common relationship type in a medical office database.
  • Foreign key setup: the PatientID field in the Appointments table must have the same data type as the PatientID primary key in the Patients table. Both should be Number (Long Integer) if PatientID is an AutoNumber in Patients. 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. Drag each table from the Show Table dialog into the diagram, 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 PatientID that does not exist in the Patients table, and (2) prevent deleting a patient record that still has related appointment records.
  • Cascade Update Related Fields: if enabled, changing a patient's PatientID in the Patients table automatically updates all matching PatientID 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 patient record automatically deletes all related appointment records. This is powerful but dangerous in a medical context — enabling cascade delete means one accidental patient deletion removes all their appointment history. Leave this unchecked in a healthcare database and instead manage deletions manually with appropriate 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 Patients table and the Appointments table to a Query Design, Access draws the relationship line between them. You can then select fields from both tables: patient name from Patients and appointment date from Appointments. The query returns one row per appointment, with the correct patient name filled in from the Patients table.
  • Inner joins return only records that have matches in both tables — the default join type. A query joining Patients and Appointments with an inner join returns only patients who have at least one appointment, and only appointments that are linked to a real patient. Patients 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 patients who have not had any appointments (the appointment fields show as blank for those patients). Change the join type by double-clicking the relationship line in Query Design view.

Responsible Use

The Relationships view in Access shows the structure of your entire database — which tables exist, which fields link them, and which integrity rules are enforced. This structural view is sensitive: anyone who can see it understands the complete data model of your patient management system. Do not share screenshots of the Relationships view or the database structure with external parties (vendors, consultants, visitors) without your IT department's approval. Database structure is proprietary operational information.

AI Assist

💡 AI Task: Ask ChatGPT — 'Explain Microsoft Access relationships and referential integrity to someone who has just started learning databases. Use an analogy from a physical medical records room (filing cabinet, patient folders, appointment slips) to explain tables, primary keys, foreign keys, and what happens when referential integrity is enforced. Then explain the difference between cascade delete and NOT enabling cascade delete in a medical office context.' Use this explanation to help a colleague understand the database concepts.

Knowledge Check

Referential integrity is enforced between the Patients and Appointments tables. A staff member tries to enter an appointment for PatientID 9999, but no patient with that ID exists. What happens?

Challenge

Apply what you've learned in this lesson.

Define and test the relationships between your Patients and Appointments tables.

  1. Open Database Tools > Relationships. Add both the Patients and Appointments tables to the diagram. Drag PatientID from the Patients table to PatientID 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.
  2. Test referential integrity: in Datasheet View, try to enter an appointment with a PatientID that does not exist (e.g., 99999). Screenshot the error message Access displays.
  3. Test deletion restriction: try to delete a patient record that has related appointment records. Screenshot the error message.
  4. Create a simple select query using Query Design that joins both tables and returns: PatientID, LastName, FirstName (from Patients) and AppointmentDate, VisitType (from Appointments). Run the query and screenshot the result showing patient names next to their appointments.