Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Greetings monks,

Recently I have been asked to design a perl module (backed by a database) to provide access to a data, which is essentially of a tree structure type. For example, the data looks like:

Parent Node1/ Child Node 1/ Sub Child Node 1/ Leaf1
My database table has the following columns & data:
id | node_name | parent_id 1 | Parent Node1 | 0 2 | Child Node1 | 1 3 | Sub Child Node1 | 2 4 | Leaf1 | 3
where:

id is autogenerated primary key
parent_id is the "id" of the parent row
node_name is the name of the node.

Apart from the regular new(), node_name(), save() and delete() functions, I plan to provide a parent(), which would query the database and return the parent of the given node (as an object.)

The class will be used to list leaf/node(s), for a given node and also display some sort of heirarchial path (or bread crumb) to the given leaf (by appending the parent's node_name() till parent() returns null)

I seek your advice for:
1. is there an optimal way to design a database table to represent an tree structure ?
2. can I provide a better design to access the heirarchial path without incurring a performance penalty ?


In reply to Tree Structure and Db by InfiniteLoop

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (8)
    As of 2015-07-28 05:51 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (252 votes), past polls