插入触发器以使用PostgreSQL更新另一个表 [英] Insert trigger to Update another table using PostgreSQL
本文介绍了插入触发器以使用PostgreSQL更新另一个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个名为奖项的表。如何在PostgreSQL中安装触发器,表中的每个插入都会更新一个不同的表?
I have a table named awards. How can I mount a Trigger in PostgreSQL where each insert in the table awards updates a different table?
推荐答案
这里有两个表分别命名为 table1
和 table2
。使用触发器,我将在插入 table1
时更新 table2
。
Here we have two tables named table1
and table2
. Using a trigger I'll update table2
on insertion into table1
.
创建表
CREATE TABLE table1
(
id integer NOT NULL,
name character varying,
CONSTRAINT table1_pkey PRIMARY KEY (id)
)
CREATE TABLE table2
(
id integer NOT NULL,
name character varying
)
触发器函数
CREATE OR REPLACE FUNCTION function_copy() RETURNS TRIGGER AS
$BODY$
BEGIN
INSERT INTO
table2(id,name)
VALUES(new.id,new.name);
RETURN new;
END;
$BODY$
language plpgsql;
触发器
CREATE TRIGGER trig_copy
AFTER INSERT ON table1
FOR EACH ROW
EXECUTE PROCEDURE function_copy();
这篇关于插入触发器以使用PostgreSQL更新另一个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文