You can create a DML trigger on a table or a view. A trigger event consists of the DELETE, INSERT, and UPDATE DML statements.
Types of DML row-level triggers
The current version of OceanBase Database supports the following types of DML row-level triggers:
BEFORE INSERT FOR EACH ROWAFTER INSERT FOR EACH ROWBEFORE UPDATE FOR EACH ROWAFTER UPDATE FOR EACH ROWBEFORE DELETE FOR EACH ROWAFTER DELETE FOR EACH ROW
Unlike stored procedures and packages, a trigger has a separate namespace. Therefore, the name of a trigger can be identical to the name of a table or stored procedure.
Syntax
You can use a CREATE TRIGGER statement to create a trigger.
To create a trigger, you must have the following privileges:
Privileges on the table associated with the trigger, such as
SELECT,INSERT,UPDATE, andDELETEprivilegesCREATE TRIGGERprivilegePrivileges on statements to be executed after the trigger is activated
CREATE [OR REPLACE] TRIGGER trigger_name
{BEFORE | AFTER }
{INSERT | DELETE | UPDATE [OF column [, column ...]]}
ON [schema.] table_name
[REFERENCING {OLD [AS] old | NEW [AS] new| PARENT as parent}]
FOR EACH ROW
[WHEN condition]
trigger_body;
Note
trigger_name: the name of the trigger. The name must be unique.
tbl_name: the name of the table for which the trigger is created.
BEFOREorAFTER: specifies whether the trigger is activated before or after the triggering event. For example, whether the trigger is activated before or after each row is inserted into the associated table.
INSERT,UPDATE, orDELETE: the type of operation for which the trigger fires.
FOR EACH ROW: the trigger entity. This statement is executed when the trigger fires. It is executed for each row affected by the trigger event.OceanBase Database also supports
NEW.columnNameandOLD.columnName.
For an
INSERTtrigger,NEW.columnNamespecifies new data that is inserted in aBEFOREscenario or was inserted in anAFTERscenario.
columnNameindicates a column name in the corresponding data table.For an
UPDATEtrigger,OLD.columnNamespecifies existing data that is updated.NEW.columnNamespecifies new data after the update.For a
DELETEtrigger,OLD.columnNamespecifies existing data that is deleted.Values in
OLD.columnNameare read-only, whereas values inNEW.columnNamecan be specified by usingSETstatements.
Parameters
| Parameter | Description | Description |
|---|---|---|
| BEFORE or AFTER | Specifies whether the trigger is executed before or after the trigger event. | A BEFORE trigger fires before the trigger event is executed. An AFTER trigger fires after the trigger event is executed. |
| FOR EACH ROW | Specifies that the trigger is a row-level trigger. | If a DML statement affects multiple rows of data in a table, the row-level trigger fires for each row. |
| REFERENCING | Specifies the correlation name. The correlation name can be used to reference the current new and old column values in the Procedural Language (PL) block of the row-level trigger. The default correlation names are OLD and NEW. |
For example, the column names can be new.c1 and old.c2 |
Limits on triggers
Note the following limits when you use triggers:
The
SELECTstatement in a trigger must use theSELECT ... INTO ...syntax, or be theSELECTstatement used to define a cursor.Transaction control statements such as
COMMIT,ROLLBACK, andSAVEPOINTcannot be used in a trigger.A stored procedure or function called by a trigger cannot contain database transaction control statements either.
OLD and NEW pseudorecords
When a trigger is executed, it needs to reference the column values in records for INSERT, UPDATE, or DELETE operations and sometimes needs to reference the column values before or after the operations.
:NEWindicates the value of a column after an operation.:OLDindicates the value of a column before an operation.
The following table lists the validity of NEW and OLD records in different operations.
| Feature | INSERT | UPDATE | DELETE |
|---|---|---|---|
| OLD | N/A | Valid | Valid |
| NEW | Valid | Valid | N/A |
Examples
When data is dropped from the regions table, use triggers to move records with a region_id value greater than 3 to the reg_his table.
Create a table named
regions.obclient>CREATE TABLE regions( region_id NUMBER(5,0), region_name VARCHAR(50) ); Query OK, 0 rows affectedInsert data into the
regionstable.obclient> INSERT INTO regions VALUES(1,'Europe'),(4,'Middle East and Africa'),(3,'Asia'),(2,'Americas'); Query OK, 4 rows affected Records: 4 Duplicates: 0 Warnings: 0Create a table named
reg_his, which has the same structure as theregionstable.obclient> CREATE TABLE reg_his AS SELECT * FROM regions WHERE 1=2; Query OK, 0 rows affectedCreate a trigger named
del_new_region.obclient> CREATE 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; / Query OK, 0 rows affectedView the
regionstable.obclient>SELECT * FROM regions; +-----------+------------------------+ | REGION_ID | REGION_NAME | +-----------+------------------------+ | 1 | Europe | | 4 | Middle East and Africa | | 3 | Asia | | 2 | Americas | +-----------+------------------------+ 4 rows in setView the
reg_histable.obclient> SELECT * FROM reg_his; Empty setDelete the data records whose region_id is greater than 2 from the
regionstable.obclient> DELETE FROM regions WHERE region_id>2; Query OK, 2 rows affectedView the
regionstable again.obclient> SELECT * FROM regions; +-----------+-------------+ | REGION_ID | REGION_NAME | +-----------+-------------+ | 1 | Europe | | 2 | Americas | +-----------+-------------+ 2 rows in setView the
reg_histable again.obclient>SELECT * FROM reg_his; +-----------+------------------------+ | REGION_ID | REGION_NAME | +-----------+------------------------+ | 4 | Middle East and Africa | +-----------+------------------------+ 1 row in set