Welcome to Dream.In.Code
Getting Help is Easy!

Join 136,085 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,549 people online right now. Registration is fast and FREE... Join Now!




Database design: Dealing with arrays

 
Reply to this topicStart new topic

Database design: Dealing with arrays

generalchaos316
20 Apr, 2008 - 05:42 AM
Post #1

New D.I.C Head
*

Joined: 4 Nov, 2007
Posts: 15


My Contributions
I am still hacking away at my little recipe program and am trying to find the best way to design the database that is going to hold all of my data (and not be dog slow when there are tons of rows). As part of this program, I would like to be able to calculate the nutritional information of a recipe "on the fly" by adding up the recipe's individual ingredients' nutrition info (which would be stored in an "ingredients" table).

I am still new to database design and have hit a wall with how to represent this data. If I were representing this in Java I would have a "Recipe" object that holds an array of "Ingredient" objects (along with other variables such as the recipe name, directions, etc). I am not sure what the best practices are for a.) representing an array (of unknown size) of values and b.) representing an abstract data type such as an "Ingredient."

I also want to be able to find recipes that contain a given ingredient(s), in case you want to use up food that is going bad, which is why I don't want to use a simple text variable for the ingredients.

Hopefully that is enough relevant information to get pointed on the right track smile.gif
User is offlineProfile CardPM
+Quote Post

mikeblas
RE: Database Design: Dealing With Arrays
20 Apr, 2008 - 12:00 PM
Post #2

D.I.C Head
**

Joined: 8 Feb, 2008
Posts: 155



Thanked: 1 times
My Contributions

In your case, I'd make a many-to-many table. One recipe can have many ingredients, and one ingredient can appear in many recipies.

If we have a recipie table:
CODE

RECIPIE
RecepieID,RecepieName
1,"Chocolate Mousse"
2,"Chocolate Cookies"
3,"Lemon Cake"


And a reference table of ingredients:

CODE

INGREDIENT
IngredientID,IngredientName
1,"Chocolate Chips"
2,"Lemons"
3,"Sugar"
4,"Fish oil"


We might have this RecepieIngredients table. (Or, we might not; I don't cook, so what the heck do I know?)

CODE

RECEPIEINGREDIENTS
RecepieID,IngredientID
1,1
1,3
2,1
2,3
3,2
3,3


In RECEPIE, the RecepieID is the primary key. Similarly, in the INGREDIENT table, the IngredientID is the PK.

In RECEPIEINGREDIENTS, the primary key has two columns: (RecepieID, IngredientID). The RecepieID there is a foreign key back to the RECEPIE table, and the IngredientID is a foreign key back to the INGREDIENT table.

I hope that helps, and that you can understand the examples--representing database tables in a forum is pretty cumbersome.
User is offlineProfile CardPM
+Quote Post

nsh22
RE: Database Design: Dealing With Arrays
25 Apr, 2008 - 12:14 PM
Post #3

New D.I.C Head
*

Joined: 23 Apr, 2008
Posts: 3


My Contributions
just so you know mike, you spelt recipe wrong ( I may have too)
User is offlineProfile CardPM
+Quote Post

mikeblas
RE: Database Design: Dealing With Arrays
28 Apr, 2008 - 06:07 AM
Post #4

D.I.C Head
**

Joined: 8 Feb, 2008
Posts: 155



Thanked: 1 times
My Contributions
Gosh, I did. Curiously, I can't edit the post.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 08:04PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month