5.7 TRIGGER

A trigger is a special type of stored procedure that is not called directly, instead being executed when a specified event occurs in the associated table or view. A DML trigger is specific to one and only one relation (table or view) and one phase in the timing of the event (BEFORE or AFTER). It can be specified to execute for one specific event (insert, update, delete) or for some combination of two or three of those events.

Two other forms of trigger exist:

  1. a database trigger can be specified to fire at the start or end of a user session (connection) or a user transaction.

  2. a DDL trigger can be specified to fire at the before or after execution of one or more types of DDL statements.

5.7.1 CREATE TRIGGER

Used forCreating a new trigger

Available inDSQL, ESQL

Syntax

  1. CREATE TRIGGER trigname
  2. { <relation_trigger_legacy>
  3. | <relation_trigger_sql2003>
  4. | <database_trigger>
  5. | <ddl_trigger> }
  6. {<psql_trigger> | <external-module-body>}
  7. <relation_trigger_legacy> ::=
  8. FOR {tablename | viewname}
  9. [ACTIVE | INACTIVE]
  10. {BEFORE | AFTER} <mutation_list>
  11. [POSITION number]
  12. <relation_trigger_sql2003> ::=
  13. [ACTIVE | INACTIVE]
  14. {BEFORE | AFTER} <mutation_list>
  15. ON {tablename | viewname}
  16. [POSITION number]
  17. <database_trigger> ::=
  18. [ACTIVE | INACTIVE] ON <db_event>
  19. [POSITION number]
  20. <ddl_trigger> ::=
  21. [ACTIVE | INACTIVE]
  22. {BEFORE | AFTER} <ddl_event>
  23. [POSITION number]
  24. <mutation_list> ::=
  25. <mutation> [OR <mutation> [OR <mutation>]]
  26. <mutation> ::= INSERT | UPDATE | DELETE
  27. <db_event> ::=
  28. CONNECT | DISCONNECT
  29. | TRANSACTION {START | COMMIT | ROLLBACK}
  30. <ddl_event> ::=
  31. ANY DDL STATEMENT
  32. | <ddl_event_item> [{OR <ddl_event_item>} ...]
  33. <ddl_event_item> ::=
  34. {CREATE | ALTER | DROP} TABLE
  35. | {CREATE | ALTER | DROP} PROCEDURE
  36. | {CREATE | ALTER | DROP} FUNCTION
  37. | {CREATE | ALTER | DROP} TRIGGER
  38. | {CREATE | ALTER | DROP} EXCEPTION
  39. | {CREATE | ALTER | DROP} VIEW
  40. | {CREATE | ALTER | DROP} DOMAIN
  41. | {CREATE | ALTER | DROP} ROLE
  42. | {CREATE | ALTER | DROP} SEQUENCE
  43. | {CREATE | ALTER | DROP} USER
  44. | {CREATE | ALTER | DROP} INDEX
  45. | {CREATE | DROP} COLLATION
  46. | ALTER CHARACTER SET
  47. | {CREATE | ALTER | DROP} PACKAGE
  48. | {CREATE | DROP} PACKAGE BODY
  49. | {CREATE | ALTER | DROP} MAPPING
  50. <psql_trigger> ::=
  51. [SQL SECURITY {INVOKER | DEFINER}]
  52. <psql-module-body>
  53. <psql-module-body> ::=
  54. !! See Syntax of Module Body !!
  55. <external-module-body> ::=
  56. !! See Syntax of Module Body !!

Table 5.7.1.1 CREATE TRIGGER Statement Parameters

ParameterDescription

trigname

Trigger name. The maximum length is 63 characters. It must be unique among all trigger names in the database.

relation_trigger_legacy

Legacy style of trigger declaration for a relation trigger

relation_trigger_sql2003

Relation trigger declaration compliant with the SQL:2003 standard

database_trigger

Database trigger declaration

tablename

Name of the table with which the relation trigger is associated

viewname

Name of the view with which the relation trigger is associated

mutation_list

List of relation (table | view) events

number

Position of the trigger in the firing order. From 0 to 32,767

db_event

Connection or transaction event

ddl_event

