473,508 Members | 2,229 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

self referencing table structure and constraints

I have a categories table that contains a FK to another category in the
same table, creating a hierarchy. At the very top is this row:

category_id | name | description | parent_id
-------------+------+-------------------------+-----------
1 | ROOT | The top level category. | 0

There is no record with category_id 0 because ROOT is at the top of the
tree. I'd like to set up a constraint on this table so that every
category has to have a parent_id and it would be impossible to delete a
category if it had subcategories. The problem is that this root
category violates that constraint. Is there a way to setup the
constraint so that it constrains every record except for forcing the
root category to point at a real parent category?
I thought of pointing ROOT to itself, but since we have some
recursive code that starts at a given category id and moves up the tree
it will hit the ROOT category and loop forever. I'd like to fix this
by constraining the database so that even working from psql it would be
difficult to damage this table by hand.
Are there any widely used techniques for dealing with this type of
constraint?
Thanks,
-M@
---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 23 '05 #1
2 2416

On Thu, 23 Sep 2004, Matthew Hixson wrote:
I have a categories table that contains a FK to another category in the
same table, creating a hierarchy. At the very top is this row:

category_id | name | description | parent_id
-------------+------+-------------------------+-----------
1 | ROOT | The top level category. | 0
There is no record with category_id 0 because ROOT is at the top of the
tree. I'd like to set up a constraint on this table so that every
category has to have a parent_id and it would be impossible to delete a
category if it had subcategories. The problem is that this root
category violates that constraint. Is there a way to setup the
constraint so that it constrains every record except for forcing the
root category to point at a real parent category?
Well, to simply have the root category not error, you could use NULL for
the parent_id if you're using a foreign key. However, it sounds like your
full problem is more complicated.

If you want to force that there always exists exactly 1 such row, it's
harder. Forcing that there's no more than 1 should be possible without
writing triggers (maybe a unique index on ((1)) where parent_id is null)
but I'm not sure how else to guarantee that there's at least 1 besides a
trigger.
I thought of pointing ROOT to itself, but since we have some
recursive code that starts at a given category id and moves up the tree
it will hit the ROOT category and loop forever. I'd like to fix this
by constraining the database so that even working from psql it would be
difficult to damage this table by hand.


Well, in that case you also may need to watch out for cycles. You can do
this with triggers, but handling concurrent changes might get tricky.

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postgresql.org so that your
message can get through to the mailing list cleanly

Nov 23 '05 #2
On Sep 23, 2004, at 6:36 PM, Stephan Szabo wrote:

On Thu, 23 Sep 2004, Matthew Hixson wrote:
I have a categories table that contains a FK to another category in
the
same table, creating a hierarchy. At the very top is this row:

category_id | name | description | parent_id
-------------+------+-------------------------+-----------
1 | ROOT | The top level category. | 0


There is no record with category_id 0 because ROOT is at the top of
the
tree. I'd like to set up a constraint on this table so that every
category has to have a parent_id and it would be impossible to delete
a
category if it had subcategories. The problem is that this root
category violates that constraint. Is there a way to setup the
constraint so that it constrains every record except for forcing the
root category to point at a real parent category?


Well, to simply have the root category not error, you could use NULL
for
the parent_id if you're using a foreign key.


Okay, now I just feel silly. For some reason I was thinking that the
parent id couldn't be NULL either.
Thanks, this is exactly what I needed.
-M@
---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
11508
by: sreddy | last post by:
I am trying to write a sql query on self referencing table. Just to brief ..Database is related to a Hiring department of the Qwest company. I need to generate a Report used by in HR...
36
4613
by: toedipper | last post by:
Hello, I am designing a table of vehicle types, nothing special, just a list of unique vehicle types such as truck, lorry, bike, motor bike, plane, tractor etc etc For the table design I am...
7
9185
by: Andrew Mayo | last post by:
Here's a really weird one for any SQL Server gurus out there... We have observed (SQL Server 2000) scenarios where a stored procedure which (a) begins a transaction (b) inserts some rows into...
4
15797
by: maricel | last post by:
I have the following base table structure - DDL: CREATE TABLE "ADMINISTRATOR"."T1" ( "C1" INTEGER NOT NULL ) IN "TEST_TS" ; ALTER TABLE "ADMINISTRATOR"."T1" ADD PRIMARY KEY
2
3803
by: Paul Cook | last post by:
Hi, I have three tables: Countries: ID Country States: ID
10
6088
by: Macka | last post by:
A few pieces of information first: * I have a class called Folder which represents a row of data in a database table. The data access side of things is not an issue. * The table has a parent...
1
2473
by: Andrew Banks | last post by:
I have a database table with the following kind of structure CategoryID (int) Parent CategoryID (int) CategoryName (varchar) The table has a self referencing relationship between...
0
1286
by: Doug Gault | last post by:
I've been very pleased to find that you can load an XML file into a DATASET using the XMLREAD method, but I'm having a problem when trying to load a file that contains self-referencing elements. ...
1
1978
by: vojinle | last post by:
Hi, Is there any example populating TreeView control from self-referencing database table? Table structure: ID Item_name Parent_ID
0
7129
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7398
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7502
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5057
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3208
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3194
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1566
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
769
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
428
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.