触发器-2
2012-03-22 10:20 java环境变量 阅读(188) 评论(0) 编辑 收藏 举报---行触发器
------------------------------------------------------
第一部分:before触发器
------------------------------------------------------
--案例01:新建一个使用属性:new的insert触发器
create or replace trigger tr01
before insert on student
for each row --必须是行级触发器
begin
dbms_output.put_line('这个是行前触发器!');
dbms_output.put_line('new id:' || :new.stuid);
dbms_output.put_line('student name:' || :new.stuname);
dbms_output.put_line('student sex:' || :new.sex);
end;
--案例02:新建一个使用属性:old的delete触发器
create or replace trigger tr01
before delete on student
for each row --必须是行级触发器
begin
dbms_output.put_line('这个是行前触发器!');
dbms_output.put_line('old id: ' ||:old.stuid);
dbms_output.put_line('old name: ' ||:old.stuname);
dbms_output.put_line('old sex: ' ||:old.sex);
end;
--案例03:新建一个使用属性:old和:new的update触发器
create or replace trigger tr01
before update on student
for each row --必须是行级触发器
begin
dbms_output.put_line('这个是行前触发器!');
dbms_output.put_line('下面是你新更新时删除的值');
dbms_output.put_line('old id:' || :old.stuid);
dbms_output.put_line('old name:' || :old.stuname);
dbms_output.put_line('old sex:' || :old.sex);
dbms_output.put_line('*****************************');
dbms_output.put_line('下面是你新插入的值');
dbms_output.put_line('new id:' || :new.stuid);
dbms_output.put_line('new name:' || :new.stuname);
dbms_output.put_line('new sex:' || :new.sex);
end;
--案例04:新建一个使用条件的触发器
create or replace trigger tr01
before delete on student
for each row --必须是行级触发器
when (old.sex='男') /*这里old前面不能使用冒号只有在主体部分才允许使用冒号*/
begin
dbms_output.put_line('这个是行前触发器!');
dbms_output.put_line('old id' ||:old.stuid);
dbms_output.put_line('old name' ||:old.stuname);
dbms_output.put_line('old sex' ||:old.sex);
end;
--案例05:
create or replace trigger tr01
before insert or delete or update of sid
on apple
for each row
begin
if inserting then
dbms_output.put_line('数据将要插入,请注意查收!');
dbms_output.put_line('请求正在执行,请稍候....................');
dbms_output.put_line('.........................................');
elsif deleting then
begin
dbms_output.put_line('apple表中的数据'||:old.sid||'将被删除!');
dbms_output.put_line('请求正在执行,请稍候....................');
dbms_output.put_line('.........................................');
end;
elsif updating then
begin
dbms_output.put_line('apple表中的数据'||:old.sid||'将被删除!');
dbms_output.put_line('请求正在执行,请稍候....................');
dbms_output.put_line('.........................................');
dbms_output.put_line('apple表中的数据'||:new.sid||'将被插入!');
dbms_output.put_line('请求正在执行,请稍候....................');
dbms_output.put_line('.........................................');
end;
else
dbms_output.put_line('未知的错误!请核实再操作!');
end if;
end;
--案例06:
create or replace trigger tr02
after insert or delete or update of sid
on apple
for each row
begin
if inserting then
dbms_output.put_line('数据已经插入,请注意查收!');
elsif deleting then
begin
dbms_output.put_line('apple表中的数据'||:old.sid||'已经被删除!');
end;
elsif updating then
begin
dbms_output.put_line('apple表中的数据'||:old.sid||'已经被删除!');
dbms_output.put_line('apple表中的数据'||:new.sid||'已经被插入!');
end;
else
dbms_output.put_line('未知的错误!请核实再操作!');
end if;
end;
--案例07:监视用户登录数据库信息
create table user_login_information
(
who varchar2(30),
when date,
action varchar2(20)
);
--方法01:
create or replace trigger tr_user_login
before insert or update or delete
on apple
declare
in_action user_login_information.action%type;
begin
if inserting then
begin
in_action:='插入';
dbms_output.put_line('用户对表apple进行了插入操作!');
end;
elsif updating then
begin
in_action:='更新';
dbms_output.put_line('用户对表apple进行了更新操作!');
end;
elsif deleting then
begin
in_action:='删除';
dbms_output.put_line('用户对表apple进行了删除操作!');
end;
end if;
insert into user_login_information(who, when, action) values (USER,SYSDATE,in_action);
end;
--方法02:
create or replace trigger tr_user_login
before insert or update or delete
on apple
declare
in_action user_login_information.action%type;
begin
case
when inserting then
begin
in_action:='插入';
dbms_output.put_line('用户对表apple进行了插入操作!');
end;
when updating then
begin
in_action:='更新';
dbms_output.put_line('用户对表apple进行了更新操作!');
end;
when deleting then
begin
in_action:='删除';
dbms_output.put_line('用户对表apple进行了删除操作!');
end;
end case;
insert into user_login_information(who, when, action) values (USER,SYSDATE,in_action);
end;
--案例08:新建一个混合触发器01
(1)新建一个表
create table deltable as select * from student where stuid='';
(2)新建一个混合触发器
create or replace trigger tr03
before insert or update or delete on student
for each row
begin
if inserting then
insert into deltable values(:new.stuid, :new.stuname, :new.sex);
end if;
if deleting then
insert into deltable values (:old.stuid, :old.stuname, :old.sex);
end if;
exception
when others then
dbms_output.put_line('不可预知的错误!');
end;
--案例09:新建一个混合触发器02
(1)新建一个表
create table deltable as select * from student where stuid='';
create table updtable as select * from student where stuid='';
create table intable as select * from student where stuid='';
(2)新建一个混合触发器
create or replace trigger tr03
before insert or update or delete on student
for each row
begin
if inserting then
insert into intable values(:new.stuid, :new.stuname, :new.sex);
end if;
if deleting then
insert into deltable values (:old.stuid, :old.stuname, :old.sex);
end if;
if updating then
insert into updtable values(:new.stuid, :new.stuname, :new.sex);
insert into updtable values (:old.stuid, :old.stuname, :old.sex);
end if;
exception
when others then
dbms_output.put_line('不可预知的错误!');
end;
--案例10:新建一个混合触发器03
(1)新建一个表
create table deltable as select * from student where stuid='';
create table updtable as select * from student where stuid='';
create table intable as select * from student where stuid='';
(2)新建一个混合触发器
create or replace trigger tr03
before insert or update or delete on student
for each row
begin
if inserting then
insert into intable values(:new.stuid, :new.stuname, :new.sex);
dbms_output.put_line('数据插入成功!');
end if;
if deleting then
insert into deltable values (:old.stuid, :old.stuname, :old.sex);
dbms_output.put_line('数据删除成功!');
end if;
if updating then
insert into updtable values(:new.stuid, :new.stuname, :new.sex);
insert into updtable values (:old.stuid, :old.stuname, :old.sex);
dbms_output.put_line('数据更新成功!');
end if;
exception
when others then
dbms_output.put_line('不可预知的错误!');
end;