SQL/MX 2.x Reference Manual (H06.04+)

SQL/MX Statements
HP NonStop SQL/MX Reference Manual540440-003
2-101
Examples of CREATE TRIGGER
Keep this limit in mind when you create tables. If you plan to create triggers on a table,
its primary key length cannot exceed 239 bytes. A table which will not have triggers
can have a primary key of 255 bytes.
Rowsets
SQL/MX rowsets are allowed in UPDATE and DELETE statements that are trigger
events.
UPDATE and DELETE statements that use rowset arrays perform multiple executions
of UPDATE or DELETE statements. UPDATE and DELETE statement triggers behave
as a sequence of statement triggers that are triggered once for each value in the array
of values in the rowset.
This behavior is different from a row trigger because each value in the rowset might
match multiple rows in the subject table. Therefore, multiple rows might be affected
(updated or deleted) before the action of the trigger is executed.
Contrast this behavior with row triggers where the trigger action is executed once for
each affected row.
For INSERT statement that use rowsets, an INSERT statement trigger is triggered
once for the entire rowset.
Examples of CREATE TRIGGER
Before and After Triggers
Suppose that you have a database to record patients’ vital signs and drugs prescribed
for them. The database consists of these tables:
vital_signs, which records vital signs at each visit
prescription, which records prescriptions written for each patient
generic_drugs, which lists generic drug equivalents for brand-name drugs
The prescription table is created like this:
CREATE TABLE prescription
( id INTEGER NOT NULL
NOT DROPPABLE,
pat_id INTEGER NOT NULL,
issuing_phys_id INTEGER NOT NULL,
date_prescribed DATE DEFAULT NULL,
drug VARCHAR(80) DEFAULT NULL,
record_id INTEGER NOT NULL,
dosage VARCHAR(30) NOT NULL,
frequency VARCHAR(30) DEFAULT NULL,
refills_remaining INTEGER DEFAULT NULL,
instructions VARCHAR(255) DEFAULT NULL,
primary key (id))
STORE BY PRIMARY KEY