Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: SQL Server
Expertise: Beginner
Nov 16, 1999

Database Normalization/Design

Question:
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.)

Answer:
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.
DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date