List of metadata change events

ddl_event_item

One of the metadata change events

The CREATE TRIGGER statement is used for creating a new trigger. A trigger can be created either for a relation (table | view) event (or a combination of events), for a database event, or for a DDL event.

CREATE TRIGGER, along with its associates ALTER TRIGGER, CREATE OR ALTER TRIGGER and RECREATE TRIGGER, is a compound statement, consisting of a header and a body. The header specifies the name of the trigger, the name of the relation (for a DML trigger), the phase of the trigger, the event(s) it applies to, and the position to determine an order between triggers.

The trigger body consists of optional declarations of local variables and named cursors followed by one or more statements, or blocks of statements, all enclosed in an outer block that begins with the keyword BEGIN and ends with the keyword END. Declarations and embedded statements are terminated with semi-colons (;).

The name of the trigger must be unique among all trigger names.

5.7.1.1 Statement Terminators

Some SQL statement editors — specifically the isql utility that comes with Firebird and possibly some third-party editors — employ an internal convention that requires all statements to be terminated with a semi-colon. This creates a conflict with PSQL syntax when coding in these environments. If you are unacquainted with this problem and its solution, please study the details in the PSQL chapter in the section entitled Switching the Terminator in isql.

5.7.1.2 SQL Security

The SQL SECURITY clause specifies the security context for executing other routines or inserting into other tables.

By default, a trigger applies the SQL Security property defined on its table (or — if the table doesn’t have the SQL Security property set — the database default), but it can be overridden by specifying it explicitly.

Note

If the SQL Security property is changed for the table, triggers that do not have an explicit SQL Security property will not see the effect of the change until the next time the trigger is loaded into the metadata cache.

See also SQL Security in chapter Security.

5.7.1.3 The Trigger Body

The trigger body is either a PSQL body, or an external UDR module body.

See The Module Body in the PSQL chapter for details.

5.7.1.4 DML Triggers (on Tables or Views)

DML — or relation — triggers are executed at the row (record) level, every time the row image changes. A trigger can be either ACTIVE or INACTIVE. Only active triggers are executed. Triggers are created ACTIVE by default.

5.7.1.4.1 Who Can Create a DML Trigger?

DML triggers can be created by:

  • Administrators

  • The owner of the table (or view)

  • Users with the ALTER ANY TABLE or — for a view — ALTER ANY VIEW privilege

5.7.1.4.2 Forms of Declaration

Firebird supports two forms of declaration for relation triggers:

  • The original, legacy syntax

  • The SQL:2003 standard-compliant form (recommended)

The SQL:2003 standard-compliant form is the recommended one.

A relation trigger specifies — among other things — a phase and one or more events.

5.7.1.4.3 Phase

Phase concerns the timing of the trigger with regard to the change-of-state event in the row of data:

  • A BEFORE trigger is fired before the specified database operation (insert, update or delete) is carried out

  • An AFTER trigger is fired after the database operation has been completed

5.7.1.4.4 Row Events

A relation trigger definition specifies at least one of the DML operations INSERT, UPDATE and DELETE, to indicate one or more events on which the trigger should fire. If multiple operations are specified, they must be separated by the keyword OR. No operation may occur more than once.

Within the statement block, the Boolean context variables INSERTING, UPDATING and DELETING can be used to test which operation is currently executing.

5.7.1.4.5 Firing Order of Triggers

The keyword POSITION allows an optional execution order (firing order) to be specified for a series of triggers that have the same phase and event as their target. The default position is 0. If no positions are specified, or if several triggers have a single position number, the triggers will be executed in the alphabetical order of their names.

