Share via


Building Referential Integrity

Establishing referential integrity involves building a set of rules to preserve the defined relationships between tables when you enter or delete records.

If you enforce referential integrity, Visual FoxPro prevents you from:

  • Adding records to a related table when there is no associated record in the primary table.
  • Changing values in a primary table that would result in orphan records in a related table.
  • Deleting records from a primary table when there are matching related records.

You can choose to write your own triggers and stored procedure code to enforce referential integrity. However, the Visual FoxPro Referential Integrity (RI) Builder enables you to determine the types of rules you want to enforce, the tables on which you want the rules enforced, and the system events that will cause Visual FoxPro to check referential integrity rules.

The RI Builder handles multiple levels of cascading for cascade deletes and updates and is recommended as a tool for building referential integrity.

To open the RI Builder

  1. Open the Database Designer.
  2. From the Database menu, choose Referential Integrity.

To use the RI Builder

  1. In the Database Designer, create a relationship between two tables, or double-click the relationship line to edit the relationship.
  2. In the Edit Relationship dialog box, choose the Referential Integrity button.
  3. In the Referential Integrity Builder, select the rules you want to enforce for updating, deleting, or inserting records.
  4. Choose OK, and then choose Yes to save your changes, generate RI code, and exit the builder.

When you use the RI Builder to build rules for your database, Visual FoxPro generates code to enforce relational integrity rules, then saves it as triggers that reference stored procedures. You can view this code by opening the stored procedure text editor for your database.

Caution   When you make changes to the design of a database, such as modifying database tables or altering indexes used in a persistent relationship, you should rerun the RI Builder before you use the database. Rerunning the RI Builder revises the stored procedure code and table triggers used to enforce referential integrity so that they reflect the new design. If you don't rerun the RI Builder, you might have unexpected results, because the stored procedures and triggers are not rewritten to reflect your changes.

See Also

Creating Persistent Relationships | Creating Stored Procedures | Viewing and Setting Database Properties | Referential Integrity (RI) Builder | Creating Databases | Creating a Database | Working with a Database | Trigger Usage