Design of tree-shaped structure table

Source: Internet
Author: User

Tree structure in our daily development often used, many functions in order to show the hierarchical relationship is often used to store tree structure, for the tree structure datasheet design, there are a variety of options for us to choose from, next I will introduce the tree structure table design of some basic programs. In the organizational structure of the Department table (department) as an example of the general design. A table is done, the parent ID can be recorded, as follows:
CREATE TABLE ' Department ' (
' DepartmentID ' smallint (5) unsigned not NULL auto_increment,//departmental water ID
' title ' varchar not NULL default ',//department Name
' ParentID ' smallint (5) unsigned not NULL default ' 0 ',//departmental Superior ID
path ' varchar not NULL default ',//departmental path ID
' Displayorder ' smallint (6) unsigned not NULL default ' 1000 ',/Department display order
PRIMARY KEY (' DepartmentID '),
KEY ' ParentID ' (' ParentID ')
) Engine=myisam DEFAULT Charset=utf8;
Data table record data are as follows:

Department ID Department Name Parent ID Path Display Order
1 Technology Development Department 0 1
2 Development Group 1 1 2
3 Develop Group A 2 1,2 3

Based on the left and right value encoding.
CREATE TABLE ' Department ' (
' DepartmentID ' smallint (5) unsigned not NULL auto_increment,//departmental water ID
' title ' varchar not NULL default ',//department Name
' ParentID ' smallint (5) unsigned not NULL default ' 0 ',//departmental Superior ID
' LfT ' smallint (6) unsigned not NULL default ' 0 ',//left value
' Rght ' smallint (6) unsigned not NULL default ' 0 ',//Right value
PRIMARY KEY (' DepartmentID '),
KEY ' ParentID ' (' ParentID ')
) Engine=myisam DEFAULT Charset=utf8;
Data table record data are as follows:
Department ID Department Name Parent ID left value Right Value
1 Technology Development Department 0 1 6
2 Development Group 1 2 5
3 Develop Group A 2 3 4
Node table + Relational table
CREATE TABLE ' Department ' (
' DepartmentID ' smallint (5) unsigned not NULL auto_increment,//departmental water ID
' title ' varchar not NULL default ',//department Name
' Displayorder ' smallint (6) unsigned not NULL default ' 1000 ',/Department display order
PRIMARY KEY (' DepartmentID ')
) Engine=myisam DEFAULT Charset=utf8;

CREATE TABLE ' departmentrelation ' (
' ParentID ' smallint (5) unsigned not NULL default ' 0 ',//node Parent ID
' childID ' smallint (5) unsigned not NULL default ' 0 ',//Node ID
' Level ' smallint (3) unsigned not NULL default ' 0 ',//node depth
PRIMARY KEY (' childID ', ' ParentID ')
) Engine=myisam DEFAULT Charset=utf8;
Data table record data are as follows:
Departmental Table (department)
Department ID
Department Name
Display order
1 Technology Development Department 1
2 Development Group 2
3 Develop Group A 3
Relational table (departmentrelation)
Parent ID Child ID Node Depth
1 1 0
1 2 1
2 2 1
1 3 2
2 3 2
3 3 2




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.