Referential Integrity can't be that easy!

        2 votes: *****     2,809 views      No comments
by Allen Browne, 20 April 2005    (for ALL VERSIONS of Access)

Microsoft Access Tips for xBase developers

Provided by Allen Browne, Updated May 2006.

Referential Integrity can't be that easy!

A Customer table is often be related to an Invoice table, on a CustomerID field. To prevent referential integrity problems, database programmers had to write code for every input screen to prevent situations like:

  • adding an invoice with an invalid CustomerID,
  • deleting a Customer who already has invoices,
  • altering a CustomerID after invoices exist.

In an Access database, all this code is replaced by a single mouse click when you create the relationship:

  1. Open the Relationships window. In Access 95 to 2003, choose Relationships from the Tools menu (Edit menu in Access 1 and 2). In Access 2007, choose Relationships on the Show/Hide group of the Database Tools ribbon.
  2. Add both the Customer and Invoice tables to the Relationships window.
  3. Drag the CustomerID field from the primary table (Customer) onto CustomerID in the related table (Invoice).
  4. Access pops up a dialog box to confirm the relationship. Check the Referential Integrity checkbox.

That's it! You never worry about referential integrity problems between these tables again.

In the same dialog box, are options for Cascading Update, and Cascading Delete. If you elect to check these boxes, referential integrity is maintained by updating or deleting data in the related table. For example, if you alter a Customer's ID, then all related records are updated to reflect the change.

Referential Integrity is one of Access' strong points, but there is one situation you should consider. A record added to the Invoice table with a blank CustomerID does not violate referential integrity. If you want to use a blank CustomerID (e.g. for a "Cash Sale"), make sure you understand about outer joins or records may appear to slip through the cracks.

Alternatively, block the possibility of a blank CustomerID by opening the Invoice table in Design View and setting its Required property of this field to "Yes". That's another Access mouse click that will save you many lines of validation code! (Not available in Access 1.)

Home Index of tips Top

Rate this article:  Your rating: PoorYour rating: Not so goodYour rating: AverageYour rating: GoodYour rating: Excellent

This is a cached tutorial, reproduced with permission.

Have your say - comment on this article.

What did you think of 'Referential Integrity can't be that easy!'?

No comments yet.

Why not be the first to comment on this article?!

Have your say...

E-mail (e-mail address will be kept private)

Comments require approval before being displayed on this page (allow 24 hours).