in reply to database table advice

I know, it doesn't handle the case of infinately deep categories, but in some cases, using a form of notation may be the better alternative, partcularly if you're using faceted classification in a poly-heirarchy.

What we do in that case, is we assign each facet a letter, and we make sure that we emit the facets in a consistent order.

So, using your example:

A : habitat
  A1 : land animals
  A2 : aquatic animals
B : animal size
  B1 : small animal
  B2 : large animal
C : number of legs
  C0 : no legs
  C2 : 2 legs
  C4 : 4 legs
D : backbone?
  D1 : vertebrate
  D2 : invertebrate
    D2.1 : invertebrate w/ exoskeleton
    D2.2 : invertebrate w/out exoskeleton
E : diet
  E1 : carnivore
  E2 : herbavore

So, for instance, a dog might be (A1;B2;C4;D1;E1). Parents to that would be any of : (A1;B2;C4;D1) (A1;B2;C4;E1) (A1;B2;D1;E1) (A1;C4;D1;E1) or (B2;C4;D1;E1). A worm might be (A1;B2;C0;D2.2) (no E, as I don't think diet is significant in classifying them, but i could be wrong) Parents would be : (A1;B2;C0;D2) (A1;B2;D2.2) (A1;C0;D2.2) or (B2;C0;D2.2).

Now, to search this, we have to use SQL's 'LIKE' command, and we need to make sure that the notation is structured in a way to allow us to unambiguously specify a category. The easiest way is to end each classification within a facet with a dot (to make sure we're not matching 'A11' when we ask for 'A1'), and to prepend each facet in some delimiter (so we don't get facet AA when we want A):

  dog :  ;A1.;B2.;C4.;D1.;E1.
  worm:  ;A1.;B2.;C0.;D2.2.

If we want all land animals : category  LIKE '%;A1.%'

If we want all land animals with four legs: category  LIKE '%;A1.%;C4.%'

...

So -- the problems? Really hard to re-arrange after the fact. Benefits? Any depth or number of facets, provided you've allocated enough space for the notation ... of course, the notation can get rather large in complex systems, and it much less efficient to use than the other methods described above.