The CREATE TRIGGER statement is used to create or replace a database trigger.
The database automatically runs the trigger when the specified condition is met.
Applicability
This topic applies only to OceanBase Database Enterprise Edition. OceanBase Database Community Edition does not support this feature.
Prerequisites
To create a trigger on your own schema or on a table in a schema, you must have the CREATE TRIGGER system privilege.
To create a trigger in another user's schema (schema.SCHEMA), you must have the CREATE ANY TRIGGER system privilege.
If the trigger executes SQL statements or calls stored procedures or functions, the trigger owner must have the privileges required to perform these actions. These privileges must be granted directly to the user, not through a role.
Syntax
The syntax of create_trigger is as follows:
CREATE [ OR REPLACE ]
TRIGGER plsql_trigger_source
where:
The syntax of
plsql_trigger_sourceis as follows:[schema.] trigger_name { simple_dml_trigger | instead_of_dml_trigger | trigger_ordering_clause }The syntax of
simple_dml_triggeris as follows:{ BEFORE | AFTER } dml_event_clause [ referencing_clause ] [ FOR EACH ROW ] [ ENABLE | DISABLE ] [ WHEN ( condition ) ] trigger_bodyThe syntax of
instead_of_dml_triggeris as follows:INSTEAD OF { DELETE | INSERT | UPDATE } [ OR { DELETE | INSERT | UPDATE } ]... ON [ schema. ] view [ referencing_clause ] [ FOR EACH ROW ] [ ENABLE | DISABLE ] trigger_bodyThe syntax of
trigger_ordering_clauseis as follows:{ FOLLOWS | PRECEDES } [ schmema.] trigger_name [ , [ schmema.] trigger_name ]...The syntax of
dml_event_clauseis as follows:{ DELETE | INSERT | UPDATE [ OF column [, column ]... ] } [ OR { DELETE | INSERT | UPDATE [ OF column [, column]... ] }...] ON [ schema.] { table | view }The syntax of
referencing_clauseis as follows:REFERENCING { OLD [ AS ] old | NEW [ AS ] new | PARENT [ AS ] parent }..The syntax of
trigger_bodyis as follows:{ plsql_block | CALL routine_clause }
Semantics
| Syntax | Keywords or syntax nodes | Description | |
|---|---|---|---|
| create_trigger | OR REPLACE | Recreates the trigger (if it exists) and recompiles it. Users who were granted privileges to the trigger before it was redefined can still access the trigger without needing to regrant privileges. | |
| create_trigger | trigger_name | The name of the trigger to create. | |
| plsql_trigger_source | schema | The name of the schema in which the trigger is located. The default is the current user's schema. | |
| plsql_trigger_source | simple_dml_trigger | Creates a simple DML trigger. | |
| plsql_trigger_source | instead_of_dml_trigger | Creates an INSTEAD OF DML trigger. INSTEAD OF triggers can read :OLD and :NEW values but cannot modify them. |
|
| plsql_trigger_source | trigger_ordering_clause | Specifies the trigger order for triggers with the same timing. The specified triggers must exist and have been successfully compiled, but they do not need to be enabled. | |
| simple_dml_trigger | BEFORE | Causes the database to trigger the corresponding trigger before the trigger event occurs. For row-level triggers, the trigger is fired before each row is modified. In BEFORE statement-level triggers, the trigger body cannot read :NEW or :OLD values. (In BEFORE row-level triggers, the trigger body can read and write :OLD and :NEW fields.) |
|
| simple_dml_trigger | AFTER | Causes the database to trigger the corresponding trigger after the trigger event occurs. For row-level triggers, the trigger is fired after each row is modified. In AFTER statement-level triggers, the trigger body cannot read :NEW or :OLD values. (In AFTER row-level triggers, the trigger body can read and write :OLD and :NEW fields.) |
|
| simple_dml_trigger | FOR EACH ROW | Creates the trigger as a row-level trigger. The database fires the row trigger when the trigger statement affects rows that meet the optional trigger constraints defined in the WHEN clause. If you omit this clause, the trigger is a statement-level trigger. The database fires the statement-level trigger only when the optional trigger constraints are met and the trigger statement is executed. |
|
| simple_dml_trigger | [ ENABLE | DISABLE ] | Enables (default) or disables the trigger. Creating the trigger in a disabled state ensures that it can be correctly compiled before it is enabled. | |
| simple_dml_trigger | WHEN (condition) | Specifies an SQL condition that the database evaluates for each row affected by the trigger statement. If the condition value for the affected row is TRUE, the trigger_body runs for that row; otherwise, the trigger_body does not run for that row. The trigger statement runs regardless of the condition's value. In the condition statement, do not place a colon (:) before the associated names NEW, OLD, or PARENT. If you specify WHEN (condition), you must also specify FOR EACH ROW. condition cannot contain subqueries or PL/SQL expressions (such as calling a user-defined function). |
|
| simple_dml_trigger | trigger_body | The PL/SQL block or CALL subprogram that the database uses to trigger the trigger. A CALL subprogram is a PL/SQL subprogram wrapped in a PL/SQL package. If trigger_body is a PL/SQL block and contains an error, the CREATE [OR REPLACE] statement fails. |
|
| simple_dml_trigger | dml_event_clause | Defines the conditions under which a DML event triggers the trigger. DML events include insert (INSERT), update (UPDATE), and delete (DELETE) operations. | |
| instead_of_dml_trigger | view | The name of the view on which to create the trigger. | |
| instead_of_dml_trigger | FOR EACH ROW | INSTEAD OF triggers are always row-level triggers. |
|
| instead_of_dml_trigger | [ ENABLE | DISABLE ] | Enables or disables the trigger. The default is to enable the trigger. Creating the trigger in a disabled state ensures that it can be correctly compiled before it is enabled. | |
| instead_of_dml_trigger | DELETE | If the trigger is created on a view, the trigger fires whenever a DELETE statement deletes a row from the table defined by the view. |
|
| instead_of_dml_trigger | INSERT | If the trigger is created on a view, the trigger fires whenever an INSERT statement adds a row to the table defined by the view. |
|
| instead_of_dml_trigger | UPDATE | If the trigger is created on a view, the trigger fires whenever an UPDATE statement modifies the value of a column in the table defined by the view. |
|
| instead_of_dml_trigger | schema | The name of the schema in which the trigger is located. The default is the current user's schema. | |
| instead_of_dml_trigger | trigger_body | The PL/SQL block or CALL subprogram that the database uses to trigger the trigger. A CALL subprogram is a PL/SQL subprogram wrapped in a PL/SQL package. If trigger_body is a PL/SQL block and contains an error, the CREATE [OR REPLACE] statement fails. |
|
| trigger_ordering_clause | Not specified | Specifies the trigger order for triggers with the same timing. The specified triggers must exist and have been successfully compiled, but they do not need to be enabled. | |
| trigger_ordering_clause | FOLLOWS | Indicates that the trigger being created must fire after the specified trigger. | |
| trigger_ordering_clause | PRECEDES | Indicates that the trigger being created must fire before the specified trigger. | |
Examples
Example 1: Create a DML trigger named del_new_region.
CREATE OR REPLACE TRIGGER del_new_region
BEFORE DELETE ON regions
FOR EACH ROW
WHEN (old.region_id >3)
BEGIN
INSERT INTO reg_his(region_id , region_name )
VALUES( :old.region_id, :old.region_name );
END;
/
Example 2: Create a trigger named tri2_t that fires after the trigger tri1_t.
CREATE SEQUENCE seq_tri INCREMENT BY 1 START WITH 1 NOMAXVALUE MINVALUE 0 NOCYCLE NOCACHE;
CREATE TABLE t(c1 INT, c2 INT);
CREATE TABLE msg (c1 INT PRIMARY KEY, c2 VARCHAR(100));
CREATE OR REPLACE TRIGGER tri1_t BEFORE INSERT ON t FOR EACH ROW
BEGIN
INSERT INTO msg VALUES (seq_tri.NEXTVAL, 'BEFORE INSERT tri1_t');
END;
/
CREATE OR REPLACE TRIGGER tri2_t BEFORE INSERT ON t FOR EACH ROW FOLLOWS tri1_t
BEGIN
INSERT INTO msg VALUES (seq_tri.nextval, 'BEFORE INSERT tri2_t');
END;
/
INSERT INTO t VALUES (1, 1);
obclient> SELECT * FROM msg;
+----+----------------------+
| C1 | C2 |
+----+----------------------+
| 1 | BEFORE INSERT tri1_t |
| 2 | BEFORE INSERT tri2_t |
+----+----------------------+
2 rows in set
