pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andy Ballingall <andy@areyoulocal.co.uk>
To: '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 16:37:54 -0000
Message-ID: <ECOWS05MDz4WolWa4lU00023f54@smtp-out5.blueyonder.co.uk> (raw)
In-Reply-To: <20051120161715.GA2832@kaufbach.delug.de>

Hi Andreas,

The rule you've given only works for an update which changes the name.

If I do another update which changed the colour instead of the name, that
rule wouldn't do the right thing.

Instead, I'm looking for something which, with a single 'rule' (or whatever
the mechanism ends up being), intercepts *any* update on apples, and applies
the changes to the pears table instead, as if the only change that occurred
was a change of table name.

I can achieve this in the application which generates the sql commands, but
someone else suggested it was possible with rules, but it may not be the
case.

Thanks!
Andy




-----Original Message-----
From: pgsql-sql-owner@postgresql.org [mailto:pgsql-sql-owner@postgresql.org]
On Behalf Of Andreas Kretschmer
Sent: 20 November 2005 16:17
To: pgsql-sql@postgresql.org
Subject: Re: [SQL] Is it possible to redirect an update/insert/delete to a
different table?

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

> Hello Peter,
> 
> I'm glad it's possible, but I can't see how from the documentation.
> 
> Say if I have a table called 'apples' and a table called 'pears'.
> 
> What would the rule look like that would remap all updates on apples so
that
> they were applied to pears instead?

create rule apples_pears_update as on update to apples do instead update
pears set name= NEW.name where id=NEW.id ;

test=# select * from apples ;
 id | name
----+------
  1 | a
(1 row)

test=# select * from pears ;
 id | name
----+------
  1 | b
(1 row)

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


http://www.postgresql.org/docs/8.1/interactive/rules-update.html

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°

---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend



-- 
No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.1.362 / Virus Database: 267.13.4/176 - Release Date: 20/11/2005





view thread (12+ messages)  latest in thread

Message-ID: <ECOWS05MDz4WolWa4lU00023f54@smtp-out5.blueyonder.co.uk>
Permalink:  ../ECOWS05MDz4WolWa4lU00023f54@smtp-out5.blueyonder.co.uk/
Also on:    postgresql.org/message-id/ECOWS05MDz4WolWa4lU00023f54@smtp-out5.blueyonder.co.uk

 · 

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: andy@areyoulocal.co.uk, akretschmer@spamfence.net
  Subject: Re: Is it possible to redirect an update/insert/delete to a different table?
  In-Reply-To: <ECOWS05MDz4WolWa4lU00023f54@smtp-out5.blueyonder.co.uk>

* 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