Question

I am thinking about my application and I want to store data about drinks. Now I am thinking what is best that if I save ingredients just as nvarchar column in table with drink or if I create new table with ingredients and create relationship one to many? I want database to be just read-only and I want to have option filter by ingredients. So what´s best way for windows phone (for performance)? And if the new table would be the better choice, I should use EntitySet, EntityRef, am I right? And I would have for every ingredient new row in table? Let´s say I have 100drinks, in average that every drink has 4ingredients so I have in first table 100rows and in second cca 400rows? Thanks for help

Was it helpful?

Solution

Actually, both solutions proposed are wrong. A drink can have many ingredients and an ingredient can be used in many drinks. Hence, we have a many-to-many relationship here. The proper way to model this is the following (I'm adding data for it to be more understandable):

Ingredients (PK: Id)

+----+--------------------+
| Id |        Name        |
+----+--------------------+
|  1 | Water              |
|  2 | Sugar              |
|  3 | Coffe              |
|  4 | Virgin Islands Tea |
|  5 | Ice                |
+----+--------------------+

Drinks (PK: Id)

+----+-------------+
| Id |    Name     |
+----+-------------+
|  1 | Black Coffe |
|  2 | Tea         |
|  3 | Ice Tea     |
+----+-------------+

Drinks_Ingredients (PK: Drink_Id, Ingredient_Id)

+----------+---------------+------------+
| Drink_Id | Ingredient_Id | Proportion |
+----------+---------------+------------+
|        1 |             1 |         70 |
|        1 |             2 |         10 |
|        1 |             3 |         20 |
|        2 |             1 |         90 |
|        2 |             4 |         10 |
|        3 |             1 |         80 |
|        3 |             4 |         10 |
|        3 |             5 |         10 |
+----------+---------------+------------+

I'm adding this Proportion column to show you how to add data that is dependant on the pair of drink-ingredient. Now, if you're worried about the size of the tables it'll be quite small as the only tables that will have the more complex data types (varchars) will be the ingredients and drinks tables, which will have the minimum amount of records possible: one per each drink and one per each ingredient.

If you still have doubts keep looking at the example, you'll get it :)

OTHER TIPS

I would do a table for ingredients with a description and an id, and store the ids in the drink table cause it's the elegant way to do it. For 100 drinks, you won't see a difference for the performance.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top