Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 983AE477828 for ; Thu, 23 Jan 2003 17:04:41 -0500 (EST) Received: from cook.itsd.co.nz (visp5-179.visp.co.nz [210.54.179.5]) by postgresql.org (Postfix) with ESMTP id 2F5AA477855 for ; Thu, 23 Jan 2003 15:19:25 -0500 (EST) Received: from K2 (k2.md5summer.org [10.0.0.50]) by cook.itsd.co.nz (8.12.3/8.12.3/Debian -4) with SMTP id h0NKJN7f018726 for ; Fri, 24 Jan 2003 09:19:24 +1300 Message-ID: <019801c2c31c$c8d63fa0$3200000a@K2> From: "Luke Pascoe" To: Subject: checking data integrity in a recursive table Date: Fri, 24 Jan 2003 09:19:51 +1300 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.2800.1106 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106 X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200301/458 X-Sequence-Number: 11323 Hi, I have a table that references itself to create a tree-like structure, eg: CREATE TABLE tree ( id SERIAL NOT NULL, name VARCHAR(255) NOT NULL, parent INT NULL, customer IN NOT NULL, CONSTRAINT parent_key... CONSTRAINT customer_fk FOREIGN KEY (customer) REFERENCES customer ); ALTER TABLE tree ADD CONSTRAINT FOREIGN KEY (parent) REFERENCES tree; As you can see tree also references the customer table. What I need is a CHECK that will ensuer that any given "tree" row has the same customer as its parent. Remember that "parent" can also be NULL. Or would this be better done as a trigger? TIA! ======================================== Luke Pascoe KMG (NZ) Limited. http://www.kmg.co.nz Mobile: (021) 303019 Email: luke.p@kmg.co.nz ========================================