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?
3
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.
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
1