I want to create a database schema for a tree. Each record will have a parent except root-record.
tree_table
id | name
-------------
1 | tree1
2 | tree2
3 | tree3
The nodes in each tree will be given.
tree_node_table
id | name | tree_id | parent_id
---------------------------------
1 | a | 1 | NULL
2 | b | 1 | 1
3 | c | 1 | 2
4 | d | 1 | 2
5 | e | 1 | 4
6 | f | 2 | NULL
7 | g | 2 | 6
8 | h | 2 | 7
9 | i | 2 | 7
10 | j | 2 | 9
I feel it is not optimized one, anybody have better design?
UPDATE: the tree_id
is using to identify quickly the tree the record belongs.