pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Luke Pascoe <luke.p@kmg.co.nz>
To: pgsql-sql@postgresql.org
Subject: checking data integrity in a recursive table
Date: Fri, 24 Jan 2003 09:19:51 +1300
Message-ID: <019801c2c31c$c8d63fa0$3200000a@K2> (raw)

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
========================================





view thread (2+ messages)  latest in thread

Message-ID: <019801c2c31c$c8d63fa0$3200000a@K2>
Permalink:  ../019801c2c31c$c8d63fa0$3200000a@K2/
Also on:    postgresql.org/message-id/019801c2c31c$c8d63fa0$3200000a@K2

 · 

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: luke.p@kmg.co.nz
  Subject: Re: checking data integrity in a recursive table
  In-Reply-To: <019801c2c31c$c8d63fa0$3200000a@K2>

* 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