توضیحات
BEFOR DELETE Trigger به معنی این است که قبل از اجرای یک دستور delete، یک تریگر فعال (fire) شود.دستور
دستور Befor delete triger به صورت زیر نوشته میشود:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 |
CREATE [ OR REPLACE ] TRIGGER trigger_name BEFORE DELETE ON table_name [ FOR EACH ROW ] DECLARE -- variable declarations BEGIN -- trigger code EXCEPTION WHEN ... -- exception handling END ; |
مثال
مثالی از دستور ساخت Befor Delete triger را مشاهده میکنیم:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 |
CREATE TABLE orders ( order_id number(5), quantity number(4), cost_per_item number(6,2), total_cost number(8,2) ); CREATE OR REPLACE TRIGGER orders_before_delete BEFORE DELETE ON orders FOR EACH ROW DECLARE v_username varchar2(10); BEGIN -- Find username of person performing the DELETE on the table SELECT user INTO v_username FROM dual; -- Insert record into audit table INSERT INTO orders_audit ( order_id, quantity, cost_per_item, total_cost, delete_date, deleted_by ) VALUES ( :old.order_id, :old.quantity, :old.cost_per_item, :old.total_cost, sysdate, v_username ); END ; |