zoukankan      html  css  js  c++  java
  • Oracle触发器

    Oracle触发器详细介绍:

    触发器

    是特定事件出现的时候,自动执行的代码块。类似于存储过程,但是用户不能直接调用他们。

    功能:

    1、 允许/限制对表的修改

    2、 自动生成派生列,比如自增字段

    3、 强制数据一致性

    4、 提供审计和日志记录

    5、 防止无效的事务处理

    6、 启用复杂的业务逻辑

    实例:

    create trigger biufer_employees_department_id

           before insert or update

                  of department_id

                  on employees

           referencing old as old_value

                          new as new_value

           for each row

           when (new_value.department_id<>80 )

    begin

           :new_value.commission_pct :=0

    end;

    触发器的组成部分:

    1、 触发器名称

    2、 触发语句

    3、 触发器限制

    4、 触发操作

    1、 触发器名称

    create trigger biufer_employees_department_id

    命名习惯:

    biufer(before insert update for each row)

    employees 表名

    department_id 列名

    2、 触发语句

    before insert or update

                  of department_id

                  on employees

           referencing old as old_value

                          new as new_value

           for each row

    说明:

    1>、 无论是否规定了department_id ,对employees表进行insert的时候

    2>、 对employees表的department_id列进行update的时候

    3、 触发器限制

    when (new_value.department_id<>80 )

    限制不是必须的。此例表示如果列department_id不等于80的时候,触发器就会执行。

    其中的new_value是代表更新之后的值。

    4、 触发操作

    是触发器的主体

    begin

           :new_value.commission_pct :=0;

    end;

    主体很简单,就是将更新后的commission_pct列置为0

    -------------------------------------------------------

    1、 语句触发器

    是在表上或者某些情况下的视图上执行的特定语句或者语句组上的触发器。能够与INSERT、UPDATE、DELETE或者组合上进行关联。但是无论使用什么样的组合,各个语句触发器都只会针对指定语句激活一次。比如,无论update多少行,也只会调用一次update语句触发器。

    例子1:

    需要对在表上进行DML操作的用户进行安全检查,看是否具有合适的特权。

    Create table foo(a number);

    Create trigger biud_foo

           Before insert or update or delete

                  On foo

    Begin

           If user not in (‘DONNY’) then

                  Raise_application_error(-20001, ‘You don’t have access to modify this table.’);

           End if;

    End;

    即使SYS,SYSTEM用户也不能修改foo表

    例子2:确定是哪个语句起作用

    即是INSERT/UPDATE/DELETE中的哪一个触发了触发器?

    可以在触发器中使用INSERTING / UPDATING / DELETING 条件谓词,作判断:

    begin

            if inserting then

                   -----

            elsif updating then

                   -----

            elsif deleting then

                   ------

            end if;

    end;

  • 相关阅读:
    IfcSameDirection
    IfcSameCartesianPoint
    java多个文件合并为一个文件
    matlab pan_tompkin算法
    IfcSameAxis2Placement
    IfcOrthogonalComplement
    IfcNormalise
    IfcMakeArrayOfArray
    matlab 日期 年月日时分秒毫秒
    IfcListToArray
  • 原文地址:https://www.cnblogs.com/gcl6/p/14654348.html
Copyright © 2011-2022 走看看