• PG语句级别触发器使用示例


    目录

    • 构造表与数据
    drop table if exists ta;
    drop table if exists tb;
    create table ta(id int, name varchar);
    create table tb(id int);
    insert into ta select  n, n || 'name' from generate_series(1, 100, 1) as t(n);
    
    
    • 创建触发器与触发器函数
    
    CREATE OR REPLACE FUNCTION func_sum_max_id_to_tb() 
    RETURNS TRIGGER 
    AS
    $BODY$
    DECLARE
    BEGIN
    			insert into tb select max(id) from ta;
    			raise notice 'hello ';
          RETURN NULL;
    END;
    $BODY$
    LANGUAGE 'plpgsql' VOLATILE CALLED ON NULL INPUT SECURITY INVOKER;
    
    create trigger trg_ta after insert or delete or update on ta for each STATEMENT EXECUTE PROCEDURE func_sum_max_id_to_tb();
    
    • 测试语句级别触发器

    • 删除之前

    select * from ta where id<11;
    
    • 结果:
      在这里插入图片描述
    • 删除之后
    delete from ta where id<11;
    

    在这里插入图片描述

    • 查看表tb
    select * from tb;
    
    • 结果
      在这里插入图片描述
    • 可见,在使用语句级别触发器的时候,即使删除了10条记录,但是只触发了一次触发器
  • 相关阅读:
    Cheat Engine 创建线程
    Cheat Engine 人造指针
    Cheat Engine 特征码
    Cheat Engine 自动注入
    Cheat Engine 作弊表框架代码
    Cheat Engine 修改汇编指令
    Shell 选择排序
    Shell 冒泡排序
    Selenium API常用方法
    Selenium数据驱动
  • 原文地址:https://www.cnblogs.com/yldf/p/11899944.html
Copyright © 2020-2023  润新知