Re: [GENERAL] Self-referential records

2010-01-24 Thread Leif Biberg Kristensen
On Sunday 24. January 2010 16.22.00 Wayne E. Pfeffer wrote: > If you do not use null to represent a root node, when you go to unwind the > data from the table to generate a hierarchy tree, you could end up with an > infinite loop. The query will always be looking for the next parent in the > hierar

Re: [GENERAL] Self-referential records

2010-01-24 Thread Wayne E. Pfeffer
If you do not use null to represent a root node, when you go to unwind the data from the table to generate a hierarchy tree, you could end up with an infinite loop. The query will always be looking for the next parent in the hierarchy. Meaning, you will want to find the parent of a node using the g

Re: [GENERAL] Self-referential records

2010-01-24 Thread Andreas Kretschmer
Xi Shen wrote: > what if you insert other values like '1', '999'? will the insertion > successful? if so, what's the difference between a deferred reference > and no reference at all? Nice question ;-) Okay, recreate the table but without NOT NULL: test=# CREATE TABLE refers ( id SERIAL PRIMA

Re: [GENERAL] Self-referential records

2010-01-24 Thread Peter Geoghegan
What is the preferred way to enforce that there is at least one orphan record if any at all, and that a record is not a Marty McFly type descendent of itself? I would suggest that a statement level after trigger is the way to go, but I myself have never actually had to enforce this. Regards, Peter

Re: [GENERAL] Self-referential records

2010-01-24 Thread Xi Shen
On Sun, Jan 24, 2010 at 10:36 PM, Andreas Kretschmer wrote: > Ovid wrote: > >> Assuming I have the following table: >> >>     CREATE TABLE refers ( >>       id        SERIAL  PRIMARY KEY, >>       name      VARCHAR(255) NOT NULL, >>       parent_id INTEGER NOT NULL, >>       FOREIGN KEY (parent_i

Re: [GENERAL] Self-referential records

2010-01-24 Thread Andreas Kretschmer
Xi Shen wrote: > > To handle that you can set the constzraint deferrable, initially > > deferred: > > > > test=# CREATE TABLE refers ( id SERIAL  PRIMARY KEY, name VARCHAR(255) NOT > > NULL, parent_id INTEGER NOT NULL, FOREIGN KEY (parent_id) REFERENCES > > refers(id) deferrable initially defer

Re: [GENERAL] Self-referential records

2010-01-24 Thread Andreas Kretschmer
Ovid wrote: > Assuming I have the following table: > > CREATE TABLE refers ( > idSERIAL PRIMARY KEY, > name VARCHAR(255) NOT NULL, > parent_id INTEGER NOT NULL, > FOREIGN KEY (parent_id) REFERENCES refers(id) > ); > I need to insert two records so tha

Re: [GENERAL] Self-referential records

2010-01-24 Thread Thomas Kellerer
Ovid wrote on 24.01.2010 14:43: Assuming I have the following table: CREATE TABLE refers ( idSERIAL PRIMARY KEY, name VARCHAR(255) NOT NULL, parent_id INTEGER NOT NULL, FOREIGN KEY (parent_id) REFERENCES refers(id) ); I need to insert two records

Re: [GENERAL] Self-referential records

2010-01-24 Thread Bill Moran
On 1/24/10 8:43 AM, Ovid wrote: Assuming I have the following table: CREATE TABLE refers ( idSERIAL PRIMARY KEY, name VARCHAR(255) NOT NULL, parent_id INTEGER NOT NULL, FOREIGN KEY (parent_id) REFERENCES refers(id) ); I need to insert two records

Re: [GENERAL] Self-referential records

2010-01-24 Thread Leif Biberg Kristensen
On Sunday 24. January 2010 14.43.10 Ovid wrote: > Assuming I have the following table: > > CREATE TABLE refers ( > idSERIAL PRIMARY KEY, > name VARCHAR(255) NOT NULL, > parent_id INTEGER NOT NULL, > FOREIGN KEY (parent_id) REFERENCES refers(id) > ); > I

[GENERAL] Self-referential records

2010-01-24 Thread Ovid
Assuming I have the following table: CREATE TABLE refers ( idSERIAL PRIMARY KEY, name VARCHAR(255) NOT NULL, parent_id INTEGER NOT NULL, FOREIGN KEY (parent_id) REFERENCES refers(id) ); I need to insert two records so that "select * from refers" looks l