Doubly Related Tables

Posts   
 
    
Posts: 112
Joined: 09-Aug-2004
# Posted on: 14-Jul-2005 23:49:19   

I have got two tables, Item and SubItem. Item is a Parent of SubItem. Any SubItem can children Items.


-------
Item 
-------
ItemId
Desc
----------


-----------
SubItem
-----------
SubItemId
ItemId
SubSubItemId
Desc
-----------


Where SubSubItem contains a key from the Item Table


So an Item can have multiple SubItems. A SubItem doesn't have to, but it can have one Item, which in turn can have multuple SubItems, and so on.

I hope that makes sense... If it does, then how do I prefetch this?

An example of how this is used

A hamburger can have an Item. SubItems could be Fries Or Salad. An Item to for Salad can be dressing. Sub Items for dressings can be Fat Free or Full Fat Or what ever other dressings may exist.

JimFoye avatar
JimFoye
User
Posts: 656
Joined: 22-Jun-2004
# Posted on: 15-Jul-2005 05:16:16   

This sounds like the classic example used to illustrate the decorator design pattern! Not the database design, I mean, but a menu item that can have an unknown number of condiments.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 15-Jul-2005 11:08:24   

I agree, this is a basic 'hierarchy in 1 table' approach, so every item is also a subitem, unless it doesn't have a parent. What the item really is is stored in another table related to the item (decorated)

You can use the 1-table approach, or use the 2 table balanced tree approach like discussed here: http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=3670 (scroll down to the bottom)

Frans Bouma | Lead developer LLBLGen Pro
Posts: 112
Joined: 09-Aug-2004
# Posted on: 15-Jul-2005 19:30:40   

Thanks alot, that worked great.