当前位置: 代码迷 >> SQL >> PL/SQL触发器基础及例证
  详细解决方案

PL/SQL触发器基础及例证

热度:58   发布时间:2016-05-05 11:06:55.0
PL/SQL触发器基础及例子

?

触发器的简介;

触发器的定义就是说某个条件成立的时候,触发器里面所定义的语句就会被自动的执行。因此触发器不需要人为的去调用,也不能调用。触发器和过程函数类似 过程函数必须要调用,

?

一个表中最多只能有12个触发器类型的,触发器和过程函数相似 触发器不需要调用直接执行,
触发时间:指明触发器何时执行,该值可取:before:表示在数据库动作之前触发器执行;after:表示在数据库动作之后触发器执行。触发事件:指明哪些数据库动作会触发此触发器:insert:数据库插入会触发此触发器;update:数据库修改会触发此触发器;delete:数据库删除会触发此触发器。表 名:数据库触发器所在的表。for each row:对表的每一行触发器执行一次。如果没有这一选项,则只对整个表执行一次。
 

触发器的创建语法:

?

触发器emp表中添加的数据,将删除的数据添加到emp_bak中

1),复制scott用户下的emp表的结构

?

create table emp_bak as select * from emp where 1=2;

2),向emp表插入一条数据

?

--插入数据到empinsert into emp values(1234,'wangerxiao','SALESMAN',7902,to_date('1990-1-1','yyyy-mm-dd'),1000,100,20);

?

3),创建emp表删除添加的触发器

create  or replace trigger tr_emp --定义触发器before delete --执行时间on emp_bak  --在emp_bak表中for each row -- 行级删除begin--:old 操作前的数据   :new 是操作后的数据  insert into emp_copy values(:old.empno,:old.ename,:old.job,:old.mgr,:old.hiredate,:old.sal,:old.comm,:old.deptno);end;

?

4),删除添加的数据

SQL> delete from emp_bak where empno=1234; 1 row deleted

?

5),查询emp_bak表

数据已经添加到emp_bak表中了

SQL> select * from emp_bak; EMPNO ENAME      JOB         MGR HIREDATE          SAL      COMM DEPTNO----- ---------- --------- ----- ----------- --------- --------- ------ 1234 wangerxiao SALESMAN   7902 1990/1/1      1000.00    100.00     20

?

6),触发器的删除

drop trigger tr_emp;

?

?

完成上述的例子,我们已经会创建基本都是的触发器了

?

?

?

2,---------------------使用触发器限制用户emp对表的操作(非工作时间只能查询数据)

--限制对emp表修改(包括INSERT,DELETE,UPDATE)的时间范围,
--即不允许在非工作时间(周末,8:30以前 17:30以后)修改emp表。

?

创建触发器

create or replace trigger tr_empsbefore delete or update or inserton empfor each rowbegin   if (to_char(sysdate,'DAY') in ('星期六','星期日') or to_char(sysdate,'HH24:MI') not between '08:30' and '22:00' )  then    raise_application_error(-20001,'不能在这个时间操作数据'); end if;end;

使用触发器限制的时候,用户输入的可能会报错,所以需要定义异常

?

 raise_application_error(-20001,'不能在这个时间操作数据'); 

?

在8点30到22:00可以 删, 更新,曾加

SQL> update emp set sal=1000 where ename='SMITH'; 1 row updated

?

不在规定时间内 更新数据

SQL> update emp set sal=1000 where ename='SMITH'; update emp set sal=1000 where ename='SMITH' ORA-20001: 不能在这个时间操作数据ORA-06512: 在 "SCOTT.TR_EMPS", line 7ORA-04088: 触发器 'SCOTT.TR_EMPS' 执行过程中出错 

?

?

3---------------------------------------触发器限定操作

--限定只对部门号为10的记录进行行触发器操作。
--如果改变的sal和comm比现在少的话报错,删除记录的话也报错,其他操作执行。

?

create or replace trigger tr_empsbrfore update of sal, comm or daleteon empfor each row  when (old.deptno=10)--只有当部门号是10的时候才触发begin    case      when updating('sal') then      if :old.sal<:new.sal then       raise_application_error(-20002,'不能降低工资');      end  if;     when updating('comm') then      if :old.comm<:new.comm then      raise_application_error(-20003,'不能降低奖金!');    end if;  when deleting then  raise_application_error(-20004,'不能开除这个部门的员工');         end case;end;

?

?

4-----------------------触发器的联机更新


--利用行触发器实现级联更新
--如果将dept表部门编号改为新值,
--就需要将emp表中的所有为该部门的部门编号改为新值

create or replace trigger tr_update_dempafter update of deptnoon deptfor each row  begin    update emp set deptno=:new.deptno where deptno=:old.deptno;  end;

?

?

?

?

?

?

?

?

?

?

?

?

5--------------------------触发器对视图的操作

?

?

?

?

?

?

1 楼 紫梦飘逸 昨天  
      
2 楼 xunmengsj 昨天  
真心想不明白为什么会有人踩
3 楼 Y_1746119035 昨天  
谁踩了,拖出去打一顿
  相关解决方案