The following example defines a trigger that performs a cascading
delete triggered by the deletion of an employee record. Such a
trigger can be used to maintain referential integrity among the
EMPLOYEES, JOB_HISTORY, RESUMES, and SALARY_HISTORY relations.
Each associated employee record (from the relations which have
foreign keys referring to the primary key in the EMPLOYEES
relation) is deleted.
RDO> DEFINE TRIGGER EMPLOYEE_ID_CASCADE_DELETE
cont> BEFORE ERASE
cont> FOR E IN EMPLOYEES
cont> EXECUTE
cont> FOR D IN DEGREES WITH
cont> D.EMPLOYEE_ID = E.EMPLOYEE_ID
cont> ERASE D
cont> END_FOR;
cont> FOR JH IN JOB_HISTORY WITH
cont> JH.EMPLOYEE_ID = E.EMPLOYEE_ID
cont> ERASE JH
cont> END_FOR;
cont> FOR R IN RESUMES WITH
cont> R.EMPLOYEE_ID = E.EMPLOYEE_ID
cont> ERASE R
cont> END_FOR;
cont> FOR SH IN SALARY_HISTORY WITH
cont> SH.EMPLOYEE_ID = E.EMPLOYEE_ID
cont> ERASE SH
cont> END_FOR
cont> FOR EACH RECORD.
The following RDO command procedure example defines a MODIFY
trigger with two actions. The first action is defined to account
for the situation where the triggering MODIFY statement has not
actually changed the value for the pertinent field (EMPLOYEE_
ID). This trigger causes a cascading update of the EMPLOYEES
relation's EMPLOYEE_ID value to the JOB_HISTORY table. The WITH
clause stipulates that the cascading update will occur only when
the EMPLOYEE_ID value actually changes. The example also logs
each MODIFY operation to the LOG relation.
!
! Invoke the database:
INVOKE DATABASE FILENAME 'PERSONNEL'
!
! Define the global fields for the LOG relation:
START_TRANSACTION READ_WRITE
!
DEFINE FIELD TYPE
DATATYPE IS TEXT 10.
%RDO-W-NOCDDUPDAT, database invoked by filename, the data dictionary will not be
updated
!
DEFINE FIELD REL_NAME
DATATYPE IS TEXT 31.
!
! Define the LOG relation:
DEFINE RELATION LOG.
TYPE.
REL_NAME.
END LOG RELATION.
!
! Define trigger TRIG1:
DEFINE TRIGGER TRIG1
AFTER MODIFY OF EMPLOYEE_ID OLD CONTEXT OLD_EMP
FOR NEW_EMP IN EMPLOYEES
WITH NEW_EMP.EMPLOYEE_ID <> OLD_EMP.EMPLOYEE_ID EXECUTE
FOR JH IN JOB_HISTORY
WITH JH.EMPLOYEE_ID = OLD_EMP.EMPLOYEE_ID
MODIFY JH USING JH.EMPLOYEE_ID = NEW_EMP.EMPLOYEE_ID
END_MODIFY
END_FOR
FOR EACH RECORD
EXECUTE
STORE L IN LOG USING
L.TYPE = "Modify";
L.REL_NAME = "EMPLOYEES"
END_STORE
FOR EACH RECORD.
!
! Test the trigger by changing the EMPLOYEE_ID of "00164" to "98765":
FOR E IN EMPLOYEES WITH E.EMPLOYEE_ID = "00164"
MODIFY E USING E.EMPLOYEE_ID = "98765"
END_MODIFY
END_FOR
!
! The trigger causes a record to be stored in the LOG relation:
FOR L IN LOG
PRINT L.TYPE,
L.REL_NAME
END_FOR
TYPE REL_NAME
Modify EMPLOYEES
!