I’m writing a program for kitchen/recipe management. As of now, the program can add new ingredients to the database. My next step is to add new recipes to the database, using the ingredients.
Since each recipe will have multiple ingredients, and an unlimited number of which can be added to a recipe, creating a separate column for each ingredient selected wouldn’t be efficient (I’m assuming).
So the method I’ve come up with to store the ingredient selections is:
-
Add a recipe_ingredients column to the recipe table in the db, and store the name of each ingredient in 1 single (text) field, separated by commas (CSV style).
-
Write a PHP script to store the list of recipe_ingredients into an array.
-
Do the same for all other attributes (recipe_ingredients_serving_size, recipe_ingredients_calories, etc.)
So the question is: Is this method optimal, and if not, what’s a better way to go about this?
This is a simple N:M(many-to-many) relationship, and your approach that you’ve laid out will likely spell out disaster in terms of efficiency and management.
Here is your situation:
recipesandingredients.Whenever you have this relationship between any two entities, you are going to want to have not two, but three tables:
recipesandingredientsare what’s known as base tables, where they store intrinsic information about that particular entity.The
recipes_has_ingredientstable is what’s known as a cross-reference table (or “XREF”), which stores the associations between the two entities. The fields in this table:recipe_idandingredient_idboth link to their respective base tables, and the combination of the two in each row in the XREF table is unique. It basically maps the many associations that eachrecipe_idmay have to differentingredient_id‘s and vice versa.Why does this design facilitate many-to-many relationships? Because data in that XREF table is allowed to look like this:
As you can clearly see: One recipe is associated with many(3) ingredients, and one ingredient is associated with many(3) recipes. Also notice how values in either of the columns are allowed to repeat, but that the combination of the two columns is unique — this is really the key aspect of this design that makes the N:M relationship work.
So here are some simple examples of how you can easily retrieve and manage data using this design:
^ The above
DELETEoperation also deletes all of that recipe’s associations if you’ve properly defined the CASCADE rules between your relationships.Looking back at your original design, what if you wanted to update or delete certain ingredients that a recipe has, or how about change the name of an ingredient? You would need hacky procedural code to modify the right positions in csv strings, or you would need to update every row in the table to reflect even the slightest changes in a single ingredient.
There are also many more compelling questions you could answer that you couldn’t really otherwise using your original design such as:
…The list goes on, and the benefits of implementing this design will serve you well. You will prevent yourself from a great deal of hardship and suffering by doing things the right way. =)