pgsql触发器:当向一张表中插入或更新一条记录时,同时向另一张表也插入或更新一条记录

2025-01-06 08:15:47
推荐回答(1个)
回答1:

digoal=# create table a (aid int primary key, aname text, time timestamp);
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "a_pkey" for table "a"
CREATE TABLE
digoal=# create table b (id int primary key, name text, time timestamp);
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "b_pkey" for table "b"
CREATE TABLE
digoal=# create or replace function tg_a () returns trigger as $$
declare
begin
case TG_OP
when 'INSERT' then
insert into b(id,name,time) values (NEW.aid, NEW.aname, NEW.time);
when 'UPDATE' then
update b set id=NEW.aid, name=NEW.aname, time=NEW.time where id=OLD.aid;
when 'DELETE' then
delete from b where id=OLD.aid;
when 'TRUNCATE' then
truncate b;
else
return NULL;
end case;
return NULL;
end;
$$ language plpgsql;
digoal=# create trigger tg_a after INSERT OR DELETE OR UPDATE ON a for each row execute procedure tg_a();
CREATE TRIGGER
digoal=# create trigger tg_a_truncate after truncate ON a for each statement execute procedure tg_a();
CREATE TRIGGER
digoal=# insert into a select generate_series(1,10),'digoal',clock_timestamp();
INSERT 0 10
digoal=# select * from a;
aid | aname | time
-----+--------+----------------------------
1 | digoal | 2013-02-03 18:59:37.592479
2 | digoal | 2013-02-03 18:59:37.592667
3 | digoal | 2013-02-03 18:59:37.592674
4 | digoal | 2013-02-03 18:59:37.592677
5 | digoal | 2013-02-03 18:59:37.59268
6 | digoal | 2013-02-03 18:59:37.592683
7 | digoal | 2013-02-03 18:59:37.592686
8 | digoal | 2013-02-03 18:59:37.59269
9 | digoal | 2013-02-03 18:59:37.592693
10 | digoal | 2013-02-03 18:59:37.592696
(10 rows)
digoal=# select * from b;
id | name | time
----+--------+----------------------------
1 | digoal | 2013-02-03 18:59:37.592479
2 | digoal | 2013-02-03 18:59:37.592667
3 | digoal | 2013-02-03 18:59:37.592674
4 | digoal | 2013-02-03 18:59:37.592677
5 | digoal | 2013-02-03 18:59:37.59268
6 | digoal | 2013-02-03 18:59:37.592683
7 | digoal | 2013-02-03 18:59:37.592686
8 | digoal | 2013-02-03 18:59:37.59269
9 | digoal | 2013-02-03 18:59:37.592693
10 | digoal | 2013-02-03 18:59:37.592696
(10 rows)
digoal=# delete from a where aid=1;
DELETE 1
digoal=# select * from b;
id | name | time
----+--------+----------------------------
2 | digoal | 2013-02-03 18:59:37.592667
3 | digoal | 2013-02-03 18:59:37.592674
4 | digoal | 2013-02-03 18:59:37.592677
5 | digoal | 2013-02-03 18:59:37.59268
6 | digoal | 2013-02-03 18:59:37.592683
7 | digoal | 2013-02-03 18:59:37.592686
8 | digoal | 2013-02-03 18:59:37.59269
9 | digoal | 2013-02-03 18:59:37.592693
10 | digoal | 2013-02-03 18:59:37.592696
(9 rows)
digoal=# select * from a;
aid | aname | time
-----+--------+----------------------------
2 | digoal | 2013-02-03 18:59:37.592667
3 | digoal | 2013-02-03 18:59:37.592674
4 | digoal | 2013-02-03 18:59:37.592677
5 | digoal | 2013-02-03 18:59:37.59268
6 | digoal | 2013-02-03 18:59:37.592683
7 | digoal | 2013-02-03 18:59:37.592686
8 | digoal | 2013-02-03 18:59:37.59269
9 | digoal | 2013-02-03 18:59:37.592693
10 | digoal | 2013-02-03 18:59:37.592696
(9 rows)

digoal=# update a set aname='new' where aid=2;
UPDATE 1
digoal=# select * from a where aid=2;
aid | aname | time
-----+-------+----------------------------
2 | new | 2013-02-03 18:59:37.592667
(1 row)
digoal=# select * from b where id=2;
id | name | time
----+------+----------------------------
2 | new | 2013-02-03 18:59:37.592667
(1 row)
digoal=# truncate a;
TRUNCATE TABLE
digoal=# select * from a;
aid | aname | time
-----+-------+------
(0 rows)
digoal=# select * from b;
id | name | time
----+------+------
(0 rows)