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:
Child Node 1/
Sub Child Node 1/
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
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 ?
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
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:
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
- 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
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
| & || & |
| < || < |
| > || > |
| [ || [ |
| ] || ] ||