RDB/VMS Relational Database Operator DEFINE_TRIGGER — VMS RDB_3.1A
Creates a trigger for the specified relation. A trigger is a mechanism which associates a set of rules with an update operation. Example: RDO> DEFINE TRIGGER EMPLOYEE_ID_CASCADE_DELETE <--- trigger name cont> BEFORE ERASE <--- action time cont> FOR E IN EMPLOYEES <--- subject relation cont> EXECUTE cont> FOR D IN DEGREES WITH <--- triggered action rse cont> D.EMPLOYEE_ID = E.EMPLOYEE_ID cont> ERASE D <--- triggered action cont> END_FOR; cont> FOR EACH RECORD. <--- frequency clause
Additional information available:
More
Each trigger is associated with a single subject relation, will be evaluated at a specific time for a particular type of update on that relation, and specifies a series of 'triggered' actions. Each triggered action consists of an optional condition and one or more statements to be evaluated either once only or for each record of the relation being updated. You can use triggers with relation-specific constraints to preserve database integrity. You can define a trigger only after you have invoked the database. See the DEFINE_RELATION statement. Defining a trigger requires READ and DEFINE access to the subject relation. If any triggered statement specifies some form of update operation, then READ, CONTROL and appropriate update access (ERASE, MODIFY, or WRITE) to the relations specified by the triggered action statements is also required. You must execute this statement in a read/write transaction. If there is no active transaction and you issue this statement, Rdb/VMS starts a read/write transaction implicitly. Other users are allowed to be attached to the database when you issue the DEFINE TRIGGER statement.
Format
DEFINE TRIGGER ─> name ┬──────────────>─────────────┬┬─>
B
E
F
O
R
E ┬┐ └>
D
E
S
C
R
I
P
T
I
O
N typebox (I)typebox (S) typebox (/)typebox (*) text */ ┘└─>
A
F
T
E
R ─┘│ ┌─────────────────────────────<──────────────────────────────────┘ └┬>
S
T
O
R
E ───────────────────>──────────────────────────────────┬┐ ├>
E
R
A
S
E ───────────────────>──────────────────────────────────┤│ └>
M
O
D
I
F
Y ┬──────────>──────────┬┬──────────────>────────────┬─┘│ └>
O
F ─┬> field-name ┬┘└> context-variable-clause ─┘ │ └───── , <────┘ │ ┌──────────────────────────────────<─────────────────────────────┘ └>
F
O
R context-var IN relation-name ┬> triggered-action-clause ┬> . └──────────────<───────────┘
Additional information available:
nametextfield-namecontext-variable-clausecontext-varrelation-name
triggered-action-clause
name
The name of the trigger that you can refer to in other statements. When choosing a name, follow these rules: Use a name that is unique among all trigger names.
text
A text string that adds a comment to the trigger definition.
field-name
The name of a field within the specified relation to be checked for modification.
context-variable-clause
context-variable-clause = ──┬────────────────────────────────┬─> ├─>
O
L
D typebox (C)typebox (O)typebox (N)typebox (T)typebox (E)Xtypebox (T) old-context-var ─┤ └─>
N
E
W typebox (C)typebox (O)typebox (N)typebox (T)typebox (E)Xtypebox (T) new-context-var ─┘ An optional clause that can contain aliases for old and new states of the context variable of the triggering statement.
Additional information available:
old-context-varnew-context-var
old-context-var
A temporary name used to refer to the record values as they existed before a modify operation occurred. You use an old-context-var only when the trigger occurs after a modify operation, and must be a different name than the one given for context_var.
new-context-var
A temporary name used to reference the new record values about to be applied by modify operation. You use a new-context-var only when the trigger occurs before a modify operation, and must be a different name than the one given for context-var.
context-var
A temporary name defining the relation on which the trigger is defined. The context variable name is an alias for the record stream context of the triggering statement which has caused this trigger to be executed.
relation-name
The name of the relation for which this trigger is defined (subject relation).
triggered-action-clause
triggered-action-clause = ─┬───────────────────────────┬─┐ └─>
W
I
T
H conditional-expr ──┘ │ ┌─────────────────────────────┘ └─>
E
X
E
C
U
T
E ─┬> triggered-statement ─┬──┬───────────────────┬─> └─────────── typebox (;) ────<────┘ └─>
F
O
R
E
A
C
H
R
E
C
O
R
D ┘ triggered-statement = ─┬─> store-statement ───────────────────┬─> ├─>
F
O
R rse store-statement ENDFOR ──┤ ├─>
F
O
R rse erase-statement ENDFOR ──┤ ├─>
F
O
R rse modify-statement ENDFOR ──┤ └─>
E
R
R
O
R ─────────────────────────────┘ A triggered action clause consists of a conditional expression (optional), one or more triggered statements, and a frequency clause (FOR EACH RECORD). The EXECUTE keyword precedes the triggered action clause.
Additional information available:
rsestore-statementerase-statementmodify-statement
rse
A record selection expression that defines which records of which relations will be affected by the triggered update action. This rse cannot refer to any host variables.
store-statement
A STORE statement to be initiated by the trigger.
erase-statement
An ERASE statement to be initiated by the trigger.
modify-statement
A MODIFY statement to be initiated by the trigger.
WITH
A conditional expression that describes the optional condition that must be satisfied before the associated triggered statements are executed. This expression cannot refer to any host variables. For more information on Rdb/VMS conditional expressions, ask for HELP on the top level topic Cond_expr.
FOR_EACH_RECORD
A frequency clause, FOR EACH RECORD (no underscores), determines whether an action is evaluated once per triggering statement, or for each record of the subject relation updated by the triggering statement. If the FOR EACH RECORD clause is not specified, the triggered action is evaluated only once, and record values are not available to the triggered action.
Example
This 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.