473,385 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Tree in mysql

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
Aug 22 '08 #1
4 2766
r035198x
13,262 8TB
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 ...?
Aug 25 '08 #2
Atli
5,058 Expert 4TB
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.
Aug 25 '08 #3
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.
Aug 25 '08 #4
Thanx for ur help guyzz...I REALLY APPRECIATE
Aug 29 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Marek Lewczuk | last post by:
I'm trying to build mysql from the development sources of mysql 4.1., retreived using bitkeeper under win2k using cygwin. I have made "bk -r edit", and now I'm having a problem running aclocal:...
12
by: pillepop2003 | last post by:
Hey! Can anyone give me a hint, how this problem is best implemented: I have a table of users (see below), where every user has one "superior user" (= parent node), this should be a fully...
1
by: Srihari | last post by:
I'm trying to develop a tree structure using javascript. The node values of the tree are generating from a mysql table depending on login. The tree structure contains 3 sub levels. I developed...
1
by: Satish.Talyan | last post by:
hi, i want to create a dynamic tree hierarchy in javascript.there are two parts in tree, group & user.when we click on group then users come under that's tree category will be opened.problem is...
13
by: hornedw | last post by:
I have been working on a ecommerce website for myself. What I needed some assistance on was when i was trying to display the categories/subcategories for the different products. I decided to use...
1
by: yogendramishra | last post by:
Hello friends, i am creating a product catelog tree with multilevel hierarchi.I am using tree class and inside that i want to show the category tree. But it is showing errors to me. I have also...
8
by: ashore | last post by:
Folks, any url's around re subject matter? Performance isn't necessarily a problem, and the depth will be under, say, twenty. Thanks, all. --AS
7
by: sosamv | last post by:
Hi all!, I'm creating a app with PHP and MySQL, the system administrator is capable of creating profiles, on each profile we create, we add a custom access menu (a javascript tree view menu), theres...
6
by: Gordon | last post by:
I've developed a CMS that manages content across several sites. The content is represented in a tree structure maintained in a database, where each item has an id and a parent that points to the...
1
by: Joe Chiang | last post by:
I've got a AJAX Tree Node working with MySQL database. What it does is, Expanding Nodes until there are no Children Nodes in the Database. Now, I am trying to Write a Java Script to Create a...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.