Results 1 to 3 of 3

Thread: Insert with many-to-many relationship

  1. #1

    Thread Starter
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    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.

    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!

  2. #2
    Hyperactive Member The_Duck's Avatar
    Join Date
    May 2005
    Location
    Leamington, UK
    Posts
    351

    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
    Last edited by The_Duck; Aug 16th, 2007 at 10:10 AM. Reason: Cateagory?

  3. #3

    Thread Starter
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: Insert with many-to-many relationship

    Thanks for the reply duck. I'll give her a shot!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width