I’m working on a database design that allows for a product to be in 1 or more categories. A category can be in a parent-child relationship. Here’s what I have so far:
==================
product
==================
product_id
name
==================
category
==================
category_id
parent_category_id
level
name
==================
product_category
==================
product_id
category_id (leaf node only)
Questions
- Does the design look good?
- Am I right to only be concerned with the leaf node in product_category->category_id? (I suppose I can work my way from the leaf node up to the root node with this information)
- Given a product, how would I get the category tree(s) that product belongs to?
- Given a category (any level in the category tree), how would I get the number of products categorized under it?
- Any other queries I need to look into?
What you have presented looks like a correct design for a tree in the database. However, your queries may become complicated and you can find yourself requiring many queries for some tasks, like one for every level when traversing the depth.
However, there also exists a totally different approach for tree implementation in databases. It’s kind of counterintuitive, but when you look at the benefits of the query simplicity, it becomes obvious that there is a big set of advanages.
Have a read: http://articles.sitepoint.com/article/hierarchical-data-database
Also, you could be better off if you use an ORM like Doctrine to implement the tree for you, along with the operations.