I have the following fields: productid, category.
A product can be in many categories, and a category can have many products. For example, product1 can be under category1 and category2. I don’t want to have to enter a duplicate entry for every new category.
Which is the most efficient way of designing the database? (I’m using MySQL.)
It seems that you have to accommodate a many-to-many relationship. Break these down into two one-to-many relationships.
You will need three tables to satisfy the problem:
- Products[ProductID, ProductDescription]
- Categories[CategoryID, CategoryDescription]
- ProdCat[ProductID, CategoryID]
This will let you record your products in one table without repetition, record the categories in another table without repetition, and record the various connections between them in the ProdCat table. How you read the direction of the relation will reveal what products are in what categories as well as the other way round.