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 |