For example, in my product catalog database. I will have two tables, category and products.
the simple and efficient design is that
- category is parent table.
- product is child table.
- category_id will be the key for category and will be the foreign key in product.
here is my problem.
i may have same product assinged to two or more categories.
so in the above design, i will have to have the same product appears twice or more in the product table, one belongs to each category it is assigned to. and the product should be unique in the table. so for the same product, in order to following the rules, i may have to assign the same product with different product_id or different product_code.
that is not good practice.
so here comes another design.
- category is parent table.
- product is parent table.
- category_and_product_link is child table, has the category_id and product_id as the foreign keys.
this way, when i want to assign the product to two or more categories, i just need to create two or more pairs of (category_id, product_id) in the category_and_product_link table.
this design solves the problem. but this design is adding complexity into the whole thing. specially, if you have more layes such as category->sub-category->sub-sub-cateogry->products. and more relations with other tables,
My questions are:
- what is your suggestion? use the second design or use the first design and elimiate the possibility that product could be assigned to different categories (if we have to, create another product record.)
which design will be general accept design?
- any other better solution?