davidvnog
davidvnog

Reputation: 694

Database structure for multi-level category (Best Approach)

The approach this user made on this question for the database structure works fine. However, thinking in a relational database approach, what is the best approach for a multi-level category database?

Upvotes: 3

Views: 5218

Answers (1)

davidvnog
davidvnog

Reputation: 694

VERY SIMPLE AND EFFECTIVE

We simply do a recursive association.

id       title        content       parent_id
==============================================
1        item1         NULL         NULL     
2        item2         NULL         1          
3        item3         abcd         2       
4        item4         efgh         2        

In this example:

item1 is the first level (see that the parent_id is NULL).

item2 is a second level option from item1 (see that the parent_id is 1 and the content is NULL making this another drill down option only).

item3 and item4 are content from item2 (see that parent_id is 2 and the content is NOT null).

Upvotes: 10

Related Questions