Tree in mysql

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • samikhan83
    New Member
    • Sep 2007
    • 33

    Tree in mysql

    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
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    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 ...?

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      Hi.

      Creating a tree structure isn't difficult. Consider this:
      [code=php]
      CREATE TABLE tree(
      ID int Primary Key AUTO_INCREMENT,
      ParentID int References tree(ID)
      );
      [/code]
      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.

      Comment

      • moltendorf
        New Member
        • Jul 2007
        • 65

        #4
        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_identifi er, stream_branch_i dentifier, and stream_sort

        The branch table contained 3 or more columns:
        branch_identifi er, 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:
        Code:
        SELECT *
        	FROM `streams` `stream`
        		LEFT JOIN `branches` `branch`
        			ON `stream`.`stream_branch_identifier` = `branch`.`branch_identifier`
        	ORDER BY
        		`stream`.`stream_identifier` ASC,
        		`stream`.`stream_sort` ASC;
        Table branches:
        Code:
        branch_identifier branch_parent
        -------------------------------
        1                 0
        2                 1
        3                 2
        4                 0
        5                 4
        6                 5
        Table streams:
        Code:
        stream_identifier stream_branch_identifier stream_sort
        ------------------------------------------------------
        1                 1                        2
        1                 2                        1
        1                 3                        0
        2                 2                        1
        2                 3                        0
        3                 3                        0
        4                 4                        2
        4                 5                        1
        4                 6                        0
        5                 5                        1
        5                 6                        0
        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.
        Code:
        SELECT *
        	FROM `streams` `stream`
        		LEFT JOIN `branches` `branch`
        			ON `stream`.`stream_branch_identifier` = `branch`.`branch_identifier`
        	WHERE `stream`.`stream_identifier` = '1'
        	ORDER BY
        		`stream`.`stream_identifier` ASC,
        		`stream`.`stream_sort` ASC;
        Result:
        Code:
        stream_identifier stream_branch_identifier stream_sort branch_identifier branch_parent
        --------------------------------------------------------------------------------------
        1                 3                        0           3                 2
        1                 2                        1           2                 1
        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.

        Comment

        • samikhan83
          New Member
          • Sep 2007
          • 33

          #5
          Thanx for ur help guyzz...I REALLY APPRECIATE

          Comment

          Working...