触发器-2
来源:互联网 发布:办公软件excel心得体会 编辑:程序博客网 时间:2024/05/22 00:11
---行触发器
------------------------------------------------------
第一部分: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;
- 触发器-2
- 触发器2
- Oracle触发器2-DML触发器
- 1, insert触发器; 2, update触发器; 3, delete触发器;
- SQL server触发器入门2-重命名触发器
- SQL server触发器入门2-重命名触发器
- Oracle触发器(建立系统事件触发器)2
- PL/SQL_触发器2(行触发器)
- 有关触发器 2
- 触发器示例 2
- PostgreSQL触发器2
- DDL 触发器 2
- oracle包、触发器2
- 4-2触发器
- 触发器(2)
- 触发器
- 触发器
- 触发器
- 程序员基本素质
- 对比的架构
- js验证表单
- 研究称水城威尼斯仍缓慢下沉 且发生倾斜
- python os.path常用模块
- 触发器-2
- 使用软引用缓存Bitmap
- 值得学习的Android开源项目
- 触摸屏原理
- 使用CopyOnWriteArrayList来解决ArrayList上的ConcurrentModificationException
- OPENCV之形态学处理
- WPF 模板中的控件修改
- Linux下,不能以根用户身份运行google chrome 浏览器的解决办法
- 《设计模式解析》 第7章 Adapter模式 复习题