Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 3FB5E476A6A for ; Thu, 19 Sep 2002 14:09:05 -0400 (EDT) Received: from mail.gmx.net (mail.gmx.net [213.165.64.20]) by postgresql.org (Postfix) with SMTP id 04515476417 for ; Thu, 19 Sep 2002 14:09:04 -0400 (EDT) Received: (qmail 13247 invoked by uid 0); 19 Sep 2002 18:09:03 -0000 Received: from chello062178186201.1.15.tuwien.teleweb.at (HELO beeblebrox) (62.178.186.201) by mail.gmx.net (mp007-rz3) with SMTP; 19 Sep 2002 18:09:03 -0000 Message-ID: <005501c26007$e2aa3bc0$4201a8c0@beeblebrox> From: "Michael Paesold" To: "PostgreSQL Server" , References: Subject: Re: Table Copy. Date: Thu, 19 Sep 2002 20:10:46 +0200 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2600.0000 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000 X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200209/224 X-Sequence-Number: 9501 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