5.7.1.4.6 Examples of CREATE TRIGGER for Tables and Views
  1. Creating a trigger in the legacy form, firing before the event of inserting a new record into the CUSTOMER table occurs.

    1. CREATE TRIGGER SET_CUST_NO FOR CUSTOMER
    2. ACTIVE BEFORE INSERT POSITION 0
    3. AS
    4. BEGIN
    5. IF (NEW.CUST_NO IS NULL) THEN
    6. NEW.CUST_NO = GEN_ID(CUST_NO_GEN, 1);
    7. END
  2. Creating a trigger firing before the event of inserting a new record into the CUSTOMER table in the SQL:2003 standard-compliant form.

    1. CREATE TRIGGER set_cust_no
    2. ACTIVE BEFORE INSERT ON customer POSITION 0
    3. AS
    4. BEGIN
    5. IF (NEW.cust_no IS NULL) THEN
    6. NEW.cust_no = GEN_ID(cust_no_gen, 1);
    7. END
  3. Creating a trigger that will file after either inserting, updating or deleting a record in the CUSTOMER table.

    1. CREATE TRIGGER TR_CUST_LOG
    2. ACTIVE AFTER INSERT OR UPDATE OR DELETE
    3. ON CUSTOMER POSITION 10
    4. AS
    5. BEGIN
    6. INSERT INTO CHANGE_LOG (LOG_ID,
    7. ID_TABLE,
    8. TABLE_NAME,
    9. MUTATION)
    10. VALUES (NEXT VALUE FOR SEQ_CHANGE_LOG,
    11. OLD.CUST_NO,
    12. 'CUSTOMER',
    13. CASE
    14. WHEN INSERTING THEN 'INSERT'
    15. WHEN UPDATING THEN 'UPDATE'
    16. WHEN DELETING THEN 'DELETE'
    17. END);
    18. END
  4. With DEFINER set for trigger tr_ins, user US needs only the INSERT privilege on tr. If it were set for INVOKER, either the user or the trigger would also need the INSERT privilege on table t.

    1. create table tr (i integer);
    2. create table t (i integer);
    3. set term ^;
    4. create trigger tr_ins for tr after insert SQL SECURITY DEFINER
    5. as
    6. begin
    7. insert into t values (NEW.i);
    8. end^
    9. set term ;^
    10. grant insert on table tr to user us;
    11. commit;
    12. connect 'localhost:/tmp/29.fdb' user us password 'pas';
    13. insert into tr values(2);

    The result would be the same if SQL SECURITY DEFINER were specified for table TR:

    1. create table tr (i integer) SQL SECURITY DEFINER;
    2. create table t (i integer);
    3. set term ^;
    4. create trigger tr_ins for tr after insert
    5. as
    6. begin
    7. insert into t values (NEW.i);
    8. end^
    9. set term ;^
    10. grant insert on table tr to user us;
    11. commit;
    12. connect 'localhost:/tmp/29.fdb' user us password 'pas';
    13. insert into tr values(2);

5.7.1.5 Database Triggers

Triggers can be defined to fire upon database events, which really refers to a mixture of events that act across the scope of a session (connection) and events that act across the scope of an individual transaction:

  • CONNECT

  • DISCONNECT

  • TRANSACTION START

  • TRANSACTION COMMIT

  • TRANSACTION ROLLBACK

Section 5.7.1.6, DDL Triggers are a sub-type of database triggers, covered in a separate section.

5.7.1.5.1 Who Can Create a Database Trigger?

Database triggers can be created by:

5.7.1.5.2 Execution of Database Triggers and Exception Handling

CONNECT and DISCONNECT triggers are executed in a transaction created specifically for this purpose. This transaction uses the default isolation level, i.e. snapshot (concurrency), write and wait. If all goes well, the transaction is committed. Uncaught exceptions cause the transaction to roll back, and

  • for a CONNECT trigger, the connection is then broken and the exception is returned to the client

  • for a DISCONNECT trigger, exceptions are not reported. The connection is broken as intended

TRANSACTION triggers are executed within the transaction whose start, commit or rollback evokes them. The action taken after an uncaught exception depends on the event:

  • In a TRANSACTION START trigger, the exception is reported to the client and the transaction is rolled back

  • In a TRANSACTION COMMIT trigger, the exception is reported, the trigger’s actions so far are undone and the commit is cancelled

  • In a TRANSACTION ROLLBACK trigger, the exception is not reported and the transaction is rolled back as intended.

5.7.1.5.2.1 Traps

