
When working in a complex database, you can use an intermediate table to create a many-to-many(M:N) relationship between two tables. In such a model every single record in table A, relates to zero, one, or many instances of entries in table B. Likewise, for every record in table B, there exists zero, one, or many related records in table A.
To put this into perspective. Here are some examples of many-to-many relationships.
To come up with an optimized database schema to model the above scenarios, you must use an intermediate table. In this guide, you'll create a sample database for an online shopping cart and create a many-to-many relationship. In this sample database, you'll model a scenario where one product might be available for sale in different outlets while a single office might also sell different products.
To complete this tutorial, make sure you have the following.
sample DatabaseFirst, SSH to your server and log in to MySQL as root.
When prompted, key in your MySQL server's root password and hit Enter to proceed. Next, run the command below to set up a sample database.
Switch to the sample database.
With the database in place, you'll now move on to creating the base tables for your shopping-cart.
In this step, you'll create the offices and products table. The offices table will contain a list of all offices where your business operates, while the products tables will list all items available for sale in the different offices.
Please note, not all products will be available for sale in the different offices. In a real-world example, your store might operate in different jurisdictions where the sale of certain products may not be allowed. Another scenario that might force you to disable the availability of the products in some stores is logistical issues such as high shipping costs or lack of customers.
First, create the offices table. Later in this guide, you'll see how using an intermediary table will be the best option for managing the products' availability in your different offices.
For this guide, assume your company operates in three offices. Populate the table with the following records.
Query the offices table to make sure that the data was inserted successfully.
Ensure you get the list below.
Next, create the products table.
Populate the products table with some records
Query the products table to make sure that all the items are inserted successfully.
Make sure all the products are displayed as shown below.
After defining and populating the base tables, the next step is setting up an intermediary table to link them.
In this step, you'll create an intermediary table to establish a many-to-many relationship between the offices and the products tables.
When creating the intermediary table, a general rule of thumb is to craft a name by concatenating the name of both tables that requires a linkage and separating them with the preposition to and the underscore character(_).
In the different examples highlighted in the introduction, here are some great names that you can use when creating the respective intermediary tables.
contractors_to_companies tablestudents_to_courses tableproducts_to_stores tableactors_to_movies tabletenants_to_apartments table.Since you're using the shopping-cart example for this guide, create a products_to_offices intermediary table.
Next, you'll populate the products_to_offices table. Before you do this, revisit your products catalog and see the globally available items for sale.
Then, you have three offices.
Assume that the WINTER COAT(product_id no 1) will be available for sale in all three offices.
Next, avail the SMART WATCH(product_id no 2) to the NEW YORK(office_id no 1) and CHICAGO(office_id no 3) offices only by running the command below.
Then, associate the UNIVERSAL REMOTE CONTROL(product_id no 3) to only the CHICAGO office(office_id no 3) offices:
Then provision the METAL CASE FLASK(product_id no 4) to both the NEW YORK(office_id no 1) and CHICAGO(office_id no 3) offices:
After running the INSERT statements above, you've now successfully established a many-to-many relationship in your database.
Query the products_to_offices table to see how the relationships are saved.
Output
To see the products' availability in each office, run the commands below.
NEW YORK(office_id = '1') catalog:
Output
LOS ANGELES(office_id = '2') catalog:
Output
CHICAGO(office_id = '3') catalog:
Output
The above outputs confirm that your many-to-many relationship is working as expected.
In this guide, you've learned how to create a many-to-many relationship using an intermediary table on MySQL. Always use the logic in this tutorial when designing a database schema where an M:N relationship is a requirement.
0 Comments
Be the first to comment and share your perspective with the community.