1. Relationships Explained
1.1. One-to-Many Relationships
One
of the strengths of Access is the ability to create relationships
between tables. As an example of a relationship, consider the Customers
and Orders tables from the Northwind sample database.
In the Northwind business
case, every order comes from a single customer. Of course, a customer
can place more than one order, which means that the Customers and Orders
tables are in a one-to-many relationship with the Customers table on
the "one" side and the Orders table on the "many" side.
1.2. Many-to-Many Relationships
It is also possible for two tables to be in a many-to-many
relationship, where multiple records from one table relate to multiple
records from another table. As an example, consider the Products and
Orders tables in the Northwind database: A product can be included in
many orders, and an order can include many products. The problem is that
you can’t relate the two tables directly without adding fields to the
Orders table to hold spots for more than one item, as in the following
incorrect example.
This table is poorly
designed because it forces you to enter data about the customer and the
order in each record, instead of just once, and an order can’t be for
more items than you have spots in the table. The proper way to manage
the relationship is to create a transition table,
which bridges the gap between the Products and Orders tables. In the
Northwind database, the Order Details table is a transition table.
The Order Details table is on the
"many" side of two one-to-many relationships—the first with the Products
table, and the second with the Orders table. By bridging the two
tables, it lets you manage the relationship between the Orders and
Products tables without resorting to improper table design.
2. Enforcing Referential Integrity
When
you create a relationship between two tables, you need to make sure
that data in the two tables remains consistent. If you no longer order
from a supplier, you would want all products from that supplier deleted
from the Products table when you delete the supplier’s data from the
Suppliers table. Enforcing referential integrity lets you do that.
2.1. Enforce Referential Integrity
Click the Database Tools tab.
Click Relationships.
Double-click the line representing the relationship you want to edit.
Select the Enforce Referential Integrity check box.
Selecting the
Cascade Update Related Fields check box means Access 2010 will change
the values in foreign key fields when the corresponding primary key
field’s value is changed.
Selecting
the Cascade Delete Related Records check box means that deleting a
record from the primary field in a relationship will cause Access 2010
to delete all related records in the second table.
Click the Close box of the Relationships window.