When you create a relationship between two tables, it is usually a good idea to enforce . So what does this technical-sounding phrase mean? Referential integrity keeps records in related fields valid and accurate. Referential integrity ensures that you don't accidentally change or delete related data in one table but not in the other. For example, say you were using two related Social Security fields to link two tables. Referential integrity would not allow you to change the Social Security number in one record without changing the Social Security number in the other related records.
Referential Integrity Prerequisites
Access is very picky about when you can set . You can only use referential integrity when all of the following conditions are met:
-
One of the linked fields is a primary key
-
The related fields are the same data type and size. (If you are using an AutoNumber field, you can relate it to a Number field with a Long Integer Field size.)
-
Both tables are in the same Access database.
-
You can't have a record in a related table unless a matching record already exists in the primary table. Orphan data in a related table is the most common problem people encounter when attempting to establish referential integrity.
Referential Integrity Rules
Once you have established referential integrity, the following rules are set:
-
You can't add a record to a related table unless a matching record already exists in the primary table.
-
You can't change the value of a primary key in the primary table if matching records exist in the related table.
-
You can't delete a record from a primary table if matching records exist in a related table.
How to Enforce Referential Integrity
In this section you will learn how to enforce referential integrity. You should still have the open from the previous lesson in order to complete this lesson.
First, let's establish a relationship between the tblCustomers
table and the tblCustomerTours
table...
- Go to the
Database Tools
tab. and click onRelationships
button to open the Relationships window. - Drag and drop the related tables into the Relationships window.
- Drag the
CustomerID
field from thetblCustomers
table to theCustomerID
field in thetblCustomerTours
table. - The Edit Relationships dialog box appears, as shown in figure. We want to enforce referential integrity so that you can't enter a
CustomerID
in thetblCustomerTours
table unless thatCustomerID
number already exists in thetblCustomers
table. - Check the
Enforce Referential Integrity
box.
This will enforce referential integrity between the tables. If you get an error message, it's because your tables and fields don't meet all the required conditions listed on the previous page. -
There are two other very important boxes in the Edit Relationships dialog box:
-
Cascade Update Related Fields
: When you change data in the main field of one table, Access will automatically update the matching data in the related table. Cascade Delete Related Records
: When you delete a record in the main table, Access will automatically delete any matching records in the related table.
-
- Check both the
Cascade Update Related Fields
box and theCascade Delete Related Records
box. Now you can save the changes to the relationship. -
Click
Create
to create the relationship between thetblCustomers
andtblCustomerTours
tables.
Access creates the relationship between the two tables and enforces between them. Notice that the join line between thetblCustomers
table and thetblCustomerTours
table looks a little different. This relationship indicates that referential integrity is being enforced between the two tables and that the tables have a one-to-many relationship, as shown in figure (more about that later). -
Click the
Close
button to close the Relationships window and clickYes
to save the changes you made.
In the next section you will get to test the results of your new referential integrity settings.
To Enforce Referential Integrity:
- Go to the
Database Tools
tab and click theRelationships
button.
(For Access version prior to 2007, click theRelationships
button on the toolbar). - Double-click the join line for the relationship you want to work with.
-
Check the
Enforce Referential Integrity
box. -
If you want changes to the primary field of the primary table copied to the related field in the related table, check the
Cascade Update Related Fields
box. -
If you want access to automatically delete orphan records in the related table, check the
Cascade Delete Related Records
box. - Click OK.