zoukankan      html  css  js  c++  java
  • oracle之触发器基础篇

    一、触发器:是一个与表关联的、存储的PL/SQL程序,当用户执行了insert、update、delete操作之后,

    oracle自动地执行触发器中定义的语句序列。
     作用:
    
     1.数据确认:如员工涨薪后,新工资不能少于之前的工资。
    
     2.安全性检查:如禁止非工作时间插入新员工。
    
     3.做审计,跟踪上所做的数据操作等。
    
     4.数据的备份与同步。
    

    类型:

      语句级触发器:在指定的操作语句之前或者之后执行一次,不管这个语句影响了多少行语句。
    
      行级触发器:触发语句作用的每一条记录都被触发,在行级触发器中使用old和new伪记录变量, 识别值的状态
    

    二、语法

      ---创建触发器
    
         create [or replace] trigger 触发器名
    
              before/after
    
            insert/update/delete [of 列名]
    
           on 表名
    
           [for each row [when(条件)]]
    
            declare
    
            ...
    
           begin
    
             PLSQL块
    
           end
    
     ---删除触发器
    
       drop trigger 触发器名
    

    三、实例

     ---新员工入职后,输出 "欢迎加入" 字符串。创建触发器
    
      create or replace trigger trig_show_hello
    
      after ---after 表示操作后触发
    
         insert on emp
    
       declare
    
       begin
    
       dbms_output.put_line('欢迎加入');
    
      end;
    
    ---插入员工。插入成功后就会触发上面的 trig_show_hello 触发器
    
      insert into emp
    
       values(9996,'华安','MANAGER','7698',sysdate,9888.87,300,30);
    
     ---更新所有员工的薪水,同一加 100,创建触发器,更新完成后给出提示
    
      create or replace trigger tric_update_sal
    
      after update on emp
    
      for each row --表示行级触发器
    
         declare
    
         begin
    
           --- :old 表示操作前的记录行,:new 表操作后的记录行
            dbms_output.put_line('原来工资:'||:old.sal|| ' 现在薪水:'||:new.sal);
    
         end;
    
           ---更新员工薪水。自动触发上面的 tric_update_sal 触发器
    
        update emp set sal = sal+100;
  • 相关阅读:
    Poj 2017 Speed Limit(水题)
    Poj 1316 Self Numbers(水题)
    Poj 1017 Packets(贪心策略)
    Poj 1017 Packets(贪心策略)
    Poj 2662,2909 Goldbach's Conjecture (素数判定)
    Poj 2662,2909 Goldbach's Conjecture (素数判定)
    poj 2388 Who's in the Middle(快速排序求中位数)
    poj 2388 Who's in the Middle(快速排序求中位数)
    poj 2000 Gold Coins(水题)
    poj 2000 Gold Coins(水题)
  • 原文地址:https://www.cnblogs.com/heavenTang/p/12954899.html
Copyright © 2011-2022 走看看