数据库里面触发器的使用-外界触发来发生某种行为

背景:
自动化测试往往会产生很多脏数据, 需要建立一种机制, 来定期删除数据
策略:建立触发器, 实时删除某些数据 【数据库里面触发器的使用-外界触发来发生某种行为】Final:
1. 创建函数delete_old_lalala_antities()
delete from lalala_antit where lalala_name like '%rrr_lalala_antities_automation1%' and lalala_antit_id <(select max(lalala_antit_id) from lalala_antit)-3;
创建触发器:
CREATE TRIGGER delete_old_lalala_antities AFTER INSERT ON lalala_antit
FOR EACH ROW EXECUTE PROCEDURE delete_old_lalala_antities();
create trigger delete_old_lalala_antities after insert on lalala_antit for each statement execute procedure delete_old_lalala_antities();
查看创建的触发器:
https://www.postgresql.org/docs/12/plpgsql-trigger.html#PLPGSQL-DML-TRIGGER
Command:
select * from pg_trigger; 查看触发器
\df+ ------- 查看function
\dy+ -------查看事件触发器
\sf asdf2() ----查看function 详se
DROP TRIGGER asdf2 ON lalala_antit; ----删除trigger. Drop
insert into lalala_antit(lalala_name) values ('hello'). ------ 插入数据命令


改动一个function
CREATE OR REPLACE FUNCTION delete_old_lalala_antities ()
RETURNS TRIGGER AS $delete_antit$
BEGIN delete from lalala_antit where lalala_name like '%rrr_lalala_antities_automation1%' and lalala_antit_id <(select max(lalala_antit_id) from lalala_antit)-10;
return NULL; END;
$delete_antit$ LANGUAGE plpgsql
$delete_antit$ LANGUAGE plpgsql

    推荐阅读