sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
samikhan83's Avatar

Tree in mysql


Question posted by: samikhan83 (Newbie) on August 22nd, 2008 06:20 PM
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
4 Answers Posted
r035198x's Avatar
r035198x August 25th, 2008 07:16 AM
Administrator - 11,298 Posts
#2: 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
Atli August 25th, 2008 11:15 PM
Moderator - 2,760 Posts
#3: 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. );

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.
moltendorf's Avatar
moltendorf August 26th, 2008 12:15 AM
Newbie - 30 Posts
#4: 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
samikhan83 August 29th, 2008 10:23 PM
Newbie - 31 Posts
#5: Re: Tree in mysql

Thanx for ur help guyzz...I REALLY APPRECIATE
Reply
Not the answer you were looking for? Post your question . . .
197,037 members ready to help you find a solution.
Join Bytes.com

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 197,037 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top MySQL Contributors