17,377
社区成员
发帖
与我相关
我的任务
分享
CREATE OR REPLACE TRIGGER trg_1
AFTER INSERT OR UPDATE OR DELETE ON test1
FOR EACH ROW
BEGIN
IF inserting THEN
INSERT INTO newtable
(a1, b1, c1, d1, remark)
VALUES
(:new.a1, :new.b1, :new.c1, :new.d1, 'insert');
ELSIF updating THEN
INSERT INTO newtable
(a1, b1, c1, d1, remark)
VALUES
(:old.a1, :old.b1, :old.c1, :old.d1, 'update');
ELSIF deleting THEN
INSERT INTO newtable
(a1, b1, c1, d1, remark)
VALUES
(:old.a1, :old.b1, :old.c1, :old.d1, 'delete');
END IF;
END;
-- 删除怎么记录?
SQL>
SQL> create table test(a int, b int, c int, remark varchar(30));
Table created
SQL> create trigger tri_test
2 before insert or update on test
3 for each row
4 begin
5 if inserting then
6 :new.remark := :new.remark || '/insert';
7 elsif updating then
8 :new.remark := :new.remark || '/update';
9 end if;
10 end;
11 /
Trigger created
SQL> insert into test(a,b,c) values(1,2,3);
1 row inserted
SQL> insert into test(a,b,c) values(4,5,6);
1 row inserted
SQL> update test set a = 100 where a =1 ;
1 row updated
SQL> col a format a5;
SQL> col b format a5;
SQL> col c format a5;
SQL> select * from test;
A B C REMARK
----- ----- ----- ------------------------------
100 2 3 /insert/update
4 5 6 /insert
SQL> drop table test purge;
Table dropped
SQL>