PostgreSQL trigger (function) examples
postgres=# \c warehouse_db
You are now connected to database "warehouse_db" as user "postgres".
warehouse_db=# set search_path ='record';
SET
warehouse_db=# show search_path ;
search_path
-------------
record
(1 row)
warehouse_db=# create table warehouse_tb1(warehouse_id integer not null,warehouse_name text not null,year_created integer,street_address text,city character varying(100),state character varying(2),zip character varying(10),
warehouse_db(# constraint "prim_key" primary key (warehouse_id));
CREATE TABLE
warehouse_db=# insert into warehouse_tb1 (warehouse_id,warehouse_name,year_created,street_address,city,state,zip)values (1,'Mark Corp',2009,'207-F Main Service Road Ease','New London','CT',4321);
INSERT 0 1
warehouse_db=#
warehouse_db=# insert into warehouse_tb1 (warehouse_id ,warehouse_name,year_created,street_address,city,state,zip)values (2,'Bill & Co',2014,'Lilly Road','New London','CT',4321);
INSERT 0 1
warehouse_db=# select warehouse_id,warehouse_name,state from warehouse_tb1;
warehouse_id | warehouse_name | state
--------------+----------------+-------
1 | Mark Corp | CT
2 | Bill & Co | CT
(2 rows)
warehouse_db=# create or replace function warehouse_audit_func() returns trigger as $first_trigger$
warehouse_db$# begin
warehouse_db$# insert into warehouse_audit (wlog_id,insertion_time) values (new.warehouse_id,current_timestamp);
warehouse_db$# return NEW;
warehouse_db$# end;
warehouse_db$# $first_trigger$ language plpgsql;
CREATE FUNCTION
warehouse_db=# create trigger audit_trigger
warehouse_db-# after insert on warehouse_tb1
warehouse_db-# for each row
warehouse_db-# execute procedure warehouse_audit_func();
CREATE TRIGGER
warehouse_db=# insert into warehouse_tb1 (warehouse_id ,warehouse_name,year_created,street_address,city,state,zip)values (3,'West point',2013,'Down Town','New London','CT',4321);
INSERT 0 1
warehouse_db=# select * from warehouse_audit ;
wlog_id | insertion_time
---------+-------------------------------
3 | 2015-10-14 10:49:05.819715+08
(1 row)
warehouse_db=#
warehouse_db=# select warehouse_id, warehouse_name,state from warehouse_tb1 ;
warehouse_id | warehouse_name | state
--------------+----------------+-------
1 | Mark Corp | CT
2 | Bill & Co | CT
3 | West point | CT
(3 rows)
warehouse_db=# drop table warehouse_audit cascade;
DROP TABLE
warehouse_db=# create table warehouse_audit(wlog_id int not null,insertion_time text not null,operation_detail character varying);
CREATE TABLE
warehouse_db=# CREATE OR REPLACE FUNCTION warehouse_audit_func_all()
warehouse_db-# RETURNS trigger AS $BODY$
warehouse_db$# BEGIN
warehouse_db$# --this IF block confirms the operation type to be INSERT.
warehouse_db$# IF (TG_OP = 'INSERT') THEN
warehouse_db$# INSERT INTO warehouse_audit
warehouse_db$# (wlog_id, insertion_time, operation_detail)
warehouse_db$# VALUES
warehouse_db$# (new.warehouse_id, current_timestamp,'INSERT operation performed. Row
warehouse_db$# with id '||NEW.warehouse_id|| 'inserted');
warehouse_db$# RETURN NEW;
warehouse_db$# --this IF block confirms the operation type to be UPDATE.
warehouse_db$# ELSIF (TG_OP = 'UPDATE') THEN
warehouse_db$# INSERT INTO warehouse_audit
warehouse_db$# (wlog_id, insertion_time, operation_detail)
warehouse_db$# VALUES
warehouse_db$# (NEW.warehouse_id, current_timestamp,'UPDATE operation performed. Row
warehouse_db$# with id '||NEW.warehouse_id||' updates values '||OLD||' with '|| NEW.*
warehouse_db$# ||'.');
warehouse_db$# RETURN NEW;
warehouse_db$# --this IF block confirms the operation type to be DELETE
warehouse_db$# ELSIF (TG_OP = 'DELETE') THEN
warehouse_db$# INSERT INTO warehouse_audit
warehouse_db$# (wlog_id, insertion_time, operation_detail)
warehouse_db$# VALUES (OLD.warehouse_id, current_timestamp,'DELETE operation
warehouse_db$# performed. Row with id '||OLD.warehouse_id|| 'deleted ');
warehouse_db$# RETURN OLD;
warehouse_db$# END IF;
warehouse_db$# RETURN NULL;
warehouse_db$# END;
warehouse_db$# $BODY$ LANGUAGE plpgsql;
CREATE FUNCTION
warehouse_db=# create trigger audit_all_ops_trigger
warehouse_db-# after insert or update or delete on warehouse_tb1
warehouse_db-# for each row
warehouse_db-# execute procedure warehouse_audit_func_all();
CREATE TRIGGER
warehouse_db=# insert into warehouse_tb1 (warehouse_id,warehouse_name,year_created,street_address,city,state,zip)values (4,'North point',2011,'Down Town','Carson','LA',4324);
INSERT 0 1
warehouse_db=# insert into warehouse_tb1 (warehouse_id,warehouse_name,year_created,street_address,city,state,zip)values (5,'South point',2012,'Down Town','Avalon','LA',4325);
INSERT 0 1
warehouse_db=# update warehouse_tb1 set city = 'arcadia' where warehouse_id = '4';
UPDATE 1
warehouse_db=# delete from warehouse_tb1 where warehouse_id =4;
DELETE 1
postgres=# select wlog_id,insertion_time from warehouse_audit ;
wlog_id | insertion_time
---------+-------------------------------
4 | 2015-11-10 16:56:25.728301+08
5 | 2015-11-10 16:56:40.24055+08
4 | 2015-11-10 16:56:54.938282+08
4 | 2015-11-10 16:57:06.432421+08
(4 rows)
create triggers on views
warehouse_db=# create table tab_view(emp_id int not null,emp_name varchar(10),emp_city varchar(10));
CREATE TABLE
warehouse_db=# insert into tab_view values (1,'Adam','Chicago');
INSERT 0 1
warehouse_db=# insert into tab_view values (2,'John','Miami');
INSERT 0 1
warehouse_db=# insert into tab_view values (3,'Smith','Dallas');
INSERT 0 1
warehouse_db=#
warehouse_db=# create view view_select as select * from tab_view;
CREATE VIEW
warehouse_db=# create function triggerfunc_on_view() returns trigger as $$
warehouse_db$# begin
warehouse_db$# if (TG_OP = 'insert') then
warehouse_db$# insert into tab_view values (new.emp_id,new.emp_name,new.emp_city);
warehouse_db$# return new;
warehouse_db$# end if;
warehouse_db$# return null;
warehouse_db$# end;
warehouse_db$# $$language plpgsql;
CREATE FUNCTION
warehouse_db=#
warehouse_db=# create trigger trigger_on_view
warehouse_db-# instead of insert on view_select
warehouse_db-# for each row
warehouse_db-# execute procedure triggerfunc_on_view();
CREATE TRIGGER
warehouse_db=# select * from view_select ;
-[ RECORD 1 ]-----
emp_id | 1
emp_name | Adam
emp_city | Chicago
-[ RECORD 2 ]-----
emp_id | 2
emp_name | John
emp_city | Miami
-[ RECORD 3 ]-----
emp_id | 3
emp_name | Smith
emp_city | Dallas
warehouse_db=# select * from pg_trigger ;
-[ RECORD 1 ]--+-----------------------------
tgrelid | 90268
tgname | RI_ConstraintTrigger_a_90297
tgfoid | 1654
tgtype | 9
tgenabled | O
tgisinternal | t
tgconstrrelid | 90287
tgconstrindid | 90274
tgconstraint | 90296
tgdeferrable | f
tginitdeferred | f
tgnargs | 0
tgattr |
tgargs | \x
tgqual |
-[ RECORD 2 ]--+-----------------------------
tgrelid | 90268
tgname | RI_ConstraintTrigger_a_90298
tgfoid | 1655
tgtype | 17
tgenabled | O
tgisinternal | t
tgconstrrelid | 90287
tgconstrindid | 90274
tgconstraint | 90296
tgdeferrable | f
tginitdeferred | f
tgnargs | 0
tgattr |
tgargs | \x
tgqual |
-[ RECORD 3 ]--+-----------------------------
tgrelid | 90287
tgname | RI_ConstraintTrigger_c_90299
tgfoid | 1644
tgtype | 5
tgenabled | O
tgisinternal | t
tgconstrrelid | 90268
tgconstrindid | 90274
tgconstraint | 90296
tgdeferrable | f
tginitdeferred | f
tgnargs | 0
tgattr |
tgargs | \x
tgqual |
-[ RECORD 4 ]--+-----------------------------
tgrelid | 90287
tgname | RI_ConstraintTrigger_c_90300
tgfoid | 1645
tgtype | 17
tgenabled | O
tgisinternal | t
tgconstrrelid | 90268
tgconstrindid | 90274
tgconstraint | 90296
tgdeferrable | f
tginitdeferred | f
tgnargs | 0
tgattr |
tgargs | \x
tgqual |
-[ RECORD 5 ]--+-----------------------------
tgrelid | 106664
tgname | audit_trigger
tgfoid | 106672
tgtype | 5
tgenabled | O
tgisinternal | f
tgconstrrelid | 0
tgconstrindid | 0
tgconstraint | 0
tgdeferrable | f
tginitdeferred | f
tgnargs | 0
tgattr |
tgargs | \x
tgqual |
-[ RECORD 6 ]--+-----------------------------
tgrelid | 106664
tgname | audit_all_ops_trigger
tgfoid | 106681
tgtype | 29
tgenabled | O
tgisinternal | f
tgconstrrelid | 0
tgconstrindid | 0
tgconstraint | 0
tgdeferrable | f
tginitdeferred | f
tgnargs | 0
tgattr |
tgargs | \x
tgqual |
-[ RECORD 7 ]--+-----------------------------
tgrelid | 106686
tgname | trigger_on_view
tgfoid | 106690
tgtype | 69
tgenabled | O
tgisinternal | f
tgconstrrelid | 0
tgconstrindid | 0
tgconstraint | 0
tgdeferrable | f
tginitdeferred | f
tgnargs | 0
tgattr |
tgargs | \x
tgqual |
warehouse_db=# select tgname from pg_trigger, pg_class where tgrelid=pg_class.oid and relname='warehouse_tb1';
-[ RECORD 1 ]------------------------
tgname | RI_ConstraintTrigger_a_90297
-[ RECORD 2 ]------------------------
tgname | RI_ConstraintTrigger_a_90298
-[ RECORD 3 ]------------------------
tgname | audit_trigger
-[ RECORD 4 ]------------------------
tgname | audit_all_ops_trigger
warehouse_db=# drop trigger audit_all_ops_trigger on warehouse_tb1;
DROP TRIGGER
warehouse_db=#
warehouse_db=# select tgname from pg_trigger, pg_class where tgrelid=pg_class.oid and relname='warehouse_tb1';
-[ RECORD 1 ]------------------------
tgname | RI_ConstraintTrigger_a_90297
-[ RECORD 2 ]------------------------
tgname | RI_ConstraintTrigger_a_90298
-[ RECORD 3 ]------------------------
tgname | audit_trigger
warehouse_db=# select current_user;
-[ RECORD 1 ]+---------
current_user | postgres
postgres=# select * from information_schema.triggers
postgres-# ;
trigger_catalog | trigger_schema | trigger_name | event_manipulation | event_object_catalog | event_object_schema | event_ob
ject_table | action_order | action_condition | action_statement | action_orientation | action_timing | act
ion_reference_old_table | action_reference_new_table | action_reference_old_row | action_reference_new_row | created
-----------------+----------------+-----------------------+--------------------+----------------------+---------------------+---------
-----------+--------------+------------------+----------------------------------------------+--------------------+---------------+----
------------------------+----------------------------+--------------------------+--------------------------+---------
postgres | public | audit_trigger | INSERT | postgres | public | warehous
e_tbl | | | EXECUTE PROCEDURE warehouse_audit_func() | ROW | AFTER |
| | | |
postgres | public | audit_all_ops_trigger | INSERT | postgres | public | warehous
e_tbl | | | EXECUTE PROCEDURE warehouse_audit_func_all() | ROW | AFTER |
| | | |
postgres | public | audit_all_ops_trigger | DELETE | postgres | public | warehous
e_tbl | | | EXECUTE PROCEDURE warehouse_audit_func_all() | ROW | AFTER |
| | | |
postgres | public | audit_all_ops_trigger | UPDATE | postgres | public | warehous
e_tbl | | | EXECUTE PROCEDURE warehouse_audit_func_all() | ROW | AFTER |
| | | |
(4 rows)
postgres=# select trigger_name from information_schema.triggers;
trigger_name
-----------------------
audit_trigger
audit_all_ops_trigger
audit_all_ops_trigger
audit_all_ops_trigger
(4 rows)
list the triggers you have created as follows:
postgres=# SELECT * FROM pg_trigger;
tgrelid | tgname | tgfoid | tgtype | tgenabled | tgisinternal | tgconstrrelid | tgconstrindid | tgconstraint | tgdefer
rable | tginitdeferred | tgnargs | tgattr | tgargs | tgqual
---------+-----------------------+--------+--------+-----------+--------------+---------------+---------------+--------------+--------
------+----------------+---------+--------+--------+--------
24576 | audit_all_ops_trigger | 24590 | 29 | O | f | 0 | 0 | 0 | f
| f | 0 | | \x |
24595 | trigger_on_view | 24599 | 69 | O | f | 0 | 0 | 0 | f
| f | 0 | | \x |
(2 rows)
see triggers associated with a particular table
postgres=# select tgname from pg_trigger ,pg_class where tgrelid=pg_class.oid and relname='warehouse_tbl';
tgname
-----------------------
audit_all_ops_trigger
(1 row)
postgres=# drop trigger audit_trigger on warehouse_tbl;
DROP TRIGGER
postgres=# drop trigger audit_all_ops_trigger on warehouse_tbl;
DROP TRIGGER
postgres=# drop trigger audit_all_ops_trigger on warehouse_tbl;
PostgreSQL trigger (function) examples的更多相关文章
- ORA-04091: table xxxx is mutating, trigger/function may not see it
今天同事让我看一个触发器为什么老是报错,当执行DML语句触发触发器后,会报ORA-04091错误:ORA-04091: table xxxx is mutating, trigger/function ...
- ORA-04091: table is mutating, trigger/function may not see it
今天在论坛里发现了一个关于ORA-04091的老帖子,收获良多,特此整理一下 关于ORA-04091: table is mutating, trigger/function may not see ...
- ORA-04091: table xxx is mutating, trigger/function may not see it
Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 Connected as tbcs SQL> SQL ...
- 【Azure 应用服务】部署Kafka Trigger Function到Azure Function服务中,解决自定义域名解析难题
问题描述 经过前两篇文章,分别使用VM搭建了Kafka服务,创建了Azure Function项目,并且都在本地运行成功. [Azure Developer]在Azure VM (Windows) 中 ...
- PostgreSQL function examples
warehouse_db=# CREATE TABLE warehouse_tbl(warehouse_id INTEGER NOT NULL,warehouse_name TEXT NOT NULL ...
- postgresql数据库3种程序(rule,trigger ,FUNCTION )
1. CREATE [ OR REPLACE ] RULE name AS ON event TO table_name [ WHERE condition ] DO [ ALSO | INSTEAD ...
- Postgresql存储过程调试:PostgreSQL 之 Function NOTICE
转载自http://zhenghaoju700.blog.163.com/blog/static/13585951820116782843994/ 先安装一个PostgreSQL(见补充知识) 比较O ...
- 错误"ORA-04091: table is mutating, trigger/function may not see it"的原因以及解决办法
错误的原因该错误是在编写trigger时常遇到的问题,其根本原因是由于对本表的操作造成的.对于使用了for each row 的触发器,做了DML操作(delete,update,insert),还没 ...
- PostgreSQL 之 CREATE FUNCTION
官方文档 语法: CREATE [ OR REPLACE ] FUNCTION name ( [ [ argmode ] [ argname ] argtype [ { DEFAULT | = } d ...
随机推荐
- Deep_learning
https://en.wikipedia.org/wiki/Deep_learning
- php://input,$_POST,$HTTP_RAW_POST_DATA区别
我们先来看两个demo 例子:php://input 代码如下 post.php 代码如下 例子:$_post 代码如下 welcome.php 代码如下 再来看$GLOBALS [& ...
- PHP 加密 和 解密 方法
关于Discuz的加密解密函数,相信大家都有所了解,该authcode函数可以说是对PHP界作出了重大的贡献,真的发觉discuz这个函数写的太精彩啦. 研究了一下这个算法,总的来说可以归纳为以下三点 ...
- 用GitLab搭建自己的私有GitHub
相信很多技术人员都知道有个github造福开发人员的git(分布式版本管理工具)代码管理社区,可以说现在git是开发人员的必备技能之一 本周有个朋友公司需要一个类似github的代码仓库管理系统,本人 ...
- Iphone H5上传照片被旋转
最近做项目发现在Iphone下,我们上传图片都会被翻转,最后查阅资料发现,的确是IOS的问题 不说过程,直接解决方法 iOS下,html方式使用<input type="file&qu ...
- HTML5 本地存储 LocalStorage
说到本地存储,这玩意真是历尽千辛万苦才走到HTML5这一步,之前的历史大概如下图所示: 最早的Cookies自然是大家都知道,问题主要就是太小,大概也就4KB的样子,而且IE6只支持每个域名20个co ...
- openCV中IplImage的使用
http://blog.csdn.net/welcome_xu/article/details/7650680 IplImage结构详细分析 IplImage 结构解读: typedef stru ...
- //四舍五入//得到倒序//比较字符串//拦截时间,实现超时锁屏//判断是否越狱//配置PodFile//Storyboard中跳转操作//处理不可逆的push界面操作
//处理不可逆的push界面操作 VerifyRealNameViewController *verifyRealNameCtrl = [VerifyRealNameViewController vi ...
- 低功耗蓝牙4.0BLE编程-nrf51822开发(2)
相关下载:http://download.csdn.net/detail/xgbing/9565708 首先看的示例是心率计一个示例程序:<KEIL path> \ARM\Device\N ...
- zepto源码--init--学习笔记
先展示init函数,由于笔记本屏幕太小,删掉了部分源码注释,才能在一屏内截图. 当我们调用$()的时候,便会直接调用zepto.init()生成zepto对象,跟jquery生成jquery对象类似. ...