Skip to content

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

In your Access database, the Customers table has customer records identified by CustomerID. The Appointments table has appointment records, each with a CustomerID field that should refer to a real customer. But right now, nothing prevents a staff member from entering CustomerID 9999 in an appointment record when no customer 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 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

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 customer 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 an AI assistant: 'Explain Microsoft Access relationships and referential integrity to someone who has just started learning databases. Use an analogy from a physical job files room (filing cabinet, customer 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 construction office context.' Use this explanation to help a colleague understand the database concepts.

Knowledge Check

Check your understanding

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.

  1. 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.
  2. 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.
  3. Test deletion restriction: try to delete a customer 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: CustomerID, LastName, FirstName (from Customers) and AppointmentDate, VisitType (from Appointments). Run the query and screenshot the result showing customer names next to their appointments.
Deliverable: Submit your Relationships diagram screenshot, two error message screenshots, and query result screenshot to your instructor. These exercises confirm that your database enforces data integrity through relationships: the defining feature that makes Access more reliable than a spreadsheet for multi-table data management.

Finished this lesson?

Progress is saved in this browser only, so you can pick up where you left off.

Black Lotus Academy | Learn a trade. Earn a living.