You can use the CREATE TRIGGER statement to create or replace a database trigger.
When specified conditions are met, the database automatically runs the trigger.
Applicability
This topic applies only to OceanBase Database Enterprise Edition. OceanBase Database Community Edition provides only the MySQL mode.
Prerequisites
To create a trigger in your own schema or in a table of your own schema, you must have the CREATE TRIGGER system privilege.
To create a trigger in the schema (schema.SCHEMA) of other users, you must have the CREATE ANY TRIGGER system privilege.
If a trigger is used to execute an SQL statement or call a stored procedure or function, the owner of the trigger must have the privileges required to perform these operations. These privileges must be granted to the owner directly rather than through roles.
Syntax
The syntax of CREATE TRIGGER is as follows:
CREATE [OR REPLACE] TRIGGER
plsql_trigger_source
BEGIN
triggered_action;
END;
The parameters are described as follows:
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 | Keyword or syntax node | Description |
|---|---|---|
| create_trigger | OR REPLACE | Re-creates this trigger if it already exists and recompiles it. Before the trigger is redefined, users granted the access privilege can still access this trigger without the need to obtain the access privilege again. |
| plsql_trigger_source | trigger_name | The name of the trigger to be created. |
| plsql_trigger_source | schema | The name of the schema where the trigger is located. The default value is your 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. The INSTEAD OF trigger can read the :OLD and :NEW values, but cannot modify these values. |
| plsql_trigger_source | trigger_ordering_clause | The firing sequence of triggers with the same point in time. The specified triggers must exist and be correctly compiled. However, you do not need to enable the specified triggers. |
| simple_dml_trigger | BEFORE | Enables the database to fire the trigger before a trigger event occurs. A row-level trigger will fire before the corresponding row is modified. In a statement-level BEFORE trigger, the trigger body cannot read the :NEW or :OLD value. However, in a row-level BEFORE trigger, the trigger body can read and write the :OLD and :NEW values. |
| simple_dml_trigger | AFTER | Enables the database to fire the trigger after a trigger event occurs. A row-level trigger will fire each time after a row is modified. In a statement-level AFTER trigger, the trigger body cannot read the :NEW or :OLD value. However, in a row-level AFTER trigger, the trigger body can read and write the :OLD and :NEW values. |
| simple_dml_trigger | FOR EACH ROW | Creates the trigger as a row-level trigger. When the optional trigger constraint defined in the WHEN condition is met, the database fires the row-level trigger. If this clause is ignored, the trigger is a statement-level trigger. If the optional trigger constraint is met, the database fires the trigger as a statement-level trigger only when a trigger statement is executed. |
| simple_dml_trigger | [ ENABLE | DISABLE ] | Creates a trigger that is in the enabled or disabled state. By default, a trigger in the enabled state is created. Creating a trigger that is in the disabled state ensures that the trigger can be correctly compiled before it is enabled. |
| simple_dml_trigger | WHEN (condition) | An SQL condition. The database evaluates each row affected by the trigger statement. If the condition value of an affected row is TRUE, trigger_body runs in this row. Otherwise, trigger_body does not run in this row. The trigger statement will run regardless of the condition value. In the condition statement, do not place a semicolon (:) prior to NEW, OLD, or PARENT. If WHEN (condition) is specified, FOR EACH ROW must also be specified. condition must not contain subqueries or PL expressions, such as calls to user-defined functions. |
| simple_dml_trigger | trigger_body | The PL block or CALL subprogram used by the database to fire the trigger. The CALL subprogram is encapsulated in PL. If trigger_body is a PL block that contains errors, the CREATE [OR REPLACE] statement will fail to be executed. |
| simple_dml_trigger | dml_event_clause | The condition for triggering a DML event in the trigger. DML events include INSERT, UPDATE, and DELETE operations. |
| instead_of_dml_trigger | view | The name of the view on which the trigger is to be created. |
| instead_of_dml_trigger | FOR EACH ROW | Creates the INSTEAD OF trigger as a row-level trigger. |
| instead_of_dml_trigger | [ ENABLE | DISABLE ] | Specifies whether to enable or disable the trigger. By default, the trigger is enabled. Creating a trigger in the disabled state can ensure that the trigger is enabled after it is correctly compiled. |
| instead_of_dml_trigger | DELETE | Enables the database to fire the trigger created on a view each time the DELETE statement deletes a row from the view. |
| instead_of_dml_trigger | INSERT | Enables the database to fire the trigger created on a view each time the INSERT statement inserts a row to the view. |
| instead_of_dml_trigger | UPDATE | Enables the database to fire the trigger created on a view each time the UPDATE statement modifies the value of a column in the view. |
| instead_of_dml_trigger | schema | The name of the schema where the trigger is located. The default value is your schema. |
| instead_of_dml_trigger | trigger_body | The PL block or CALL subprogram used by the database to fire the trigger. The CALL subprogram is encapsulated in PL. If trigger_body is a PL block that contains errors, the CREATE [OR REPLACE] statement will fail to be executed. |
| trigger_ordering_clause | The firing sequence of triggers with the same point in time. The specified triggers must exist and be correctly compiled. However, you do not need to enable the specified triggers. | |
| trigger_ordering_clause | FOLLOWS | Specifies that the new trigger fires after the specified trigger. |
| trigger_ordering_clause | PRECEDES | Specifies that the new trigger fires before the specified trigger. |
| triggered_action | The action or logic executed by the trigger, including any SQL statement, condition judgment, and stored procedure call to be executed when the trigger fires. The value can contain variable operations, condition judgment, and data writes, to implement the business logic to be executed by the trigger. For more information about condition predicates in triggers, see the Condition predicates section. |
Condition predicates
A condition predicate is used in a trigger to detect the condition that triggers the DML statement, so as to determine the logic to be executed by the trigger.
Note
CURSOR is supported in condition predicates since OceanBase Database V4.2.1 BP8.
UPDATING: determines whether anUPDATEoperation is performed. When theIF UPDATING THENstatement is used in a trigger, the condition isTRUEif anUPDATEoperation is triggered. In this case, the trigger can execute specific logic to execute theUPDATEoperation.UPDATING('column_name'): determines whether anUPDATEoperation is performed on the column specified bycolumn_name. When theIF UPDATING('column_name') THENstatement is used in a trigger, the condition isTRUEif anUPDATEoperation is triggered and the specified column is updated. In this case, the trigger can execute specific logic to update the specified column.INSERTING: determines whether anINSERToperation is performed. When theIF INSERTING THENstatement is used in a trigger, the condition isTRUEif anINSERToperation is triggered. In this case, the trigger can execute specific logic to execute theINSERToperation.DELETING: determines whether aDELETEoperation is performed. When theIF DELETING THENstatement is used in a trigger, the condition isTRUEif aDELETEoperation is triggered. In this case, the trigger can execute specific logic to execute theDELETEoperation.
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, which fires after another trigger named 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
Example 3: Create a trigger to record the update events in another table when certain columns in a table are updated.
Create a table named
tbl1, which is monitored by the trigger.CREATE TABLE tbl1(col1 INT, col2 VARCHAR2(20));Insert a data record into the
tbl1table.INSERT INTO tbl1(col1, col2) VALUES(1,'A1');Create a log table named
tbl2, which is used by the trigger to record specific events.CREATE TABLE tbl2(col1 VARCHAR2(255));Change the end character to
//.DELIMITER //Create a trigger named
update_tbl1_triggerto monitor the update operations on thecol1andcol2columns in thetbl1table and insert corresponding information into thetbl2table based on the update operation performed. The logic of the trigger is as follows:- If the
col1andcol2columns of any row are both updated, the "Updating both col1 and col2!" message is inserted into thetbl2table. - If only
col1is updated, the "Updating only col1!" message is inserted into thetbl2table. - If only
col2is updated, the "Updating only col2!" message is inserted into thetbl2table.
CREATE OR REPLACE TRIGGER update_tbl1_trigger BEFORE UPDATE OF col1, col2 ON tbl1 FOR EACH ROW BEGIN IF UPDATING('col1') AND UPDATING('col2') THEN INSERT INTO tbl2(col1) VALUES ('Updating both col1 and col2!'); END IF; IF UPDATING('col1') AND NOT UPDATING('col2') THEN INSERT INTO tbl2(col1) VALUES ('Updating only col1!'); END IF; IF UPDATING('col2') AND NOT UPDATING('col1') THEN INSERT INTO tbl2(col1) VALUES ('Updating only col2!'); END IF; END; //- If the
Change the end character back to
;.DELIMITER ;Update the
col2value wherecol1 = 1in thetbl1table to'A11'.UPDATE tbl1 SET col2 = 'A11' WHERE col1 = 1;Query data in the
tbl2table.SELECT * FROM tbl2;The return result is as follows:
+---------------------+ | COL1 | +---------------------+ | Updating only col2! | +---------------------+ 1 row in set