2

I have products in my database (MySQL). I have my prices based on different combinations of options for my products. I have the following data:

PRODUCTS

prodID | prodName
1      | Ball

OPTIONS

optionID | optionName | prodID
1        | color      | 1
2        | size       | 1
3        | material   | 1

OPTIONVALUES

ovID | optionID | ovValue
1    | 1        | red
2    | 1        | blue
3    | 2        | small
4    | 2        | big
5    | 3        | wood
6    | 3        | glass

for prices I would like to have like this:

red small wood ball - $13 red small glass ball - 14 red big wood ball - 16 red big glass ball - 17 blue small wood ball - 12.5 [...]

How can I design the database structure for this? I have tried several ways but none was good. if the number of the options were static then every option could have its own table, but this was that is not a good solution.

2 Answers 2

1

I like what you already have. If you want it to be fully flexible as i guess you do, i would do the following:

Add two more Tables.

Product_Instance

instanceID | prodID | price
1          | 1      | 13

Product_Instance_Options

instanceID | ovID
1          | 1
1          | 3
1          | 6

In this way you could define all possible combinations and prices for them. Might be a hell of a lot work to set up all the prices, but if you cant calculate them (like glass = +2$, small=2$ / big=4$) you will have this however you do it.

Sign up to request clarification or add additional context in comments.

2 Comments

how would you query this to get a price for a given array of options?
answer to my comment question above: stackoverflow.com/questions/40296781/…
1

How about something like this:

productTypes

prodID | prodName
1      | Ball
2      | Car

productInstances

prodInstanceID | prodID | Prize
1              | 1      | $19
2              | 1      | $50
3              | 2      | $8

properties

propertyID  | propertyName
1           | color
2           | size
3           | material

propertiesValues

pvID |propertyID  | propertyName
1    | 1          | red
2    | 1          | blue
3    | 2          | small
4    | 2          | medium
5    | 2          | large
6    | 3          | wood
7    | 3          | glass

productProperties

ppID | prodInstanceID | pvID
1    | 1              | 1
2    | 1              | 3
3    | 1              | 6
4    | 2              | 2
5    | 2              | 4
6    | 2              | 7
7    | 3              | 1
8    | 3              | 3 
9    | 3              | 6 

In your design you can't determine which option belongs to which product instance. If you look at your table optionvalues, there is no way to group together your options.

4 Comments

you have to insert all the values (like red/small) for each and every instance of every product. You might find the word "small" hundreds or thousands of times in your Database. This is redundant = bad.
anyway, +1 for a closely appropiation
@Flo, you are right about that. I modified my answer. Moved price to productinstance and created a propertyvalues table. Edit: Actually, now my answer is the same as your answer.
jea and now youre answer is pretty much the same as mine only with an additional primary key. (which is not neccesary)

Your Answer

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.