bahaa Posted June 7, 2011 Share Posted June 7, 2011 Hello, If you want to have categories and subcategories, what is the best approach to do that ? a table for the category and another one for the subcategory, or one table only? Link to comment Share on other sites More sharing options...
Antonio Conte Posted June 7, 2011 Share Posted June 7, 2011 category (id, name) 1 / Category 1 2 / Sub 2 3 / Category 3 three (id, parent*) 1 / 0 2 / 1 3 / 0 Result: Category 1 ---> Sub 2 Category 3 ------------------- Underlined: Primary key Asterix: Foreign key 1 Link to comment Share on other sites More sharing options...
bahaa Posted June 8, 2011 Author Share Posted June 8, 2011 category (id, name) 1 / Category 1 2 / Sub 2 3 / Category 3 three (id, parent*) 1 / 0 2 / 1 3 / 0 Result: Category 1 ---> Sub 2 Category 3 ------------------- Underlined: Primary key Asterix: Foreign key I did not quite understand this. How about this First table Category Cat_id Parent_id Item Subcategory ID Sub_id Item the sub_id in subcategory is the parent id in category Link to comment Share on other sites More sharing options...
HartleySan Posted June 8, 2011 Share Posted June 8, 2011 I would assume that a category would contain mulitple subcategories. That means that there is a one-to-many relationship between the category and subcategory tables. In which case, the category ID should be a foreign key in the subcategories table. For example: Category table id category_name 1 cars 2 trucks --- Subcategory table id category_id (foreign key) subcategory_name 1 2 Ford trucks 2 1 Ferrari sports cars 3 1 Honda coupes Anything, I think you get the point. Then, you could use joins to get the proper info and have a properly normalized DB. 1 Link to comment Share on other sites More sharing options...
Recommended Posts