Search
ctrl/
Ask AI
Light
Dark
System

Triggers

This section describes the DDL commands pertaining to triggers.

Define a new trigger.

{create | alter} type type-name "{"
  create trigger name
    after
    {insert | update | delete} [, ...]
    for {each | all}
    [ when (condition) ]
    do expr
"}"

The command create trigger nested under create type or alter type defines a new trigger for a given object type.

The trigger name must be distinct from that of any existing trigger on the same type.

The options of this command are identical to the SDL trigger declaration.

Declare a trigger that inserts a Log object for each new User object:

Copy
alter type User {
  create trigger log_insert after insert for each do (
    insert Log {
      action := 'insert',
      target_name := __new__.name
    }
  );
};

Declare a trigger that inserts a Log object conditionally when an update query makes a change to a User object:

Copy
alter type User {
  create trigger log_update after update for each
  when (<json>__old__ {**} != <json>__new__ {**})
  do (
    insert Log {
      action := 'update',
      target_name := __new__.name,
      change := __old__.name ++ '->' ++ __new__.name
    }
  );
}

Remove a trigger.

alter type type-name "{"
  drop trigger name;
"}"

The command drop trigger inside an alter type block removes the definition of an existing trigger on the specified type.

type-name

The name (optionally module-qualified) of the type being triggered on.

name

The name of the trigger.

Remove the log_insert trigger on the User type:

Copy
alter type User {
  drop trigger log_insert;
};