My title might not really convey what i'm trying to achieve, I have some tables which are related but i can't figure out how to create this relationship to make storing and retrieving data much easy. Below is what i was able to come up with.
color table
colorId(PK)
productId(FK)
colorName
size table
sizeId(PK)
productId(FK)
size
product table
productId(PK)
priceId(FK)
Qty
Name
Title
price table
priceId(PK)
productId(FK)
Now my problem is i have product with different variety for example Men's blue addidas glide running shoes
with productId 1, this product have different sizes and different color and the prices varies on sizes and color. example, if this pair of shoe color BLUE size 11 sells for $50, the same pair of shoe with bigger size and different color say size 12 color red might sell for $55, also this same pair of shoe color blue might have only size 11 available in stock and color blue size 12 of this pair might not be available, how do i create my table to save all the differences between color and sizes, and also prices. Any help on this, thanks
You probably shouldnt have every single attribute in it's own table, a potentially better way would be to have a product table and a variant table. This way you could have price, size, color, quantity available, etc all in the same table, then join on the product ie Men's blue adidas glide running shoes
. Your variant table would look something like this:
| variant_id | product_id | price | colour_id | size_id | quantity_available | in_stock |
| 1 | 1 | 50.00 | 1 | 11 | 20 | 1 |
| 2 | 1 | 55.00 | 2 | 12 | 0 | 1 |
Then you could load each variant individually, and have the quantity_available
updated whenever you make a sale. I've also included an in_stock
boolean so you can override whether stock appears on your site without having to adjust the quantity_available
. You'd then have the variant_id
on the purchase
table so you can join these later. Also, I like having sizes and colours on their own tables so you can put a taxonomy in place to aggregate all they blues say, or all the XS, S, M, L, XL.
Something like:
| colour_id | name | base_colour_id |
| 1 | Royal Blue | 3 |
| 2 | Spanish Blue | 3 |
| 3 | Blue | 3 |
| 4 | Ultramarine | 3 |
| 5 | Green | 5 |
| 6 | Mint Green | 5 |
This way you can add more unique colour variants and still report on base colours or full colour names. Here you'd use: SELECT * FROM variant_colour a JOIN variant_colour b ON b.colour_id = a.base_colour_id
you'd get:
| colour_id | name | base_colour_id | colour_id | name | base_colour_id |
| 1 | Royal Blue | 3 | 3 | Blue | 3 |
| 2 | Spanish Blue | 3 | 3 | Blue | 3 |
| 3 | Blue | 3 | 3 | Blue | 3 |
| 4 | Ultramarine | 3 | 3 | Blue | 3 |
| 5 | Green | 5 | 5 | Green | 5 |
| 6 | Mint Green | 5 | 5 | Green | 5 |
This same idea can be used for sizes etc so you don't end up with hundreds of unique attribute variants that make your info impossible to report on.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.