views:

173

answers:

3

I am building an e-commerce website from scratch and have to make a special product configuration page.

It's only 1 type of product, but it is configurable on several levels:

  1. Color (about 4 different options). Value is a VARCHAR.
  2. Material (about 10 different options). Value is a VARCHAR.
  3. Size (About 30 different options). Has 2 Values, a width column and a height column.
  4. Finish (About 20 different options). Value is a VARCHAR.
  5. Other various VARCHAR options etc.

My question is what would this look like in a typical MySQL database. Do I have a table for each type of option or just one table and somehow give it enough columns and have it store all options? I will need to store orders and be able to store the information for the order in a table as well.

I also want to be able to have off the shelf products that aren't customizeable, just like a normal store.

Any help is appreciated!!

+2  A: 

I suggest that you go with one master table, with all of the product information, and a slew of lookup tables, that connects to the master table.

It should look like this:

Product_table:
   ID primary key,
   ColorInt int foreign key lkp_Color_table.ID,
   Material_int foreign key lkp_Material_table.ID,
   // snip the rest

And here's the lookup table:

lkp_color_table:
   ID primary key,
   ColorStr varchar,
   description varchar

lkp_Material_table:
   ID primary key,
   MaterialStr varchar,
   description varchar

The Product_table can look like this:

ID||ColorInt||Material
1 ||1       ||1
2 ||1       ||2

Whereas the lkp_color_table can look like this:

ID||colorstr||description
1 ||red     ||red color
2 ||blue    ||blue color

Note that lkp_color_table can contain unused color , same goes for other lookup tables. So if you have 30 possible colors, you just have to populate lkp_color_table with 30 items, and so on.

There is no need to create a separate Product_id for each color-material- combination, you just have to create a product_id if you use it.

Ngu Soon Hui
Thanks! Question: If in the product table I want to store normal products with no configuration, how do I distinguish this from my configurable product...just leave the colorint_int and materialint_int null?
abadaba
Yes you can leave it null, but MAKE sure that when you create the table, you don't set the column to be non-null
Ngu Soon Hui
Thank You, Very Clear.
abadaba
One more question, Ngu, what does the ColorInt_int and MaterialInt_int columns store? Do i have to enter a row in the product table for every possible configuration? I just want to have 1 row in the product table for my special configurable product...
abadaba
They are storing integer, pointing to a row in lk_*_table
Ngu Soon Hui
but then i need a product for every single configuration? E.g. productid:1, color:1,material:1productid:2, color:1,material:2productid:3, color:1,material:3productid:4, color:2,material:1etc. etc.Im not sure I understand
abadaba
You don't need, you can have a lot of colors, but only 1 or no product.
Ngu Soon Hui
Sorry, what does the row for this configurable product look like in the Product_table? Thanks!
abadaba
Ngu, thanks for the example, but what if I have just one configurable product, but it has 30 possible colors, 80 possible materials. Would I have to create a row for each combination? thats 30*80=240 rows??
abadaba
So you mean, only when a user purchases a particular customized combo, then I add that combo to the product table?
abadaba
A: 

I'd say that you'll need:

OrderTable (OrderID, CustomerID, Date, Price, Shipment, ...)

OrderProductTable (OrderID, ProductID, Quantity)

ProductTable (ProductID, StandardBit, ColorID, Option2ID, ..., OptionNID); StandardBit - to know if product is standard or configurable.

CustomerTable (CustomerID, ...)

ShipmentTable ()

OptionTable for every configurable option (OptionID, Description).

Irina C
+1  A: 

2 + N tables:

  • Products (every single product in the catalog has a record here
  • SpecialKindOfProduct (Your configurable product type, a product_id, and a bunch foreign keys to lookup tables (color_id, size_id, etc)
  • N Lookup tables (colors, sizes, etc)

When you add a second kind of configurable product in the future, you just create AnotherSpecialKindOfProduct.

Hope this is clear enough to follow.

The main advantage is that all your products have some shared attributes (the columns of "Products"), but can have extended attributes as well.

You can easily select everything you need by LEFT JOINing Products to SpecialKindOfProduct, etc.

timdev
this is great, very easy to understand. thanks!
abadaba
Then vote it up, and/or accept it, that's the stuff that makes the SO world go around.
timdev
I can't vote up cause i need 15 karma or something. I wanted to accept your post, but I wanted to hear more answers first, hope you understand, thanks!
abadaba
No problem -- just saw you were new, so figured I'd hint. Didn't realize you needed karma to upvote, though it makes sense.
timdev
Hey Tim, what exactly goes in the SpecialKindOfProduct table? If my special product can have any one of 30 colors, any one of 40 materials, and any one of 80 sizes, do i need to do a row for each permutation...30*40*80??
abadaba
SpecialProducts just store a product_id (a unique foreign key to the products table), and then columns like color_id (which is a foreign key to table colors), size_id. So you have one lookup table for each type of option (color, size, etc), and a corresponding field in SpecialProducts.
timdev
So special products just has 1 row that looks like this?id:1, product_id:1, color_id: 1, material_id: 1?Is the columns in the special product table just bits (1 for using the color table, 0 for not using the color table)? Sorry I feel dumb for asking this, but I am just not getting a clear picture...
abadaba