Obviously there is no direct way of knowing if a DISCONNECT or TRANSACTION ROLLBACK trigger caused an exception. It also follows that the connection to the database cannot happen if a CONNECT trigger causes an exception and a transaction cannot start if a TRANSACTION START trigger causes one, either. Both phenomena effectively lock you out of your database until you get in there with database triggers suppressed and fix the bad code.

Suppressing Database Triggers

Some Firebird command-line tools have been supplied with switches that an administrator can use to suppress the automatic firing of database triggers. So far, they are:

  1. gbak -nodbtriggers
  2. isql -nodbtriggers
  3. nbackup -T
5.7.1.5.2.2 Two-phase Commit

In a two-phase commit scenario, TRANSACTION COMMIT triggers fire in the prepare phase, not at the commit.

5.7.1.5.2.3 Some Caveats
  1. The use of the IN AUTONOMOUS TRANSACTION DO statement in the database event triggers related to transactions (TRANSACTION START, TRANSACTION ROLLBACK, TRANSACTION COMMIT) may cause the autonomous transaction to enter an infinite loop

  2. The DISCONNECT and TRANSACTION ROLLBACK event triggers will not be executed when clients are disconnected via monitoring tables (DELETE FROM MON$ATTACHMENTS)

Only the database owner and administrators have the authority to create database triggers.

5.7.1.5.3 Examples of CREATE TRIGGER for Database Triggers
  1. Creating a trigger for the event of connecting to the database that logs users logging into the system. The trigger is created as inactive.

    1. CREATE TRIGGER tr_log_connect
    2. INACTIVE ON CONNECT POSITION 0
    3. AS
    4. BEGIN
    5. INSERT INTO LOG_CONNECT (ID,
    6. USERNAME,
    7. ATIME)
    8. VALUES (NEXT VALUE FOR SEQ_LOG_CONNECT,
    9. CURRENT_USER,
    10. CURRENT_TIMESTAMP);
    11. END
  2. Creating a trigger for the event of connecting to the database that does not permit any users, except for SYSDBA, to log in during off hours.

    1. CREATE EXCEPTION E_INCORRECT_WORKTIME 'The working day has not started yet.';
    2. CREATE TRIGGER TR_LIMIT_WORKTIME ACTIVE
    3. ON CONNECT POSITION 1
    4. AS
    5. BEGIN
    6. IF ((CURRENT_USER <> 'SYSDBA') AND
    7. NOT (CURRENT_TIME BETWEEN time '9:00' AND time '17:00')) THEN
    8. EXCEPTION E_INCORRECT_WORKTIME;
    9. END

5.7.1.6 DDL Triggers

DDL triggers allow restrictions to be placed on users who attempt to create, alter or drop a DDL object. Their other purposes is to keep a metadata change log.

DDL triggers fire on specified metadata changes events in a specified phase. BEFORE triggers run before changes to system tables. AFTER triggers run after changes in system tables.

Important

The event type [BEFORE | AFTER] of a DDL trigger cannot be changed.

In some sense, DDL triggers are a sub-type of database triggers.

5.7.1.6.1 Who Can Create a DDL Trigger?

DDL triggers can be created by:

5.7.1.6.2 Suppressing DDL Triggers

A DDL trigger is a type of database trigger. See Suppressing Database Triggers how to suppress database — and DDL — triggers.

