pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
checking data integrity in a recursive table
2+ messages / 2 participants
[nested] [flat]

* checking data integrity in a recursive table
@ 2003-01-23 20:19  Luke Pascoe <luke.p@kmg.co.nz>
  0 siblings, 1 reply; 2+ messages in thread

From: Luke Pascoe @ 2003-01-23 20:19 UTC (permalink / raw)
  To: pgsql-sql

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





^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: checking data integrity in a recursive table
@ 2003-01-31 05:32  Bruce Momjian <pgman@candle.pha.pa.us>
  parent: Luke Pascoe <luke.p@kmg.co.nz>
  0 siblings, 0 replies; 2+ messages in thread

From: Bruce Momjian @ 2003-01-31 05:32 UTC (permalink / raw)
  To: Luke Pascoe <luke.p@kmg.co.nz>; +Cc: pgsql-sql

Luke Pascoe wrote:
> 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?

Good question.  I don't think you can do actualy SQL lookups in a CHECK.
I think you will need a trigger, either in pl/pgsql or in C using SPI to
issue the lookup queries.

You can have a CHECK clause that deals with multiple columns:

	CREATE TABLE friend2 (
	             firstname CHAR(15),
	             lastname  CHAR(20),
	             city      CHAR(15),
	             state     CHAR(2)      CHECK (length(trim(state)) = 2),
	             age       INTEGER      CHECK (age >= 0),
	             gender    CHAR(1)      CHECK (gender IN ('M','F')),
	             last_met  DATE         CHECK (last_met BETWEEN '1950-01-01'
	                                           AND CURRENT_DATE),
	             CHECK (upper(trim(firstname)) != 'ED' OR
	                    upper(trim(lastname)) != 'RIVERS')
	);

However, that doesn't help you because you can't reference a column in
another row of the same table.


-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073



^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2003-01-31 05:32 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2003-01-23 20:19 checking data integrity in a recursive table Luke Pascoe <luke.p@kmg.co.nz>
2003-01-31 05:32 ` Bruce Momjian <pgman@candle.pha.pa.us>

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