Solved

DB Design for Product and Subproduct Tables

Posted on 2007-12-06
7
390 Views
Last Modified: 2008-02-01
We have a database which holds a list of products in a Product table. Each product can contain one or more subproducts, each of which is characterised by a product type. This is how I was thinking of designing the relevant tables:

- Subproduct Type Table (PK Subproduct Type ID, Subproduct Type description);
- Product Table (PK Product ID, Product attributes) has a one to many relationship with
- Subproduct Table (PK Subproduct ID, FK Product ID, FK Subproduct Type ID, Subproduct attributes).

However, someone has suggested to me the following design, which apparently should help queries to run faster:

- Product Table (PK Product ID, Product attributes) has a one to many relationship with
- Subproduct Type Table (PK Subproduct Type ID, FK Product ID, Subproduct Type description) has a one to many relationship with
- Subproduct Table (PK Subproduct ID, FK Subproduct Type ID, Subproduct attributes).

Which design would you recommend? I have worked with databases before, but I have never designed one from scratch.
0
Comment
Question by:Rothbard
  • 4
  • 3
7 Comments
 
LVL 25

Expert Comment

by:imitchie
ID: 20418391
It really depends on what sort of typical queries you expect to run in the future, as well as whether the relationship makes sense at all!
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20418408
Your original design allows for

Product: Car, Truck, Van, Bike  (vehicle)
SubProduct: LightVan, Tricycle, ForkliftTruck
SubProductType: Budget, MidRange, Premium.

So you can Budget-car, budget-van, premium-bike etc, with SubProduct in the middle.

However the suggested design swaps this all around.

Product: Car, Truck, Van, Bike  (vehicle)
SubProductType: PremiumCar, BudgetCar, LightBike, CompactCar, PremiumVan
SubProduct: etc etc

You see how the relationships are completely different. It makes a lot less sense to change your design, unless you have 10million records and 2000 product types and a slow database engine.  Then maybe the 2nd design can serve the query "Budget cars" a little faster, but not even by much.
0
 

Author Comment

by:Rothbard
ID: 20418591
Sorry, I should have clarified what I meant. The relationship between Product and Subproduct is a "has a" one. A better description would have been "Product" and "Component". Each Product is made up by 1 to 5 Components. We have tens of thousands of Products and Components. In total there are only about 10 different Component Types.

If we choose the first design, we are going to have a very small Component Type table, with 10 different entries listing all the Component Types in the system. The Component table is linked to the Product table by a Product ID foreign key, and to the Component Type table by a Component Type ID foreign key.

If we choose the second design, the Component Type table will have a separate entry for each Product, i.e. it will have Product ID as a foreign key. The Component table then has only got a Component Type ID foreign key.
0
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 
LVL 25

Expert Comment

by:imitchie
ID: 20418702
I did understand that.  I was just illustrating that they have very different meanings.  Your "small items" category has to be replicated for each Product in the 2nd scenario.
0
 

Author Comment

by:Rothbard
ID: 20418782
So you would recommend the first scenario?
0
 
LVL 25

Accepted Solution

by:
imitchie earned 500 total points
ID: 20418874
For me personally, yes, it makes a lot more sense relationally.
But as with all things, only you would know best. What works 90% of the time may just not work if you fall into the 10% category.
If you had good indexes on the foreign keys on Component, I can't see why it should be much slower, if any.
0
 

Author Comment

by:Rothbard
ID: 20530606
Sorry for the delay, I had forgotten to accept your answer. Thanks.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article describes some very basic things about SQL Server filegroups.
Creating and Managing Databases with phpMyAdmin in cPanel.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now