5.7.1.6.3 Examples of DDL Triggers
  1. Here is how you might use a DDL trigger to enforce a consistent naming scheme, in this case, stored procedure names should begin with the prefix SP_:

    1. set auto on;
    2. create exception e_invalid_sp_name 'Invalid SP name (should start with SP_)';
    3. set term !;
    4. create trigger trig_ddl_sp before CREATE PROCEDURE
    5. as
    6. begin
    7. if (rdb$get_context('DDL_TRIGGER', 'OBJECT_NAME') not starting 'SP_') then
    8. exception e_invalid_sp_name;
    9. end!

    Test

    1. create procedure sp_test
    2. as
    3. begin
    4. end!
    5. create procedure test
    6. as
    7. begin
    8. end!
    9. -- The last command raises this exception and procedure TEST is not created
    10. -- Statement failed, SQLSTATE = 42000
    11. -- exception 1
    12. -- -E_INVALID_SP_NAME
    13. -- -Invalid SP name (should start with SP_)
    14. -- -At trigger 'TRIG_DDL_SP' line: 4, col: 5
    15. set term ;!
  2. Implement custom DDL security, in this case restricting the running of DDL commands to certain users:

    1. create exception e_access_denied 'Access denied';
    2. set term !;
    3. create trigger trig_ddl before any ddl statement
    4. as
    5. begin
    6. if (current_user <> 'SUPER_USER') then
    7. exception e_access_denied;
    8. end!

    Test

    1. create procedure sp_test
    2. as
    3. begin
    4. end!
    5. -- The last command raises this exception and procedure SP_TEST is not created
    6. -- Statement failed, SQLSTATE = 42000
    7. -- exception 1
    8. -- -E_ACCESS_DENIED
    9. -- -Access denied
    10. -- -At trigger 'TRIG_DDL' line: 4, col: 5
    11. set term ;!

    Note

    Firebird has privileges for executing DDL statements, so writing a DDL trigger for this should be a last resort, if the same effect cannot be achieved using privileges.

  3. Use a trigger to log DDL actions and attempts:

    1. create sequence ddl_seq;
    2. create table ddl_log (
    3. id bigint not null primary key,
    4. moment timestamp not null,
    5. user_name varchar(63) not null,
    6. event_type varchar(25) not null,
    7. object_type varchar(25) not null,
    8. ddl_event varchar(25) not null,
    9. object_name varchar(63) not null,
    10. sql_text blob sub_type text not null,
    11. ok char(1) not null
    12. );
    13. set term !;
    14. create trigger trig_ddl_log_before before any ddl statement
    15. as
    16. declare id type of column ddl_log.id;
    17. begin
    18. -- We do the changes in an AUTONOMOUS TRANSACTION, so if an exception happens
    19. -- and the command didn't run, the log will survive.
    20. in autonomous transaction do
    21. begin
    22. insert into ddl_log (id, moment, user_name, event_type, object_type,
    23. ddl_event, object_name, sql_text, ok)
    24. values (next value for ddl_seq, current_timestamp, current_user,
    25. rdb$get_context('DDL_TRIGGER', 'EVENT_TYPE'),
    26. rdb$get_context('DDL_TRIGGER', 'OBJECT_TYPE'),
    27. rdb$get_context('DDL_TRIGGER', 'DDL_EVENT'),
    28. rdb$get_context('DDL_TRIGGER', 'OBJECT_NAME'),
    29. rdb$get_context('DDL_TRIGGER', 'SQL_TEXT'),
    30. 'N')
    31. returning id into id;
    32. rdb$set_context('USER_SESSION', 'trig_ddl_log_id', id);
    33. end
    34. end!

    The above trigger will fire for this DDL command. It’s a good idea to use -nodbtriggers when working with them!

    1. create trigger trig_ddl_log_after after any ddl statement
    2. as
    3. begin
    4. -- Here we need an AUTONOMOUS TRANSACTION because the original transaction
    5. -- will not see the record inserted on the BEFORE trigger autonomous
    6. -- transaction if user transaction is not READ COMMITTED.
    7. in autonomous transaction do
    8. update ddl_log set ok = 'Y'
    9. where id = rdb$get_context('USER_SESSION', 'trig_ddl_log_id');
    10. end!
    11. commit!
    12. set term ;!
    13. -- Delete the record about trig_ddl_log_after creation.
    14. delete from ddl_log;
    15. commit;

    Test

    1. -- This will be logged one time
    2. -- (as T1 did not exist, RECREATE acts as CREATE) with OK = Y.
    3. recreate table t1 (
    4. n1 integer,
    5. n2 integer
    6. );
    7. -- This will fail as T1 already exists, so OK will be N.
    8. create table t1 (
    9. n1 integer,
    10. n2 integer
    11. );
    12. -- T2 does not exist. There will be no log.
    13. drop table t2;
    14. -- This will be logged twice
    15. -- (as T1 exists, RECREATE acts as DROP and CREATE) with OK = Y.
    16. recreate table t1 (
    17. n integer
    18. );
    19. commit;
    1. select id, ddl_event, object_name, sql_text, ok
    2. from ddl_log order by id;
    3. ID DDL_EVENT OBJECT_NAME SQL_TEXT OK
    4. === ========================= ======================= ================= ======
    5. 2 CREATE TABLE T1 80:3 Y
    6. ====================================================
    7. SQL_TEXT:
    8. recreate table t1 (
    9. n1 integer,
    10. n2 integer
    11. )
    12. ====================================================
    13. 3 CREATE TABLE T1 80:2 N
    14. ====================================================
    15. SQL_TEXT:
    16. create table t1 (
    17. n1 integer,
    18. n2 integer
    19. )
    20. ====================================================
    21. 4 DROP TABLE T1 80:6 Y
    22. ====================================================
    23. SQL_TEXT:
    24. recreate table t1 (
    25. n integer
    26. )
    27. ====================================================
    28. 5 CREATE TABLE T1 80:9 Y
    29. ====================================================
    30. SQL_TEXT:
    31. recreate table t1 (
    32. n integer
    33. )
    34. ====================================================

