• oracle触发器


    1. 什么是触发器?

      当用户满足某一条件(如:登陆数据库, insert, update, delete,create等等)时, 会引起某个存储过程的

      自动执行, 我们把这个隐含被调用的存储过程就称为触发器.

    2.触发器的分类

      dml触发器    # insert delete update

      ddl触发器    # create table; create view; drop...

      系统触发器  # 与系统行为有关的触发器, 如登陆, 退出, 启动/关闭数据库等

    3.创建触发器

      create or replace trigger trigger_name    # trigger_name为自定义的触发器名称

      { before | after }    # 设定在之前/之后触发

      { insert|delete|update [of column1, column2]}  # 设定触发的条件

      on 方案.表名      # 方案即用户空间

      [for each row]    # 设定行级触发器, 如没有则为语句级触发器

      [when condition]

      begin

      这里放触发器执行时要干的事   # 如: dbms_output.put_line('输出内容到窗口')

      if 条件 then dbms_output.put_line('输出内容到窗口')

      RAISE_APPLICATION_ERROR(-2000, '描述')   # 用来阻止触发条件的继续执行

      end if;

      end;

      补充: RAISE_APPLICATION_ERROR这个是oracle定义的用来抛异常的函数, 它有两个参数

        第一个是错误号, 自定义,范围(-20000至-20999), 第二个是提示信息.

    4.行级触发器和语句级触发器:

      行级触发器: 涉及多少行就触发执行多少次;

      语句级触发器: 只会在执行触发语句时触发一次, 不论涉及到多少行.

    5.谓词的使用(inserting, updating, deleting)

      create or replace trigger trigger_name

      before

      insert or update or delete on

      scott.emp          # 这里用来指定哪个用户的哪个表

      begin

      case

        when inserting then

        dbms_output.put_line('不能添加')

        RAISE_APPLICATION_ERROR(-20001, '不能添加')

        when updating then

        dbms_output.put_line('不能更新')

        RAISE_APPLICATION_ERROR(-20002, '不能更新')

        when deleting then

        dbms_output.put_line('不能删除')

        RAISE_APPLICATION_ERROR(-20003, '不能删除')

      end case;

      end;

    6.:new和:old的使用. 这两能代指新值和原值, 使用时必须指定为行级触发器, 下图中忘记加了

      

     7. 让触发器中put_line内容输出到窗口办法:

      命令行输入set serveroutput on;

  • 相关阅读:
    web框架本质及第一个Django实例
    jQuery练习题HTML文件
    jQuery快速入门
    前端之JS
    前端基础之HTML
    前端基础之css
    并发编程之 协程
    Linux目录结构详解
    第三周 time库
    网络打印机拒绝访问,无法连接处理方法汇总
  • 原文地址:https://www.cnblogs.com/quzq/p/11336484.html
Copyright © 2020-2023  润新知