Access 2007

Building Table Relationships

Introduction

Access 2007 Relationships By now, you've set up the tables you need for your database and have created fields for the different tables. Relationships provide Access 2007 with the means to bring this information together for you when you need it.

This lesson explains how to establish relationships between the tables in an Access 2007 database. You will learn how to read and manipulate the relationship map . You will also learn about primary and foreign keys , relationship types , and referential integrity .

Building table relationships

To establish a relationship between tables:

  • Click the Relationships command in the Show/Hide group on the Database Tools tab in the Ribbon .

    NOTE: Tables must be closed in order to establish relationships.

    Relationships Command Relationships Command
  • When the Show Table dialog box appears:
    • Select each table name, then click Add for the tables you want to relate.
    • When you're done, close the Show Table dialog box.
    Show Table Dialog Box Show Table Dialog Box
  • You should now see a relationship map that contains all of the tables that were selected.


    Relationship Map Relationship Map

To move a table that appears in the relationship map:

  • Place your mouse over the table you want to move.
  • Hold down the left mouse button, then drag the table to a new location.
  • Release the mouse button to drop the table in its new place.

Understanding the relationship map

The relationship map lists all of the tables that were selected to relate, as well as all of the fields that were previously set up for that table. Notice that the first field has a key icon next to it. This is the primary key for the table.

Primary Keys Primary Keys

Primary and foreign keys

A primary key is the first field in each table of the database. You may recall that this field auto-numbers by default, so every record in the table has its own unique number to identify it. Access uses this number to quickly pull information together when you run queries or reports, which are covered later.

In the example above, the primary key for the Customers table is Customer ID , the primary key for the Orders table is Order ID , and the primary key for the Books table is Book ID .

A foreign key is a field that is the primary field in its own table but that shows up in another table. If you look closely at the Orders table, the fields Customer ID and Book ID appear there, as well as in their own respective tables. These fields are the primary key in their own tables, but in the Orders table, they are considered foreign keys .

Foreign Keys Foreign Keys

Relating tables

There are a few ways to establish relationships between tables:

  • Using the Edit Relationships command located on the Design tab of the Ribbon
  • Using the drag-and-drop method

Both methods give you the same end result, but the drag-and-drop method is much easier and saves you several steps.

To relate tables with the drag-and-drop method:

  • Select a field name from one table by holding down the left mouse button.


    Relationship Map Relationship Map
  • Drag the field name from one table to the other table in the desired relationship.
  • Drop the first field name onto the field name you want to relate by releasing the left mouse button.
    In the example above, we selected the Book ID field from the Books table and dragged and dropped it on the Book ID field in the Orders table.

  • The Edit Relationships dialog box appears.


    Edit Relationships Dialog Box Edit Relationships Dialog Box
  • Select the Enforce Referential Integrity option. This option is explained in detail below.
  • Click Create .

Understanding types of relationships

Access 2007 allows for several different types of relationships. These include:

  • One-to-One
  • One-to-Many
  • Many-to-Many

The relationship type you will come across most frequently—and the one created in our bookstore scenario—is the One-to-Many relationship.

One-to-Many

The One-to-Many relationship means data for that field will show up a single time in one table but many times in the related table.

For example, let's look at one of the book titles in our bookstore. The Book ID for the book should appear only once in the Books table because this table lists every title stocked in the store. But it will probably appear many times in the Orders table because we hope it gets ordered by many people many times.

The symbols for the One-to-Many relationship look like this:

One to Many Relationships One to Many Relationships

Enforcing referential integrity

In the Edit Relationships dialog box, an option to Enforce Referential Integrity appears.

You should click Enforce Referential Integrity to make sure you never have an order for a book that doesn't appear in the Books table. Selecting this option tells Access to check for these things when someone is working with your data records.

Editing existing relationships

Access 2007 allows you to edit relationships that already exist. This can be done using the Edit Relationships command on the Ribbon . However, a much simpler way is to simply double-click the link that appears in the relationship map. Either method brings up the Edit Relationships dialog box, where you can change your settings.

Challenge!

If you haven't already done so, save the sample Ready2Read database to your computer.

  • Open the database, and establish a relationship between the Books table and the Orders table using the drag-and-drop method.
  • Establish a relationship between the Customers table and the Orders table using the Edit Relationships command on the Ribbon .
  • Edit an established relationship by double-clicking the link.
  • Explore the options and settings in the Edit Relationships dialog box.
  • Move the tables around in the relationship map.