pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andreas Kretschmer <akretschmer@spamfence.net>
To: pgsql-sql@postgresql.org
Subject: Re: Is it possible to redirect an update/insert/delete to a different table?
Date: Sun, 20 Nov 2005 17:42:34 +0100
Message-ID: <20051120164234.GB2832@kaufbach.delug.de> (raw)
In-Reply-To: <ECOWS02M7sInp0vS0DO0004c584@smtp-out2.blueyonder.co.uk>
References: <c2d9e70e0511200623w6e90d15bt64a03a2cfcd7132b@mail.gmail.com>
	<ECOWS02M7sInp0vS0DO0004c584@smtp-out2.blueyonder.co.uk>

Andy Ballingall <andy@areyoulocal.co.uk> schrieb:

> Hello Jaime,
> 
> I'm still not quite clear.
> 
> Say I have a number of different updates on a table 'apples' in my code,
> including:
> 
> UPDATE apples set pips=6 and color='yellow' where id=3;
> UPDATE apples set size=10 where id=6;
> 
> What would a rule look like which, when *any* update is attempted on the
> apples table, will instead apply the update to a different table - 'pears'.

Try it.

test=# create table apples (id int, name1 text, name2 text);
CREATE TABLE
test=# create table pears (id int, name1 text, name2 text);
CREATE TABLE
test=# create rule apples_pears_update as on update to apples do instead
update pears set name1= NEW.name1, name2=NEW.name2 where id=NEW.id ;
CREATE RULE
test=# insert into apples values (1, 'a', 'a');
INSERT 0 1
test=# insert into pears values (1, 'a', 'a');
INSERT 0 1
test=#
test=# update apples set name1='b' where id = 1;
UPDATE 1
test=# select * from pears ;
 id | name1 | name2
----+-------+-------
  1 | b     | a
(1 row)

test=# update apples set name2='c' where id = 1;
UPDATE 1
test=# select * from pears ;
 id | name1 | name2
----+-------+-------
  1 | a     | c
(1 row)

test=# update apples set name1='e', name2='e' where id = 1;
UPDATE 1
test=# select * from pears ;
 id | name1 | name2
----+-------+-------
  1 | e     | e
(1 row)



> 
> -----Original Message-----

Please, no top-posting.


HTH, Andreas
-- 
Really, I'm not out to destroy Microsoft. That will just be a completely
unintentional side effect. (Linus Torvalds)
Kaufbach, Saxony, Germany, Europe.              N 51.05082°, E 13.56889°



view thread (12+ messages)  latest in thread

Message-ID: <20051120164234.GB2832@kaufbach.delug.de>
Permalink:  ../20051120164234.GB2832@kaufbach.delug.de/
Also on:    postgresql.org/message-id/20051120164234.GB2832@kaufbach.delug.de

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: akretschmer@spamfence.net
  Subject: Re: Is it possible to redirect an update/insert/delete to a different table?
  In-Reply-To: <20051120164234.GB2832@kaufbach.delug.de>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox