views:

354

answers:

3

Do they have a table for all categories and another for all sub-categories (and another for the sub-sub-categories and so on), or what? How do all the levels go around communicating with each other?

I'm a noob getting started on a project that might have that level of complexity and I am having a hard-time wrapping my head around that.

I'm working with Rails but I'd also appreciate answers in database schemas, pointers to further reading etc.

+1  A: 

My recommendation is to use separate tables for each, however, the question of "can a subcategory exist in multiple categories" also alters the outcome.

If there's a one to one you have Categories table A, then Subcategories Table B with Parent Category ID (from A).

But if they can exist in multiple categories then you likely would have a normalized table structure whereby you have Categories A, Subcategories B and a third table of Cat_ID and Sub_ID as a single record.

Also, relating to the product, you'd also have a normalized table setup where you have Products Table, Categories table and then Product_Categories with product_id and category_id. This is infinitely scalable (god forbid) with sub categories table, product_subcategories, sub sub, sub sub sub, blah blah blah, etc. etc. etc.

jerebear
+4  A: 

I am assuming that you are dealing with hierarchical data here.

You just need two tables. One of the tables is for the categories and relationships between the categories. For example:

CategoryID CategoryName ParentCategoryID
----------------------------------------
1          Computers    NULL
2          Programming  1
3          Games        1
4          Python       2

The other table is for storing the data associated with the categories. For example:

CategoryID ItemID Description
----------------------------------------------
4          1      Book – Programming in Python
3          1      World of Warcraft

The first table contains a foreign key column that links the subcategories to their parent categories. This is known as the “Adjacency List Model”. This model has the advantage of being simple to understand and doing various things (e.g. retrieving the path to the Python category – /Computers/Programming/Python) with this model can be done quite easily with client-side code (if you don’t mind the performance cost because you may need to make multiple queries to the database). However, it can be mindboggling if you try to do it in full SQL. For example, retrieving the path for a category will require self-joins.

Another way of structuring this table is to think of the categories and sub-categories as sets and subsets (known as the “Nested Set Model”). It’s hard to explain this model without diagrams, and this article seems to do a better job in explaining the concepts (both “Adjacency List” and “Nested Set” models):

http://dev.mysql.com/tech-resources/articles/hierarchical-data.html

maxyfc
Your solution looks like the most scalable with the least amount of work (no need to keep creating tables). The Nested Set model is absolutely gorgeous, and I found a Rails plugin that I assume makes it easier to work with them. Thanks!
Baby Diego
Thanks maxyfc.This really helped me.
Yogi Yang 007
A: 

The easiest way to do this is to develop a relationship diagram. Go high level and then drill down.

Sir Psycho