From: Philip Lijnzaad <lijnzaad@ebi.ac.uk>
Newsgroups: comp.databases.theory
Subject: Re: Creating a Hierarchical Structure
Date: 08 Aug 2001 18:40:59 +0100
Organization: EBI
Lines: 45
Sender: lijnzaad@sol6.ebi.ac.uk
Message-ID: <u7hevilaac.fsf@sol6.ebi.ac.uk>
References: <p4ec7.21643$Ug.5815984@typhoon.we.rr.com>
NNTP-Posting-Host: sol6.ebi.ac.uk
X-Trace: niobium.hgmp.mrc.ac.uk 997292464 24132 193.62.199.128 (8 Aug 2001 17:41:04 GMT)
X-Complaints-To: news@net.bio.net
NNTP-Posting-Date: Wed, 8 Aug 2001 17:41:04 +0000 (UTC)
X-Newsreader: Gnus v5.7/Emacs 20.5




> How would you, or what is the commonly accepted way, of creating a database
> which represents a hierarchical category structure ala Yahoo!?

The commonly accepted way is excatly the one you outline. The drawback is you
can't pull out complete subtrees with one statement (unless you have
something like Oracle's CONNECT BY PRIOR). Joe Celko regularly posts a
different solution to your problem; I think i saw his post this week! Search
groups.google.com for "celko hierarchy".

Note that not everybody agrees with Celko's claim that the 'common way' is not
normalized. The problem with is approach is that it's difficult to select the
direct parents or direct children of a given node. In practice, you might be
best off with having both LFT and RGT and PARENT_ID, giving you the best of
both worlds, at the expense of denormalisation.

> What I have done until now is to create a table called categories. Each
> field has a categoryID, a parentID and the category's name. The parentID is
> "0" for all top level categories, and all sub-categories inherit a parentID
> containing the categoryID of the category above it. So for instance:
 
> CategoryID     ParentID      Name
>        1                     0          Shopping
>        2                     1          Retail
>        3                     1          Business to Business
>        4                     2          Clothing


> I was talking to some guy yesterday who said the standard what of creating
> such a system in a directory was to have a table which contains your root
> categories and then a new table for every level of sub-category. 

This sounds positively insane to me, in the general case. For very fixed
hierarchies it may work though (although I don't see how you would portably
determine which table to select from/join to during a query). 

                                                                      Philip
-- 
Real programs don't eat cache (Malay)
-----------------------------------------------------------------------------
Philip Lijnzaad, lijnzaad@ebi.ac.uk \ European Bioinformatics Institute,rm A2-08
+44 (0)1223 49 4639                 / Wellcome Trust Genome Campus, Hinxton
+44 (0)1223 49 4468 (fax)           \ Cambridgeshire CB10 1SD,  GREAT BRITAIN


