Insert with many-to-many relationship
Hi all,
This is my first project (recipe DB for the family) that involves more than 3 little tables in an Access database, so I think you'll see a lot of me here for awhile. :bigyello:
Using MySQL 5.0 and my tables look somewhat like this:
Recipe
RecipeID
CategoryID
SourceID
Instructions
etc..
Ingredient
IngredientID
Ingredient
RecipeIngredient
RecipeID
IngredientID
Measurement
...
Category
CategoryID
Category
Source
SourceID
Source
..and some other tables like this with just the ID and field.
I'll have one form to fill in all fields. I'm basically looking for some beginning logic on how to insert into these tables because frankly I don't know where to start particlarly with the Recipe-Ingredient many-to-many relationship.
Do I first insert into the Category, Source tables if necessary, get those IDs, then insert into the Recipe table, get that ID and then insert into Ingredient, then finally into the RecipeIngredient junction table? Should this be a stored procedure?
I'd appreciate any sort of general bump in the right direction regarding inserting into multiple tables, some of which have a many-to-many relationship. Thanks again!
Re: Insert with many-to-many relationship
What you need to do is wrap your connection in a transaction. You should always insert your parent rows first, followed by your child rows.
Your insert order should be
Source
Category
Ingredient
Recipe
RecipeIngredient
Re: Insert with many-to-many relationship
Thanks for the reply duck. I'll give her a shot!