home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / database / 8905 < prev    next >
Encoding:
Internet Message Format  |  1993-01-07  |  5.4 KB

  1. Path: sparky!uunet!nntp1.radiomail.net!fernwood!autodesk!tropic!tchild
  2. From: tchild@autodesk.com (Timothy Child)
  3. Newsgroups: comp.databases
  4. Subject: Re: Hierarchical Structures in RDBMS
  5. Message-ID: <18335@autodesk.COM>
  6. Date: 7 Jan 93 23:10:08 GMT
  7. References: <1993Jan6.094340.5870@devon.co.uk>
  8. Sender: news@Autodesk.COM
  9. Reply-To: tchild@autodesk.com
  10. Organization: Autodesk, Inc.
  11. Lines: 140
  12.  
  13. In article 5870@devon.co.uk, don@duncan (Don Radvan) writes:
  14. > I have an interesting problem. I need to represent hierarchical data  
  15. > structures within a relational db, but I need to do it in a way that  
  16. > permits the user to create his own hierarchy. That is, I do not know the  
  17. > structure of the hierarchy before hand. 
  18. > For example:
  19. >     A user wants to organize various objects. These objects will  
  20. > appear as leaf nodes in the user's own hierarchy. The user may choose many  
  21. > different ways of organizing these objects, indeed, s/he may have many  
  22. > simultaneous hierarchies of organization for each set of objects. What  
  23. > results is a sort of directory tree. Each directory is a category of  
  24. > objects and each "file" or non-directory entry in a directory is an  
  25. > object. Given the objects of apples and oranges, we may get the following:
  26. >                                   Stuff
  27. >                                     |
  28. >                     --------------------------------
  29. >                   Food                          Non-Food
  30. >                     |
  31. >             ---------------------- . . .
  32. >          Fruit        Veggies 
  33. >            |
  34. >      -------------
  35. >   Apples       Oranges
  36. > And so when s/he does a query such as the following:
  37. >     select name where Food = "Fruit"
  38. > or
  39. >     select name where Stuff = "Food" AND Food = "Fruit"
  40. > we would get Apples and Oranges.
  41.  
  42.  It's possible to model this problem with an RDBMS, but I don't know of
  43.  a STANDARD way to express the appropriate SQL to return the information
  44.  and I don't think there is one!  
  45.  
  46.  What you have in the hierarchy above is a specialized form of directed
  47.  acyclic graph (DAG).  The solution proposed below applies cyclic and
  48.  acyclic graphs so the system also must contain extra logic to assure the
  49.  appropriate specialized form of graph.
  50.  
  51.  A graph can be modeled in an RDBMS with two tables Nodes and Links.
  52.  Nodes are the entities of the hierarchy like: Stuff, Fruit, Food, etc,
  53.  . . .
  54.  
  55.  A node relation would be something like:
  56.  
  57.      Node id (prime key), node attributes, . . . 
  58.  
  59.  Links are the connections between Nodes and contain the keys of the parent
  60.  and sibling nodes. A link relation would be something like . . .
  61.  
  62.      Parent Node id, Sibling Node id, (together making the primary key), 
  63.         link attributes,  etc . . .
  64.  
  65.  In the above example the tables would look like, . . .
  66.  
  67.   Nodes              Links
  68.  
  69.  Stuff            Stuff, Food
  70.  Food            Stuff, Non_Food
  71.  Non_Food        Food, Fruit
  72.  Fruit            Food, Veggies
  73.  Veggies        Fruit, Apples
  74.  Apples            Fruit, Oranges
  75.  Oranges
  76.  
  77.  The query strategy is to start with  a given Node id and search
  78.  the Link table for all entires that contain that Node id as the
  79.  parent. The parents sibling node ids can be used to search the Links
  80.  table for their siblings. The search continues recursively until the
  81.  desired number of levels has been reached.
  82.  
  83.  Standard SQL queries have no way of dealing with this recursive
  84.  search. Some vendors provide work arounds with non standard
  85.  features.  Another solution is to have specific queries
  86.  for a specific number of levels.
  87.  
  88.  There is one piece of semantics this relational approach doesn't
  89.  capture and that is sub-entity synonyms. Because all entries in the
  90.  hierarchy are nodes they appear in the node table as unique names, no
  91.  duplicates are allowed.  It is possible to have a hierarchy with two
  92.  sub-entities with the same name that are different, my above solution
  93.  doesn't support this.
  94.  
  95.  For example consider the hierarchy ....
  96.  
  97.  
  98.             Stuff
  99.           |
  100.     -------------------
  101.     |           |
  102.       Food         Non-Food
  103.     |           |
  104.       -------         ---------
  105.       |     |          |          |
  106.    Fruit             Paint
  107.       |                  |
  108.     --------          -----------
  109.     |        |         |     |    |
  110.   Orange   Apple    Blue  Red Orange    
  111.  
  112.  
  113.   Orange is both a kind of fruit and a paint. To support this sort of
  114.   arrangement you need to introduce an extra key field to identify node
  115.   types.  Let's call it the type id. The Node and Link relations now
  116.   look like:
  117.  
  118.   Node Id, Type Id, (a compound primary key) ...
  119.  
  120.   Parent Node Id, Parent Type Id, Sibling Node Id, Sibling Type Id,
  121.      (all 4 fields are the primary key) . . .
  122.  
  123.   I would also recommend adding an additional table of Type Ids.
  124.   The type table would allow for referential integrity checks
  125.   on the type id.
  126.  
  127.   This solution only works if the nodes are of different types. It
  128.   wouldn't work if the hierarchy was a UNIX directory structure
  129.   for example.
  130.  
  131.   Another problem with this approach and the relation model is the
  132.   issue that a Node is a Node and only a Node. Therefore all nodes have
  133.   an identical number and kinds of attributes.  This problem can be
  134.   dealt with in a couple of ways, including extra tables which contain
  135.   specific attributes for each type. It is considered bad practice to
  136.   need to add new tables because of new subtype instances.
  137.  
  138.   In general graph like structures are one of the toughest things to
  139.   work with in the relational model.  A network or an object oriented
  140.   data base has a more natural affinity for solving this kind of
  141.   problem.
  142.  
  143.  -Tim Child
  144.  
  145.  Autodesk Inc. (Exercising an individual first amendment right.)
  146.  
  147.