See alsoSection 5.7.2, ALTER TRIGGER, Section 5.7.3, CREATE OR ALTER TRIGGER, Section 5.7.5, RECREATE TRIGGER, Section 5.7.4, DROP TRIGGER, DDL Triggers in Chapter Procedural SQL (PSQL) Statements

5.7.2 ALTER TRIGGER

Used forModifying and deactivating an existing trigger

Available inDSQL, ESQL

Syntax

  1. ALTER TRIGGER trigname
  2. [ACTIVE | INACTIVE]
  3. [{BEFORE | AFTER} <mutation_list>]
  4. [POSITION number]
  5. {<psql_trigger> | <external-module-body>}
  6. <psql_trigger> ::=
  7. [<sql_security>]
  8. [<psql-module-body>]
  9. <sql_security> ::=
  10. SQL SECURITY {INVOKER | DEFINER}
  11. | DROP SQL SECURITY
  12. !! See syntax of CREATE TRIGGER for further rules !!

The ALTER TRIGGER statement only allows certain changes to the header and body of a trigger.

5.7.2.1 Permitted Changes to Triggers

  • Status (ACTIVE | INACTIVE)

  • Phase (BEFORE | AFTER) (of DML triggers)

  • Events (of DML triggers)

  • Position in the firing order

  • Modifications to code in the trigger body

If an element is not specified, it remains unchanged.

Note

A DML trigger cannot be changed to a database (or DDL) trigger.

It is not possible to change the event(s) or phase of a database (or DDL) trigger.

Reminders

The BEFORE keyword directs that the trigger be executed before the associated event occurs; the AFTER keyword directs that it be executed after the event.

More than one DML event — INSERT, UPDATE, DELETE — can be covered in a single trigger. The events should be separated with the keyword OR. No event should be mentioned more than once.

The keyword POSITION allows an optional execution order (firing order) to be specified for a series of triggers that have the same phase and event as their target. The default position is 0. If no positions are specified, or if several triggers have a single position number, the triggers will be executed in the alphabetical order of their names.

5.7.2.2 Who Can Alter a Trigger?

DML triggers can be altered by:

  • Administrators

  • The owner of the table (or view)

  • Users with the ALTER ANY TABLE or — for a view — ALTER ANY VIEW privilege

Database and DDL triggers can be altered by:

5.7.2.3 Examples using ALTER TRIGGER

  1. Deactivating the set_cust_no trigger (switching it to the inactive status).

    1. ALTER TRIGGER set_cust_no INACTIVE;
  2. Changing the firing order position of the set_cust_no trigger.

    1. ALTER TRIGGER set_cust_no POSITION 14;
  3. Switching the TR_CUST_LOG trigger to the inactive status and modifying the list of events.

    1. ALTER TRIGGER TR_CUST_LOG
    2. INACTIVE AFTER INSERT OR UPDATE;
  4. Switching the tr_log_connect trigger to the active status, changing its position and body.

    1. ALTER TRIGGER tr_log_connect
    2. ACTIVE POSITION 1
    3. AS
    4. BEGIN
    5. INSERT INTO LOG_CONNECT (ID,
    6. USERNAME,
    7. ROLENAME,
    8. ATIME)
    9. VALUES (NEXT VALUE FOR SEQ_LOG_CONNECT,
    10. CURRENT_USER,
    11. CURRENT_ROLE,
    12. CURRENT_TIMESTAMP);
    13. END

