pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Michael Paesold <mpaesold@gmx.at>
To: PostgreSQL Server <postgres@rsd.it>
To: pgsql-sql@postgresql.org
Subject: Re: Table Copy.
Date: Thu, 19 Sep 2002 20:10:46 +0200
Message-ID: <005501c26007$e2aa3bc0$4201a8c0@beeblebrox> (raw)
References: <Pine.LNX.4.44.0209191747250.31615-100000@sunrise2.radiostudiodelta.it>

Alex wrote:
> HI!
> 
> I'm new to postgres. I need to have a table as a copy of another one.
> 
> Example:
> 
> CREATE TABLE one (
>         fileda INTEGER,
>         filedb INTEGER,
>         filedc INTEGER );
> 
> CREATE TABLE two (
>         fileda INTEGER,
>         filedb INTEGER,
>         filedc INTEGER );
> 
> As on insert to table one I should get the same insert on table two.
> As on delete to table one I should get the same delete on table two.
> As on update to table one I should get the same update on table two.
> 
> Can someone provide the examples i can study ?

You could do it with RULEs. Here is an example how you would do the DELETE.

CREATE RULE copy_one_to_two AS
  ON DELETE TO one
  DO
    DELETE FROM two
    WHERE two.fileda=OLD.fileda
      AND two.filedb=OLD.filedb
      AND two.filedc=OLD.filedc;

You should change the where clause if you have a primary key on that table.
I am presuming fileda/filedb/filedc are unique in combination...
Read the section of the docs about RULEs for INSERT and UPDATE examples.

Regards,
Michael Paesold




view thread (4+ messages)  latest in thread

Message-ID: <005501c26007$e2aa3bc0$4201a8c0@beeblebrox>
Permalink:  ../005501c26007$e2aa3bc0$4201a8c0@beeblebrox/
Also on:    postgresql.org/message-id/005501c26007$e2aa3bc0$4201a8c0@beeblebrox

 · 

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: mpaesold@gmx.at, postgres@rsd.it
  Subject: Re: Table Copy.
  In-Reply-To: <005501c26007$e2aa3bc0$4201a8c0@beeblebrox>

* 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