Dameng Database-Explanation and use of table-level triggers

Dameng Database - Simple explanation and use of table-level triggers

Introduction

This chapter mainly introduces the use of table-level triggers in DM database.

Article reference:
Dameng technical documentation

The version is as follows

name Version
DM database DM 8.0 and above
CPU architecture Mainstream CPUs at home and abroad such as x86, ARM, Loongson, and Feiteng

1. Basic definition

The triggering actions of table-level triggers are three data operation commands, namely INSERT , DELETE and UPDATE operations. According to the level of trigger, it can be divided into tuple level (row level) and statement level.

Tuple-level (row-level) trigger example:

-- **行级触发器,对触发命令所影响的每一条记录都激发一次**。
-- 假如一个UPDATE命令更新了500行记录,那么这个表上的元组级UPDATE触发器将被执行500次。
-- 通过FOR EACH ROW子句创建,用一个WHEN子句来限制针对当前记录是否执行该触发器。
-- WHEN子句包含一条布尔表达式,当它的值为TRUE时,执行触发器;否则,跳过该触发器。
CREATE  OR  REPLACE TRIGGER TRG_NAME   [WITH ENCRYPTION]
BEFORE|AFTER|INSTEAD OF 
INSERT OR DELETE OR UPDATE [OF 列名]
ON TABLE_NAME 
FOR EACH ROW  [WHEN 条件] --行级触发器中,此子句一定不能省略
BEGIN
 PRINT 'INSERT OR DELETE OR UPDATE OPERATION ON TABLE_NAME';--要执行的SQL。
END;

The following "[]" content is optional:

[WITH ENCRYPTION] : Encrypt the data. After encryption, the content of the SQL statement in the trigger cannot be seen.
UPDATE [OF column name] : If the UPDATE command is specified, you can further specify which column in the table is fired when the UPDATE command is affected.
FOR EACH ROW [WHEN condition] : For tuple-level triggers, a WHEN clause can be used to limit whether the trigger is executed for the current record. The WHEN clause contains a Boolean expression that, when it evaluates to TRUE, executes the trigger; otherwise, the trigger is skipped.

Statement-level trigger example:

-- **语句级触发器,对每个触发命令执行一次**。
-- 一条将500行记录插入表TEST中的INSERT语句,这个表上的语句级INSERT触发器只执行一次。
-- 通过FOR EACH STATEMENT子句创建的,此子句可省略。
CREATE OR REPLACE TRIGGER TRG_NAME
AFTER INSERT OR DELETE OR UPDATE ON TABLE_NAME
FOR EACH STATEMENT -- 语句级:此子句可省略
BEGIN
 PRINT 'INSERT OR DELETE OR UPDATE OPERATION ON TABLE_NAME';--要执行的SQL
END;

Trigger timing specification:
1. By specifying the BEFORE or AFTER keyword, choose to run the trigger before or after the trigger action;
2. By specifying the INSTEAD OF keyword, choose to replace the original operation when the action is triggered. INSTEAD OF allows to build view, and only supports row-level triggering.

Application scenarios:
Tuple-level triggers are often used in applications such as data auditing and integrity checking.
Statement-level triggers are generally used to introduce additional security measures to the types of operations performed on tables.

2. Use cases

Business requirements: There is an existing table. When one or more records are updated, the field F_upd_time (update time) is automatically updated to the time when the operation is performed.

Table Structure:

Insert image description here

Using DM Visual Tool
Under Triggers in Target Mode, select Table Level Triggers

Insert image description here

Right-click to create a new trigger
. Select the trigger time before the operation (BEFORE).
The fields triggered before the operation (BEFORE) can be checked except for upd_time.
The new and old record aliases are respectively new and old.
The trigger conditions and encryption definitions are optional.

Insert image description here

The trigger body is shown in the figure

Insert image description here
Click OK.
The specific sql is as follows

create or replace trigger "EIP"."trg_upd_w_yljl"
before UPDATE of "ID_","REF_ID_","F_drill_results","F_find_results","F_improvement","F_drill_type","F_departments","F_recorder","F_record_content","F_personnel","F_location","F_time","F_drill_name","F_organization","F_create_time","F_form_data_rev_"
on "EIP"."W_yljl"
referencing OLD ROW AS "old" NEW ROW AS "new"
for each row
BEGIN	
    new.F_upd_time:=sysdate;
END;

Guess you like

Origin blog.csdn.net/ChennyWJS/article/details/131913198