注意,为了能在终端看见put_line的输出,还需要先开启
set serveroutput on
--用来测试的表create table test_tri(
a1 int,
a2 int
)
tablespace test
-----行级insert触发器-----
1) 插入的一行新数据保存在:new
2) insert触发器没有:old值3) 对:new的修改,只能定义在before类型的触发器中---触发器语法---create or replace trigger 名称
[after|before] [delete|update|insert]
[of 列名] [on 表名]
[referencing new as 新行别名 old as 旧行别名]
[for each row] [when(条件)]
declare
....
begin
...
exception
....
end; --定义create or replace trigger test_before_insert
before insert on test_tri
for each row
when(new.a2 is null)begin
dbms_output.put_line('insert row original: a1=' || :new.a1 || ' a2=' || :new.a2);
:new.a2 := :new.a1 * 2;
end;
/
create or replace trigger test_after_insert
after insert on test_tri
for each row
begin
dbms_output.put_line('insert row actual: a1=' || :new.a1 || ' a2=' || :new.a2);
end;
/
--测试SQL> insert into test_tri(a1) values(1);
insert row original: a1=1
a2=insert row actual: a1=1 a2=2
SQL> insert into test_tri values(2,3);
insert row actual: a1=2 a2=3
SQL> select * from test_tri;
A1 A2
---------- ----------
1
2(由触发器生成的值) 2 3
-----DDL触发器-----
--任何create语句都会触发这个语句create or replace trigger test_ddl_trigger
before create on schema
begin
dbms_output.put_line( 'DDL Trigger' );
insert into test_tri values(9,9);
end;
posted on 2010-01-11 14:58
Jcat 阅读(251)
评论(0) 编辑 收藏 所属分类:
Database