MySQL Workbench Forward Engineer Relationship Tables - mysql

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.

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.

Related

ms access: create 1-m relationship from junction table to other table

I have two tables, tblRecipes and tblChemicals, that are linked in a many to many relationship using a junction table tblRecipesChemicalsLink. I have a bunch of variables for calculations that differ between the same ingredient used in a different recipe. There are also a differing number of those variables between the same ingredients for different recipes:
recipe A: Water: coefficient A = 0,2648; coefficient B = 0,589,
coefficient D =0,1
recipe B: Water: coefficient E = 0,569;
coefficient C = 0,987
I want the database to be flexible in the number of variables that can be associated with the unique combination of recipe and chemical, so I wanted to create a 1-m relationship from the junction table tblRecipesChemicalsLink to a table holding my variables with fields like Value,Name,Description,ID etc. I have not figured out a way to do this succesfully. The junction table in access only lists the individual keys for tblRecipes and tblChemicals, but I would need to link the variable table to the unique combination of those keys. Adding a new ID field to the junction table and adding an ID field as primary key for the Variable table and then linking those only allowed for a many-one relationship between junction and variable table. I would need it to be the other way around. Is there some way to do this in Access? Do I have to somehow write a new custom key and construct it from the primary key values of the tables that are linked by the junction table manually?
I am using ms Access. I am looking for a table with a different coefficient in every ROW linked to the junction table, not different COLUMNS for every class (like in inheritance). The coefficients cannot be assigned to meaningful subtypes in my case.
Do some research on Ternary Relationships. These are relationships of degree three.
Your existing junction table implements a M-M binary relationship. You are going to want an M-M-M relationship. This means that your junction table will consist of not two but three foreign keys. The first two will identify a recipe and a chemical, as they do now. Your new table, (let's call it coefficients) has an ID field, as you have pointed out in your question. A foreign key that references coefficient.id is what you need to add to the junction table.
This means that every Recipe-Chemical pair would need one or more rows in the junction table, one for each coefficient that participates in the recipe for the chemical.
There are more design issues. Your coefficient table is going to have to handle some abstract form of typing unless every coefficient can be specified as a floating point number.
This is really one step towards a star schema design. I have never attempted a start schema design in MS Access, and I don't know what pitfalls you are getting near.

MS Access Multiple Many-to-Many relationships

I have one table "Liability" which is currently providing the available coverage amounts in a dropdown box. It has the following fields:
[CoverageAmountID]
[CoverageAmount]
[Cost]
[StateID]
[ProductID]
[CompanyID]
There are many coverage amounts, companies, states & products.
A complicated query currently pulls the correct Coverage Amount options. My question is if I need to split this up and how?
I have no relationships for this now but there are State, Product & Company Tables.
Do I create junction table with the CoverageAmountID, StateID, ProductID & CompanyID as the primary key?
Is there a better way to handle this with multiple m:m junction tables?
I'm struggling with how this data should be structured. The attached picture shows what I have in the table currently.
Normalizing the liability database:
Instead of using many values in one field, use intermediary tables for many to many relationships
Better use numbers for keys, using text in relationships will be challenging.
If you have one product per coverage then you have a one to many relationship in which case you can use a direct relationship from CoverageTbl to ProductTbl
Yes, you need separate tables with own primary Keys for ProductTbl, StateTbl & CompanyTbl
Attached is a tentative design showing relationships, Note all IDs are of type number, Codes are of type text(5)
database design/relationships

My SQL workbench creating many to many relationship in EER diagram

so I was trying to create an EER diagram from a database model and I wanted to do something similar to this.
Say I have a table named Bag and another one named Address. I already set the PK in Address to be the FK in Bag, when creating the diagram I found that I cannot find the option "many to many" in mySQL workbench.
I wonder what is causing this to happen(maybe I'm doing something wrong but right now I cannot think of any reasonable explanation...)
Hopefully someone can shed some light on this.
Thanks!
Usually many to many relationship would have an additional table to create the relationship -
Address ( table holds addresses )
AddressBag ( tableholds the many to many the bags to addresses relationship with FK to address and FK to bags)
Bag ( table holds the bags )
Many2Many relationship need an additional table to define the relationship between two tables. This table holds the FK to each of the two or more tables. If we want to get the data, just need to select columns by this relationship. Wish this would help you.

In ERD modeling Does A Relation Map To A Database Table

The following is an Entity Relationship of a a Baseball League.
I'm having a bit of confusion understanding Relations and Attributes of Relations.
An description of the diagram follows:
According to the description, Participates is a Relation and Performance is an Attribute (complex) of Participates.
Questions:
How do Participates Map to actual tables in a database?
Would there be a Participates table with the fields that define Performance?
{Hitting(AtBat#, Inning#, HitType, Runs, RunsBattedIn,
StolenBases)}, {Pitching(Inning#, Hits, Runs, EarnedRuns, StrikeOuts, Walks, Outs, Balks, WildPitches)}, {Defense(Inning{FieldingRecord(Position,
PutOuts, Assists, Errors)})}
Similarly are Plays_For, Away_Team and Home_Team also tables.
As you create tables in a database (say MySql) how are Relations differentiated from Entities / Objects like Player, Team and Game.
Thanks for your help.
Question 1: Participates would be an actual table with foreign key columns for Player and Game as well as the column(s) for Performance. All M-N relationships need to be modelled in a separate table.
Question 2: To keep it as a semi-decent relational DB you would have to separate all the info into separate columns so that each column would only hold one singular data. If you didn't separate the data you would break the first normal form and would probably run into problems later in the design.
Question 3: As these three are 1-N you could also implement them with columns on the N-side. In the Game table for example you could have two foreign keys to Team table as well as all the data about the relationships in columns. For claritys sake you could make those relationships as separate tables also. As a sidenote: are you sure Player-Team is a 1-N-relationship so that a if a player changes teams the history-info about the StartDate and EndDate of the previous team is immediately lost?
Question 4: They are all treated absolutely the same - no differentiation.

When or use a many-to-many relation in a database?

I want to know in what situations we create many to many relation. What is the need of doing such?
A quick search goes a long way. Though the following is for MS Access, the concept is the same for any relational database.
Via: office.microsoft.com - Create a many-to-many relationship:
You have a many-to-many relationship when a single record in one table
can relate to many records in another, and a single record in that
second table can also relate to many records in the first. For
example, say your company has several types of computers and several
technicians, with each technician certified to work on some, but not
all, of the computers. Each technician can be related to more than one
computer, and in turn, each computer can be related to more than one
technician.
To track who can work on a given machine, you create a many-to-many
relationship by adding the primary keys from both sides of the
relationship to a third table, called a junction or link table. In
other words, a many-to-many relationship is really just a pair of
one-to-many relationships.