9

I am starting to design a typical product based MySQL database but I keep running in circles on how to design the tables.

I have many types of products

  • Jeans
  • Tshirts
  • Dresses

Which have many of the same attributes

  • Size
  • Price
  • Colour

But some of the products have specific attributes, like length or logo or package.

Do I make a separate table for every product even though many of the columns are the same, or do I make a general product table and then have some sub-tables for specific products?

Tulains Córdova
39.6k13 gold badges102 silver badges157 bronze badges
asked Apr 13, 2015 at 21:05
3
  • 1
    How many 'specific attributes' do you have? Is it an entirely known list? or are you going to get odd ball ones in there from time to time as new products are added? Commented Apr 13, 2015 at 21:06
  • I would imagine sizing information is different for jeans, t-shirts and dresses too... Commented Apr 14, 2015 at 1:23
  • The reason I ask this is that its one thing to have a bunch of specific clothing related details if you are, say, a clothing store. On the other hand, if you are a general retailer, that level of detail of clothing fields isn't going to be applicable to the toy department, or home improvement, or automotive, or ... And thus you will want a more general solution across the board. But it would help to understand the nature of the product areas and range of items to be added. Commented Apr 14, 2015 at 3:24

2 Answers 2

9

Do not make a table for every product.

This problem has been solved many ways. Try this:

Make a product (or products) table, put your common product attributes in the product table, then make an attribute table and a productattribute table, something like:

attribute
---------
attributeId
attributeName
attributeDescription
productattribute
----------------
productId
attributeId
attributeValue

Then you can assign arbitrary attributes to your products.

You can take the schema a little further if you like, using:

attribute
---------
attributeId
attributeName
attributeDescription
attributeType // [float, int, varchar, etc.]
productAttribute
----------------
productId
attributeId
attributeValueFloat
attributeValueInt
attributeValueVarChar

This technique complicates your schema a bit, so be aware of what you are getting into if you decide to use the extra columns to store values by data type.

An alternative that might just suit your needs a little better would be to use a NoSQL Store like MongoDB for your products. Then you can store the attributes you need for each type of product without resorting to the complexity of a relational database schema.

answered Apr 14, 2015 at 3:05
1

One approach would be to design your database similar to how you would define a class hierarchy, where you define base table(s) (classes) that provide the common attributes, and then you add additional tables that provide specific attributes to extend the table to the specific products.

Define a base product table

create table Products_base as
 id
 item
 brand
 model
 description
 size
 color
 price #more about this later

Define additional tables for products that differ substantially

create table pants as
 base #foreign key to base product
 waist
 length
 material
 style
create table dress as
 base #foreign key to base product table
 waist
 hemlength
 bust
 material
 belt

Another approach would be to define the base product table, and then define an attributes table, and provide the attributes for each additional product,

create table Products_base as
 id autoincrement
 item
 brand
 model
 description
 size
 color
 price #more about this later
create table attributes as
 id autoincrement
 name
 description
create table product_+attributes as
 product_id #product.id
 attribute_id #attributes.id
 value

Using pants as an example, you would want to add attributes to pants (add to the attributes table)

"length", "length of item"
"inseam", "length of inseam"
"style", "style of article" #example, "jeans"
#etc

And then you would add values for the attributes

product.id("pants"), attribute.id("inseam"), "32in"
product.id("pants"), attribute.id("waist"), "34in"
product.id("pants"), attribute.id("style"), "jeans"
product.id("dress"), attribute.id("style"), "sundress"
#etc
answered Apr 14, 2015 at 19:02
1
  • Creating an extra table for every kind of products seems like a no-go. Commented Apr 14, 2015 at 19:39

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.