Search code examples
mysqldatabaseentity-relationshipforward-engineer

MySQL Workbench Forward Engineer Relationship Tables


I used the Forward Engineer tool in the MySQL Workbench to generate a database based on the tables I entered. I was able to populate my tables, such as "Villagers," "Fish," and "Items" for the database I am building around the game Stardew Valley. However, when I linked the tables in the ER Diagram it created new tables based on relationships, such as Cooking_Has_Fish because many fish can be used in many recipes and many recipes can use many fish. But, once I populated the Cooking and Fish tables there was nothing in the generated Cooking_Has_Fish table. I am trying to understand how this table functions or how it can be used, or if it needs to be populated and I missed something.

Thanks for reading.


Solution

  • No table ever gets populated on its own, you need to make sure that it gets populated. The third tables created for many-to-many relationships are called associative or junction table and as a minimum contain the primary keys of the 2 tables they join.

    If you have a recipe_id identifying recipies and a fish_id identifying fishes, then Cooking_Has_Fish table would have at least a recipe_id and a fish_id field.

    If you want to associate fish and chips (with recipe_id 1) with cod (fish_id being 2), then you would do the following insert:

    insert into Cooking_Has_Fish (`recipe_id`, `fish_id`) values (1,2)
    

    This means the cod is required for the "fish and chips". You may add additional fields to your association table, such as how much cod you need for the fish and chips.

    In summary: you have to populate your association tables based on how you would like to associate your entities (tables) that have many-to-many realationship with each other.