9

I have an assignment for shopping cart dealing with shirt store, and was confusing with database design in storing shirt attributes such as color, size and stock for each item.

Let's say to store below shirt to db:

Product name: Nike shirt  
Available colors: black, white, blue 
Size: M, L, XL 
Stock: Black - M - 5 pc
       White - L - 10 pc
       Blue  - M - 2 pc
       Blue  - XL - 3 pc
       (and so on...)

Instead of storing above info iteratively in a table like so:

table shirt
  id    product       color    size   stock
---------------------------------------------
   1    Nike Shirt    black     M       5
   2    Nike Shirt    white     L       10
   3    Nike Shirt    blue      M       2
   4    Nike Shirt    blue      XL      3
  ....

What is the best way to design table to keep these attribute and product effectively?

I know that could be JOIN multiple table together, but I need advise on these attributes on how to put separately with difference table and fetch the info when people goes to respective page and show them up how many stock are left for the specific size?

1
  • 1
    This depends what level you want to have, you can have 1 table, you can have 2, 3, 4, 5, 10.. all depends how big your solution is. Is this for homework, or for enterprise biz. Could be one table with products, T-Shirt... one with vendors.. Nike... one with Properties... like Color, Size, Gender... one that handles your repository (the stock).... you see what I mean. I recommend you to read about en.wikipedia.org/wiki/Database_normalization Jul 31, 2015 at 13:28

4 Answers 4

16

Here's your table.

Shirt

  id    product       color    size   stock
---------------------------------------------
   1    Nike Shirt    black     M       5
   2    Nike Shirt    white     L       10
   3    Nike Shirt    blue      M       2
   4    Nike Shirt    blue      XL      3
  ....

You see how you've duplicated the product name "Nike Shirt" and the color "blue". In a normalized relational database, we don't want to duplicate any information. What do you think would happen if someone accidently changed "Nike Shirt" to "Nike Skirt" in row 4?

So, let's normalize your table.

We'll start with a Product table.

Product

  id    product    
------ ------------
   0    Nike Shirt

Generally, database id numbers start with zero, not one.

Next, let's create a Color table.

Color

  id    color   
------  -------
   0    black    
   1    white    
   2    blue 

Next, let's create a Size table.

Size

  id   size 
------ -----
   0    XS
   1    S
   2    M
   3    L
   4    XL
   5    XXL 

Ok, now we have 3 separate object tables. How do we put them together so we can see what's in stock?

You had the right idea with your original table.

Stock

  id    product       color    size   stock
---------------------------------------------
   0        0           0        2       5
   1        0           1        3      10
   2        0           2        2       2
   3        0           2        4       3

The product, color, and size numbers are foreign keys back to the Product, Color, and Size tables. The reason we do this is to eliminate duplication of the information. You can see that any piece of information is stored in one place and one place only.

The id isn't necessary on the Stock table. The product, color, and size should be unique, so those 3 fields could make a compound key to the Stock table.

In an actual retail store, a product could have many different attributes. The attributes would probably be stored in a key/value table. For your simple table, we can break the table up into normalized relational tables.

10
  • 2
    Substituting surrogate keys for text identifiers isn't normalizing. The original functional dependency (product, color, size) -> stock remains unchanged in your final version, and is in fact fully normalized.
    – reaanb
    Jul 31, 2015 at 21:33
  • This answer is an example of "over-normalization".
    – Rick James
    Jul 31, 2015 at 22:09
  • 1
    It has nothing to do with normalization.
    – reaanb
    Jul 31, 2015 at 22:31
  • @gilbert-le-blanc How would you query this table?
    – Dre_Dre
    Dec 19, 2016 at 0:44
  • if you would want to display the same product with different colors as a single page to the user, where user can change the color(hence changing the product), how would you link all these products together?
    – vikrant
    Jul 12, 2020 at 17:16
0

Substituting surrogate keys for text identifiers isn't normalizing. The original functional dependency (product, color, size) -> stock remains unchanged in your final version, and is in fact fully normalized. – reaanb Jul 31 '15 at 21:33

0

The way of you give the answer with making a junction table which is contain the different type of product attributes is very good. But when you have handers of products it will work too but it might be a bit confusing for the designer.

1
  • 2
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    May 21 at 18:12
-1

This is a horrendously complicated business domain. Your suggested single-table layout has a couple of challenges. Firstly, lots of repeated entries are a smell in database design. Secondly, it kind supposes that all entities have the same attributes - as @gilbertleblanc writes, there are likely going to be lots of other attributes a real-world application would need to store (manufacturer, material, allergy info, etc.).

So, I would split this into two questions:

  • Which products and variants exist?
  • How much stock do we have of each item?

The minimal way to represent your sample data would be:

products
----------
id
name
color
size


product_stock
------------
product_id
stock_quantity

In real-life scenarios, the stock table is usually a ledger of transactions - the sum of movement gives you the current amount in stock.

product_id    date     movement
---------------------------------
1            1 Jan 2021      10
1            2 Jan 2021.     -1
1            3 Jam 2021.     -4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.