oracle触发器

select * from scott.emp
-----------------------------语句级触发器---------------------------
create or replace trigger addinfo_tri
before insert or update or delete on scott.emp
begin
if to_char(sysdate,'dy','nls_date_language=american') in ('sat','sun') then
raise_application_error(-20001,'不能在周末修改信息');
end if;
end;

update scott.emp set sal=5000 where empno=7369;

-----------------------------after级触发器   审计DML操作---------------------------
create table audit_table(
name varchar2(20),
ins int,
upd int,
del int,
starttime date,
endtime date
)tablespace PraMing;


select * from  audit_table;

create or replace trigger alterinfo_tri
after insert or update or delete on scott.emp
declare
v_temp int;
begin
select count(*) into v_temp from audit_table where name='zhang';
if v_temp=0 then
insert into audit_table values('zhang',0,0,0,sysdate,null);
end if;
case
when inserting then
update audit_table set ins=ins+1,endtime=sysdate where name='zhang';
when updating then
update audit_table set upd=upd+1,endtime=sysdate where name='zhang';
when deleting then
update audit_table set del=del+1,endtime=sysdate where name='zhang';
end case;
end;


select * from  audit_table;
insert into audit_table values('lisi',0,0,0,sysdate,null);

update scott.emp set sal=999 where ename='SMITH';


begin
dbms_output.put_line(dbms_utility.get_time());
end;
-----------------------------行级触发器---------------------------
create or replace trigger altersal_tri
before update on scott.emp
for each row
begin
if :new.sal<:old.sal then
raise_application_error(-20009,'不能低于原有工资');
end if;
end;

update scott.emp set sal=99.9 where ename='SMITH';


-------------------instead of  触发器   专用于视图------------------------------------

select * from scott.emp
select * from scott.dept
select e.empno,e.ename,e.sal,d.dname from scott.emp e inner join scott.dept d on d.deptno=e.deptno;

create or replace view allinfo
as
select e.empno,e.ename,e.sal,d.dname from scott.emp e inner join scott.dept d on d.deptno=e.deptno;

select * from allinfo

create or replace trigger allinfoview_tri
instead of update on allinfo
begin
raise_application_error(-20008,'修改信息');
end;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值