Connecting Tech Pros Worldwide Forums | Help | Site Map

Tree in mysql

samikhan83's Avatar
Member
 
Join Date: Sep 2007
Posts: 33
#1: Aug 22 '08
hi...
i am designing the database for an inventory system while designing it forming tree like structure.... so i am unable to implement tree in database so can anyone help with this....

its like category->sub-category->sub-sub-category......

THANX IN ADAVANCE

Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#2: Aug 25 '08

re: Tree in mysql


Quote:

Originally Posted by samikhan83

hi...
i am designing the database for an inventory system while designing it forming tree like structure.... so i am unable to implement tree in database so can anyone help with this....

its like category->sub-category->sub-sub-category......

THANX IN ADAVANCE

Just have the sub-sub-categorytable keep a sub-category_ID and the sub-category table have a category_ID ...?
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,748
#3: Aug 25 '08

re: Tree in mysql


Hi.

Creating a tree structure isn't difficult. Consider this:
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE tree(
  2.     ID int Primary Key AUTO_INCREMENT, 
  3.     ParentID int References tree(ID)
  4. );
  5.  
It simply references itself, the first record having a NULL value for a parent and each new record having a previous record as it's parent, therefore creating a tree structure.

The challenging part is getting your API to read this properly.
Member
 
Join Date: Jul 2007
Location: San Ramon, California
Posts: 65
#4: Aug 26 '08

re: Tree in mysql


Personally, I have made various trees, and decided to use two tables to effectively make the trees readable at a fast pace.

The stream table contained 3 columns:
stream_identifier, stream_branch_identifier, and stream_sort

The branch table contained 3 or more columns:
branch_identifier, branch_parent

Each stream contained a sorted list of which branches to follow to get to that one little result.

To then pull out a list of branches to follow to get to that one result I wanted, I'd run a fairly simple SELECT statement:
Expand|Select|Wrap|Line Numbers
  1. SELECT *
  2.     FROM `streams` `stream`
  3.         LEFT JOIN `branches` `branch`
  4.             ON `stream`.`stream_branch_identifier` = `branch`.`branch_identifier`
  5.     ORDER BY
  6.         `stream`.`stream_identifier` ASC,
  7.         `stream`.`stream_sort` ASC;
Table branches:
Expand|Select|Wrap|Line Numbers
  1. branch_identifier branch_parent
  2. -------------------------------
  3. 1                 0
  4. 2                 1
  5. 3                 2
  6. 4                 0
  7. 5                 4
  8. 6                 5
Table streams:
Expand|Select|Wrap|Line Numbers
  1. stream_identifier stream_branch_identifier stream_sort
  2. ------------------------------------------------------
  3. 1                 1                        2
  4. 1                 2                        1
  5. 1                 3                        0
  6. 2                 2                        1
  7. 2                 3                        0
  8. 3                 3                        0
  9. 4                 4                        2
  10. 4                 5                        1
  11. 4                 6                        0
  12. 5                 5                        1
  13. 5                 6                        0
  14. 6                 6                        0
If I just wanted to get to the branch with the 1 for its identifier, I could add a WHERE clause, and get a result table as shown below.
Expand|Select|Wrap|Line Numbers
  1. SELECT *
  2.     FROM `streams` `stream`
  3.         LEFT JOIN `branches` `branch`
  4.             ON `stream`.`stream_branch_identifier` = `branch`.`branch_identifier`
  5.     WHERE `stream`.`stream_identifier` = '1'
  6.     ORDER BY
  7.         `stream`.`stream_identifier` ASC,
  8.         `stream`.`stream_sort` ASC;
Result:
Expand|Select|Wrap|Line Numbers
  1. stream_identifier stream_branch_identifier stream_sort branch_identifier branch_parent
  2. --------------------------------------------------------------------------------------
  3. 1                 3                        0           3                 2
  4. 1                 2                        1           2                 1
  5. 1                 1                        2           1                 0
There may be a different way you want to go about this, but the question you're asking is the exact one I asked myself yesterday when I wanted to get a set of modules out of a table, and a way to get to modules within sub directories.
samikhan83's Avatar
Member
 
Join Date: Sep 2007
Posts: 33
#5: Aug 29 '08

re: Tree in mysql


Thanx for ur help guyzz...I REALLY APPRECIATE
Reply