Understanding many-to-many table relationships

In a many-to-many relationship, one table corresponds to many rows in another table. In addition, one item from one table can correspond to multiple rows in the other. An example of this could be tables of customers and products. Customers could purchase many products, and many products can be purchased by many customers. In the case of the baseball database, an example of a one-to-many relationship is the appearances table's relationship to the batting table. There are many rows per playerid in the appearances table and there are many rows for each of those playerids in the batting table.

The following screenshot shows an example of a many-to-many relationship: 

The following screenshot shows you an example of an entire ERD: 

In the preceding screenshot, the people table has a one-to-many relationship with the salaries table, a one-to-zero or many relationship with the collegeplaying table, and a one-to-zero or many relationship with the appearances table. In addition, the collegeplaying table has a many-to-one relationship with the schools table. 

You can reverse engineer an entity-relationship diagram in MySQL Workbench. It works best if all the tables have foreign key references, but if they don't, then you can add them via the diagram. 

To reverse engineer a diagram in MySQL Workbench, you can go through the following steps: 

  1. Select Database in the menu, and then select Reverse Engineer:

  1. Set the parameters for connecting to the DBMS—in this case, it's my local instance—and then click Continue:

  1. Click on Continue on the Connect to DBMS and Fetch Information page after it shows Execution Completed Successfully and Fetch finished:

  1. Select a schema and then click on Continue. In this case, I'm selecting the schema we created in a previous chapter of this book (yourschema):

  1. Click on Continue on the Retrieve and Reverse Engineer Schema Objects page after it shows Execution Completed Successfully and Fetch finished
  2. On the Select Objects to Reverse Engineer page, click the Show Filter button to see all the objects that it will reverse engineer. You can exclude objects by clicking the arrows between the boxes that show Objects to Process and Excluded Objects. Click Execute when you have included the objects that you want:

  1. Click Continue on the Reverse Engineer Progress page after it shows Operation Completed Successfully.
  1. Click Close on the Reverse Engineering Results page, which shows you the summary of the reverse-engineered objects:

  1. This will bring you the EER diagram. As you can see in the following screenshot, there is a one-to-one or many relationship between the teams table and the managers table:

The EER diagram can be very useful right after it is generated if the proper foreign key constraints have been set up. If your database doesn't have these, you will have to manually map table relationships inside the EER diagram. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.42.163