See alsoSection 5.7.1, CREATE TRIGGER, Section 5.7.3, CREATE OR ALTER TRIGGER, Section 5.7.5, RECREATE TRIGGER, Section 5.7.4, DROP TRIGGER

5.7.3 CREATE OR ALTER TRIGGER

Used forCreating a new trigger or altering an existing trigger

Available inDSQL

Syntax

  1. CREATE OR ALTER TRIGGER trigname
  2. { <relation_trigger_legacy>
  3. | <relation_trigger_sql2003>
  4. | <database_trigger>
  5. | <ddl_trigger> }
  6. {<psql_trigger> | <external-module-body>}
  7. !! See syntax of CREATE TRIGGER for further rules !!

The CREATE OR ALTER TRIGGER statement creates a new trigger if it does not exist; otherwise it alters and recompiles it with the privileges intact and dependencies unaffected.

5.7.3.1 Example of CREATE OR ALTER TRIGGER

Creating a new trigger if it does not exist or altering it if it does exist

  1. CREATE OR ALTER TRIGGER set_cust_no
  2. ACTIVE BEFORE INSERT ON customer POSITION 0
  3. AS
  4. BEGIN
  5. IF (NEW.cust_no IS NULL) THEN
  6. NEW.cust_no = GEN_ID(cust_no_gen, 1);
  7. END

See alsoSection 5.7.1, CREATE TRIGGER, Section 5.7.2, ALTER TRIGGER, Section 5.7.5, RECREATE TRIGGER

5.7.4 DROP TRIGGER

Used forDropping (deleting) an existing trigger

Available inDSQL, ESQL

Syntax

  1. DROP TRIGGER trigname

Table 5.7.4.1 DROP TRIGGER Statement Parameter

ParameterDescription

trigname

Trigger name

The DROP TRIGGER statement drops (deletes) an existing trigger.

5.7.4.1 Who Can Drop a Trigger?

DML triggers can be dropped by:

  • Administrators

  • The owner of the table (or view)

  • Users with the ALTER ANY TABLE or — for a view — ALTER ANY VIEW privilege

Database and DDL triggers can be dropped by:

5.7.4.2 Example of DROP TRIGGER

Deleting the set_cust_no trigger

  1. DROP TRIGGER set_cust_no;

See alsoSection 5.7.1, CREATE TRIGGER, Section 5.7.5, RECREATE TRIGGER

5.7.5 RECREATE TRIGGER

Used forCreating a new trigger or recreating an existing trigger

Available inDSQL

Syntax

  1. RECREATE TRIGGER trigname
  2. { <relation_trigger_legacy>
  3. | <relation_trigger_sql2003>
  4. | <database_trigger>
  5. | <ddl_trigger> }
  6. {<psql_trigger> | <external-module-body>}
  7. !! See syntax of CREATE TRIGGER for further rules !!

The RECREATE TRIGGER statement creates a new trigger if no trigger with the specified name exists; otherwise the RECREATE TRIGGER statement tries to drop the existing trigger and create a new one. The operation will fail on COMMIT if the trigger is in use.

Warning

Be aware that dependency errors are not detected until the COMMIT phase of this operation.

5.7.5.1 Example of RECREATE TRIGGER

Creating or recreating the set_cust_no trigger.

  1. RECREATE TRIGGER set_cust_no
  2. ACTIVE BEFORE INSERT ON customer POSITION 0
  3. AS
  4. BEGIN
  5. IF (NEW.cust_no IS NULL) THEN
  6. NEW.cust_no = GEN_ID(cust_no_gen, 1);
  7. END

See alsoSection 5.7.1, CREATE TRIGGER, Section 5.7.4, DROP TRIGGER, Section 5.7.3, CREATE OR ALTER TRIGGER