X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 5797753598 for ; Fri, 25 Mar 2005 19:46:29 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 70263-09 for ; Fri, 25 Mar 2005 19:46:28 +0000 (GMT) Received: from floppy.pyrenet.fr (news.pyrenet.fr [194.116.145.2]) by svr1.postgresql.org (Postfix) with ESMTP id D3FF95356D for ; Fri, 25 Mar 2005 19:46:27 +0000 (GMT) Received: by floppy.pyrenet.fr (Postfix, from userid 106) id 7D4A43095A; Fri, 25 Mar 2005 20:46:28 +0100 (MET) From: "Andrus Moor" X-Newsgroups: pgsql.sql Subject: Merging item codes using referential integrity Date: Fri, 25 Mar 2005 21:46:11 +0200 Organization: Hub.Org Networking Services Lines: 40 Message-ID: X-Complaints-To: usenet@news.hub.org X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.2180 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180 X-RFC2646: Format=Flowed; Original To: pgsql-sql@postgresql.org X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=1.34 tagged_above=0 required=5 tests=AWL, PRIORITY_NO_NAME, UPPERCASE_25_50 X-Spam-Level: * X-Archive-Number: 200503/346 X-Sequence-Number: 20825 I have item table and a lot of child tables where the items are used. I want to merge two item codes into single item in all tables. It is not nice to write a lot of separate UPDATE statements for each table. So I want to utilize REFERENCES clause for merging. I tried the following code but got duplicate key error in UPDATE statement. Any idea how to impement this? CREATE TABLE parent ( code CHAR(10) PRIMARY KEY ); CREATE TABLE orders ( anyfield CHAR(10) REFERENCES parent ON UPDATE CASCADE ); CREATE TABLE invoices ( anyotherfield CHAR(10) REFERENCES parent ON UPDATE CASCADE ); -- ... a lot of more child tables with different table and field names but -- always with same REFERENCES clause. INSERT INTO parent VALUES ('1'); INSERT INTO parent VALUES ('2'); INSERT INTO orders VALUES ('1'); INSERT INTO invoices VALUES ('1'); INSERT INTO orders VALUES ('2'); INSERT INTO invoices VALUES ('2'); BEGIN; -- Direct Postgres to update all child tables. This causes error. UPDATE parent SET code='1' WHERE code='2'; -- Remove duplicate row CREATE TABLE parent AS SELECT * FROM parent GROUP BY CODE ; COMMIT; Andrus.