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

    DML触发器:是一种用在 表 上的触发器,通常在 insert,Update, delete语句上应用。

    如果触发器仅影响一行,则触发器执行顺序:

    (1)before 语句触发器

    (2)before 行级触发器

    (3)执行 DML语句

    (4)after 行级触发器

    (5)after 语句触发器

    如果触发器影响多行,则触发器执行顺序:

    (1)before语句触发器

    (2)被影响的第一行的before行触发器

    (3)第一行执行DML语句

    (4)第一行的after行级触发器

    (5)被影响的第二行的before行触发器

    (6)第二行执行DML语句

    (7)第二行的after行级触发器

    (8)after语句触发器

    语法:

    1 create or replace trigger [schema.]trigger_name
    2 {before | after} {delete | insert | {update [of column_list]}} on [schema.]table --触发时机
    3 [referencing {OLD as old_name} | {NEW as new_name} | {Parent as parent_name}]   --定义OLD、NEW的别名
    4 [For each row]                                        --行级触发器 or 语句级
    5 [When (condition)]                                     --触发器被触发后,是否执行触发体代码的条件
    6 plsql_clock | call_procedure_statement                        

    其中,(1)before | after:指定触发器是在对表的操作发生之前触发还是之后触发。

     可以同时指定多个动作,

    1 before delete or insert or update on tb_test

     在使用Update作为触发行为时,还可以使用 Update of 指定一个或多个字段,仅在这些字段被更新时才会触发:

    1 before update of column1, column2 ON tb_test

    (2)when condition : 只有满足when指定的条件,才会执行触发体中的代码。在when子句中,可以使用以下几个谓词:

      OLD 谓词:执行前的字段的值的名称,比如 Update一个表时,使用 OLD.column1 是指执行Update操作之前的列的值。

      NEW 谓词:执行后的字段的值的名称,比如 Update一个表时,使用 NEW.column1是指执行 Update操作之后的列的值。

          在触发器内部使用NEW和OLD谓词时,要在前面加上 :冒号,在WHEN子句中,可以不加上冒号。

    (3)可以在触发器体的语句块中使用 INSERTING、UPDATING、DELETING谓词,这些谓词会返回相应的DML操作的布尔值,

      如果为TRUE,则表示执行了相应的 INSERT、UPDATE、DELETE操作。

    eg:

    create or replace trigger tr_emp_test
    before insert or update or delete ON employee
    referencing old as old_name new as new_name
    for each row
    when(new_name.emp_name > old_name.emp_name)
    DECLARE
      v_sal number;
    begin
      if deleting then
         null;
      end if;
    end;
  • 相关阅读:
    洛谷P2024 [NOI2001]食物链 题解 并查集
    洛谷P1632 点的移动 题解 枚举
    洛谷P2733 家的范围 题解 动态规划
    洛谷P1432 倒水问题 题解 广搜经典入门题(SPFA求解)
    18个常用的Linux 命令
    python 基础知正则表达式
    python 多功能下载网页
    Python3 安装urllib2包之小坑
    python 爬虫需要的库
    python html简单入门
  • 原文地址:https://www.cnblogs.com/ly01/p/8622387.html
Copyright © 2011-2022 走看看