Examples of trigger actions

This section provides some examples of triggers.

The following trigger action copies a row from the flights table into the flight_history table whenever any row gets inserted into flights and adds the comment "inserted from trig1" in the status column of the flight_history table.

CREATE TRIGGER trig1 
  AFTER UPDATE ON flights 
  REFERENCING OLD AS UPDATEDROW 
  FOR EACH ROW
  INSERT INTO flights_history 
    VALUES (UPDATEDROW.FLIGHT_ID, UPDATEDROW.SEGMENT_NUMBER,
            UPDATEDROW.ORIG_AIRPORT, UPDATEDROW.DEPART_TIME,
            UPDATED ROW.DEST_AIRPORT, UPDATEDROW.ARRIVE_TIME,
            UPDATEDROW.MEAL, UPDATEDROW.FLYING_TIME, UPDATEDROW.MILES,
            UPDATEDROW.AIRCRAFT,'inserted from trig1');

The following trigger action updates the FlightAvailability table after an update of the flights table by setting the flight_id column to the value of the flight_id column in the modified row. The update of the FlightAvailability table happens only if the triggering update actually changed the value of FLIGHTS.FLIGHT_ID.

CREATE TRIGGER FLIGHTSUPDATE
  AFTER UPDATE ON flights
  REFERENCING OLD AS OLD NEW AS NEW
  FOR EACH ROW
  WHEN (OLD.FLIGHT_ID <> NEW.FLIGHT_ID)
  UPDATE FlightAvailability
    SET FLIGHT_ID = NEW.FLIGHT_ID
    WHERE FLIGHT_ID = OLD.FLIGHT_ID;
Related concepts
Trigger action overview
Performing referential actions
Accessing before and after rows
Triggers and exceptions