Step 6. Complete Referential Integrity
REP007 - File Validation Rules/Triggers
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 File. This rule will prevent an employee from losing its parent department.
1. Open file iiiDepartments in the Editor.
2. Select the tab.
3. Select the key field iiiDeptCode field in the list of rules.
4. Select the button on the ribbon, and expand the menu to select to add the rule to the iiiDeptCode field.
5. In the tab, create the rule as follows:
|
Use the ellipsis button for File Name to use the Find dialog :
Note that the rule is only enforced when deleting a record. Your tab should now look like this:
6. In the section, define the File Lookup Rule to set the field in error if the department exists in the Employee file:
|
Your tab should now look like this:
7. Recompile the file iiiDepartments.
Check the file compile completed successfully.
8. Execute your Department Maintenance form iiiMaintDepartment.
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. Exit the application.
10. Close the file.