Step 6. Complete Referential Integrity

In this step, you will complete the referential integrity checks. You will add a rule that checks if any employees exist for a specific department before allowing the department to be deleted from the Department Table. This rule will prevent an employee from losing its parent department.

1.  Open table iiiDepartments in the Editor. 

2.  Select the Rules and Triggers tab.

3.  Select the key field iiiDeptCode field in the list of rules.

4.  In the Rules and Triggers group on the Home ribbon, expand the Add menu to select Add Lookup Check to add the rule to the iiiDeptCode field.

5.  In the Details tab, create the rule as follows:

Description Cannot delete - has Employee
Sequence 2
Table Name iiiEmpByDeptView
When Inserting Never apply rule
When Updating Never apply rule
When Deleting Always apply rule (DLT)

 

   Use the ellipsis button for Table Name to use the Find dialog  :

 

     Note that the rule is only enforced when deleting a record. Your Details tab should now look like this:

6.  In the Actions section, define the Table Lookup Rule to set the field in error if the department exists in the Employee table:

If a "key match" is found in target table

Set field in error (ERROR)

If a "key match" is NOT found in target table

Evaluate next rule (NEXT)
Message file

(delete the entry in this field)

Message Number

(delete the entry in this field)

Message text Cannot delete if used in Employee table

 

     Your Details tab should now look like this:

7.  Recompile the table iiiDepartments.

     Check the table compile completed successfully.

8.  Execute your Department Maintenance form iiiMainDepartment

a.  Try to delete department ADM. You will not be able to delete this record.

b.  Add a new department XYZ. Delete this department. The delete will be allowed because no employees have been defined for this department.

9.  Close the form.

10.  Close the table definition in the editor.