{"id":1824,"date":"2023-10-12T16:45:57","date_gmt":"2023-10-12T08:45:57","guid":{"rendered":"https:\/\/five.co\/?p=1824"},"modified":"2026-02-25T11:43:19","modified_gmt":"2026-02-25T03:43:19","slug":"how-to-create-many-to-many-relationships-in-sql","status":"publish","type":"post","link":"https:\/\/preview-php85.dzine.org\/five\/blog\/how-to-create-many-to-many-relationships-in-sql\/","title":{"rendered":"How To Create Many-to-Many Relationships in SQL"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"learn-how-to-implement-many-to-many-relationships-with-an-easy-example\">Learn How to Implement Many-To-Many Relationships With an Easy Example<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Relational database systems, such as MySQL, PostgreSQL, or SQLite make storing and retrieving data quick and efficient. The underlying principle of a relational database is to store data in<strong> related tables<\/strong>, which avoids data redundancy. Tables can be related in several ways: in a one-to-one relationship, a <a href=\"https:\/\/preview-php85.dzine.org\/five\/blog\/how-to-create-one-to-many-relationships-in-sql\/\" data-type=\"post\" data-id=\"2373\">one-to-many relationship<\/a>, or in a <strong>many-to-many relationship<\/strong>. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Of these three, only the<strong> many-to-many relationship requires an extra table<\/strong>, called a join table, junction table, intersection table, or&nbsp;cross-reference table. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s figure out how to create many-to-many relationships in SQL.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"download-five-to-learn-how-to-implement-a-many-to-many-relationship\">Implement a Many-to-Many Relationship in a Web Application<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Learn how to implement many-to-many relationships by building a web application on a MySQL database in Five&#8217;s database app builder.<\/p>\n\n\n\n<div style=\"background-color: #001524;\"><hr style=\"height: 5px;\">\n<pre style=\"text-align: center; overflow: hidden; white-space: pre-line;\"><span style=\"color: #f1ebda; background-color: #4588d8; font-size: calc(25px + 0.390625vw);\"><strong>SQL Made Simple - Try Five<\/strong><br><span style=\"font-size: 14pt;\">Five is the Easiest Way to Create, Model and Query a SQL Database<\/span><\/span><\/pre>\n<p style=\"text-align: center;\"><a href=\"https:\/\/preview-php85.dzine.org\/five\/get-started\" target=\"_blank\" rel=\"noopener\"><button style=\"background-color: #f8b92b; border: none; color: black; padding: 20px; text-align: center; text-decoration: none; display: inline-block; font-size: 18px; cursor: pointer; margin: 4px 2px; border-radius: 5px;\"><strong>Get Started<\/strong><\/button><\/a><br><span style=\"color: #ffffff; font-size: revert;\">___<\/span><\/p>\n<p style=\"text-align: center;\"><span style=\"text-decoration: underline;\"><strong><span style=\"color: #ffffff; text-decoration: underline;\">Here&#8217;s What You Can Do With Five<\/span><\/strong><\/span><\/p>\n<p style=\"text-align: center;\"><span style=\"color: #ffffff;\">Create and Model a SQL Database \u2705<br>Write or Build SQL Queries \u2705<br>Visualize Queries as Charts or in Dashboards \u2705<br>Add CRUD Permissions to Control Data Access \u2705<br>Host Your Database Online \u2705&nbsp;<\/span><\/p>\n<p style=\"text-align: center;\"><span style=\"color: #f1ebda; background-color: #4588d8;\"><em><strong>&#8220;Five bridges the gap between SQL and the web, <br>allowing me to create full-stack applications almost entirely in SQL&#8221;<\/strong><\/em><\/span><br><span style=\"color: #ffffff;\">&#8211; Crag Jones, Database Administrator (DBA)<\/span><\/p>\n<hr style=\"height: 5px;\"><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<div class=\"root-eb-toc-hxbh1yr wp-block-essential-blocks-table-of-contents\"><div class=\"eb-parent-wrapper eb-parent-eb-toc-hxbh1yr \"><div class=\"eb-toc-container eb-toc-hxbh1yr  eb-toc-is-not-sticky eb-toc-not-collapsible eb-toc-initially-not-collapsed eb-toc-scrollToTop style-1 list-style-none\" data-scroll-top=\"false\" data-scroll-top-icon=\"fas fa-angle-up\" data-collapsible=\"false\" data-sticky-hide-mobile=\"false\" data-sticky=\"false\" data-scroll-target=\"scroll_to_toc\" data-copy-link=\"false\" data-editor-type=\"\" data-hide-desktop=\"false\" data-hide-tab=\"false\" data-hide-mobile=\"false\" data-itemcollapsed=\"false\" data-highlight-scroll=\"false\"><div class=\"eb-toc-header\"><h2 class=\"eb-toc-title\">Table of Contents<\/h2><\/div><div class=\"eb-toc-wrapper \" data-headers=\"[{&quot;level&quot;:2,&quot;content&quot;:&quot;Learn How to Implement Many-To-Many Relationships With an Easy Example&quot;,&quot;text&quot;:&quot;Learn How to Implement Many-To-Many Relationships With an Easy Example&quot;,&quot;link&quot;:&quot;learn-how-to-implement-many-to-many-relationships-with-an-easy-example&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;Implement a Many-to-Many Relationship in a Web Application&quot;,&quot;text&quot;:&quot;Implement a Many-to-Many Relationship in a Web Application&quot;,&quot;link&quot;:&quot;implement-a-many-to-many-relationship-in-a-web-application&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;Understanding Many-to-Many Relationships&quot;,&quot;text&quot;:&quot;Understanding Many-to-Many Relationships&quot;,&quot;link&quot;:&quot;understanding-many-to-many-relationships&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;Implementing a Many-to-Many Relationship in SQL&quot;,&quot;text&quot;:&quot;Implementing a Many-to-Many Relationship in SQL&quot;,&quot;link&quot;:&quot;implementing-a-many-to-many-relationship-in-sql&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;Many-to-Many Relationships (6-min video)&quot;,&quot;text&quot;:&quot;Many-to-Many Relationships (6-min video)&quot;,&quot;link&quot;:&quot;many-to-many-relationships-6-min-video&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;Step 1: Creating the Event and Participant Tables&quot;,&quot;text&quot;:&quot;Step 1: Creating the Event and Participant Tables&quot;,&quot;link&quot;:&quot;step-1-creating-the-event-and-participant-tables&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;Step 2: Creating the EventParticipant Join Table&quot;,&quot;text&quot;:&quot;Step 2: Creating the EventParticipant Join Table&quot;,&quot;link&quot;:&quot;step-2-creating-the-eventparticipant-join-table&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;Using Five to Create MySQL Databases&quot;,&quot;text&quot;:&quot;Using Five to Create MySQL Databases&quot;,&quot;link&quot;:&quot;using-five-to-create-mysql-databases&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;1. Visually Create Tables in Five&quot;,&quot;text&quot;:&quot;1. Visually Create Tables in Five&quot;,&quot;link&quot;:&quot;1-visually-create-tables-in-five&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;2. Let Five Auto-Generate Primary Keys&quot;,&quot;text&quot;:&quot;2. Let Five Auto-Generate Primary Keys&quot;,&quot;link&quot;:&quot;2-let-five-auto-generate-primary-keys&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;3. Use Five to Create Table Relationships&quot;,&quot;text&quot;:&quot;3. Use Five to Create Table Relationships&quot;,&quot;link&quot;:&quot;3-use-five-to-create-table-relationships&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;4. Visually Inspect Your Database Schema &quot;,&quot;text&quot;:&quot;4. Visually Inspect Your Database Schema &quot;,&quot;link&quot;:&quot;4-visually-inspect-your-database-schema&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;Expansion: Build a Web App on a MySQL Database&quot;,&quot;text&quot;:&quot;Expansion: Build a Web App on a MySQL Database&quot;,&quot;link&quot;:&quot;expansion-build-a-web-app-on-a-mysql-database&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;Recap: How to Implement Many-to-Many Relationships in SQL&quot;,&quot;text&quot;:&quot;Recap: How to Implement Many-to-Many Relationships in SQL&quot;,&quot;link&quot;:&quot;recap-how-to-implement-many-to-many-relationships-in-sql&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;FAQ&quot;,&quot;text&quot;:&quot;FAQ&quot;,&quot;link&quot;:&quot;faq&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;What is a many-to-many relationship in SQL?&quot;,&quot;text&quot;:&quot;What is a many-to-many relationship in SQL?&quot;,&quot;link&quot;:&quot;what-is-a-many-to-many-relationship-in-sql&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;How do you implement a many-to-many relationship in SQL?&quot;,&quot;text&quot;:&quot;How do you implement a many-to-many relationship in SQL?&quot;,&quot;link&quot;:&quot;how-do-you-implement-a-many-to-many-relationship-in-sql&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;Why is a junction table required for many-to-many relationships?&quot;,&quot;text&quot;:&quot;Why is a junction table required for many-to-many relationships?&quot;,&quot;link&quot;:&quot;why-is-a-junction-table-required-for-many-to-many-relationships&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;Can a junction table store additional information?&quot;,&quot;text&quot;:&quot;Can a junction table store additional information?&quot;,&quot;link&quot;:&quot;can-a-junction-table-store-additional-information&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;What are common use cases for many-to-many relationships?&quot;,&quot;text&quot;:&quot;What are common use cases for many-to-many relationships?&quot;,&quot;link&quot;:&quot;what-are-common-use-cases-for-many-to-many-relationships&quot;}]\" data-visible=\"[true,true,true,true,true,true]\" data-delete-headers=\"[{&quot;label&quot;:&quot;Learn How to Implement Many-To-Many Relationships With an Easy Example&quot;,&quot;value&quot;:&quot;learn-how-to-implement-many-to-many-relationships-with-an-easy-example&quot;,&quot;isDelete&quot;:true},{&quot;label&quot;:&quot;Implement a Many-to-Many Relationship in a Web Application&quot;,&quot;value&quot;:&quot;implement-a-many-to-many-relationship-in-a-web-application&quot;,&quot;isDelete&quot;:true},{&quot;label&quot;:&quot;Understanding Many-to-Many Relationships&quot;,&quot;value&quot;:&quot;understanding-many-to-many-relationships&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Implementing a Many-to-Many Relationship in SQL&quot;,&quot;value&quot;:&quot;implementing-a-many-to-many-relationship-in-sql&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Many-to-Many Relationships (6-min video)&quot;,&quot;value&quot;:&quot;many-to-many-relationships-6-min-video&quot;,&quot;isDelete&quot;:true},{&quot;label&quot;:&quot;Step 1: Creating the Event and Participant Tables&quot;,&quot;value&quot;:&quot;step-1-creating-the-event-and-participant-tables&quot;,&quot;isDelete&quot;:true},{&quot;label&quot;:&quot;Step 2: Creating the EventParticipant Join Table&quot;,&quot;value&quot;:&quot;step-2-creating-the-eventparticipant-join-table&quot;,&quot;isDelete&quot;:true},{&quot;label&quot;:&quot;Using Five to Create MySQL Databases&quot;,&quot;value&quot;:&quot;using-five-to-create-mysql-databases&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;1. Visually Create Tables in Five&quot;,&quot;value&quot;:&quot;1-visually-create-tables-in-five&quot;,&quot;isDelete&quot;:true},{&quot;label&quot;:&quot;2. Let Five Auto-Generate Primary Keys&quot;,&quot;value&quot;:&quot;2-let-five-auto-generate-primary-keys&quot;,&quot;isDelete&quot;:true},{&quot;label&quot;:&quot;3. Use Five to Create Table Relationships&quot;,&quot;value&quot;:&quot;3-use-five-to-create-table-relationships&quot;,&quot;isDelete&quot;:true},{&quot;label&quot;:&quot;4. Visually Inspect Your Database Schema &quot;,&quot;value&quot;:&quot;4-visually-inspect-your-database-schema&quot;,&quot;isDelete&quot;:true},{&quot;label&quot;:&quot;Expansion: Build a Web App on a MySQL Database&quot;,&quot;value&quot;:&quot;expansion-build-a-web-app-on-a-mysql-database&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Recap: How to Implement Many-to-Many Relationships in SQL&quot;,&quot;value&quot;:&quot;recap-how-to-implement-many-to-many-relationships-in-sql&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;FAQ&quot;,&quot;value&quot;:&quot;faq&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;What is a many-to-many relationship in SQL?&quot;,&quot;value&quot;:&quot;what-is-a-many-to-many-relationship-in-sql&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;How do you implement a many-to-many relationship in SQL?&quot;,&quot;value&quot;:&quot;how-do-you-implement-a-many-to-many-relationship-in-sql&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Why is a junction table required for many-to-many relationships?&quot;,&quot;value&quot;:&quot;why-is-a-junction-table-required-for-many-to-many-relationships&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Can a junction table store additional information?&quot;,&quot;value&quot;:&quot;can-a-junction-table-store-additional-information&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;What are common use cases for many-to-many relationships?&quot;,&quot;value&quot;:&quot;what-are-common-use-cases-for-many-to-many-relationships&quot;,&quot;isDelete&quot;:false}]\" data-smooth=\"true\" data-top-offset=\"\"><div class=\"eb-toc__list-wrap\"><ul class=\"eb-toc__list\"><li><a href=\"#understanding-many-to-many-relationships\">Understanding Many-to-Many Relationships<\/a><li><a href=\"#implementing-a-many-to-many-relationship-in-sql\">Implementing a Many-to-Many Relationship in SQL<\/a><li><a href=\"#using-five-to-create-mysql-databases\">Using Five to Create MySQL Databases<\/a><li><a href=\"#expansion-build-a-web-app-on-a-mysql-database\">Expansion: Build a Web App on a MySQL Database<\/a><li><a href=\"#recap-how-to-implement-many-to-many-relationships-in-sql\">Recap: How to Implement Many-to-Many Relationships in SQL<\/a><li><a href=\"#faq\">FAQ<\/a><ul class=\"eb-toc__list\"><li><a href=\"#what-is-a-many-to-many-relationship-in-sql\">What is a many-to-many relationship in SQL?<\/a><li><a href=\"#how-do-you-implement-a-many-to-many-relationship-in-sql\">How do you implement a many-to-many relationship in SQL?<\/a><li><a href=\"#why-is-a-junction-table-required-for-many-to-many-relationships\">Why is a junction table required for many-to-many relationships?<\/a><li><a href=\"#can-a-junction-table-store-additional-information\">Can a junction table store additional information?<\/a><li><a href=\"#what-are-common-use-cases-for-many-to-many-relationships\">What are common use cases for many-to-many relationships?<\/a><\/li><\/ul><\/ul><\/div><\/div><\/div><\/div><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"understanding-many-to-many-relationships\">Understanding Many-to-Many Relationships<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The real world is full of many-to-many relationships. Consequently, implementing this relationship in a database design is a common task for database administrators or anyone working with databases.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, let&#8217;s say we have been given the task to build an Event Management System. The system is supposed to let us store data about events and participants:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Events<\/strong> have an event name, location, and date, and <\/li>\n\n\n\n<li><strong>Participants<\/strong> have names, email addresses, and a company.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">This is a classic example of a many-to-many or N:N relationship: one event can have many participants. And one participant can attend many events. Our hypothetical participant John Doe, who likes to party, attended both Coachella and Burning Man. And so did his wife, Jane Doe. Each participant (John and Jane) attended many events. And each event had many participants. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">How do we implement this in our database schema? Let&#8217;s get started by thinking about our tables and entity relationship diagram.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"implementing-a-many-to-many-relationship-in-sql\">Implementing a Many-to-Many Relationship in SQL<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To implement a many-to-many relationship we need something called a <strong>join table<\/strong>. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A join table has exactly one purpose: it joins the two tables that have a many-to-many relationship. It stores all of the information describing the many-to-many relationship. In our case, it keeps track of every event and participant by referencing each event&#8217;s and participant&#8217;s primary keys. <\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading has-text-align-center\" id=\"many-to-many-relationships-6-min-video\">Many-to-Many Relationships (6-min video)<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Watch the video below to create a many-to-many relationship, using Five, a SQL-based low-code development environment that lets you rapidly develop and deploy database applications.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe title=\"How to Create a Many-to-Many Relationship in Five | Low-Code For Real Developers | Five.Co\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/5K8KmZjXNNs?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">If you don&#8217;t feel like watching the video, follow the steps below to create a many-to-many relationship between two tables. Let&#8217;s begin and create our database.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"step-1-creating-the-event-and-participant-tables\">Step 1: Creating the Event and Participant Tables<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">First, create your Event and Participant table. As described above, an event has a name, location, and date. A participant has a name, email, and company.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s what our tables look like:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img width=\"1024\" height=\"494\" src=\"https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2023\/10\/image-13-1024x494.png\" alt=\"Five.Co - The Event and Participant Table\" class=\"wp-image-1829\" srcset=\"https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2023\/10\/image-13-1024x494.png 1024w, https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2023\/10\/image-13-300x145.png 300w, https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2023\/10\/image-13-768x370.png 768w, https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2023\/10\/image-13-1536x741.png 1536w, https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2023\/10\/image-13.png 1682w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">What&#8217;s the most important field in each one of these tables? <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It&#8217;s the <strong>Primary Key<\/strong>, which in our database schema is marked by the little key icon. A primary key is a unique identifier of each record inside your table. In the Event table, the primary key is the EventKey. In the Participant table, it&#8217;s the ParticipantKey.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The primary key is what we&#8217;re referencing in our join table, which we will create in the next steps. And if you require, here&#8217;s the SQL statement to create the two tables above:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282c34\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"-- Create a new database (if it doesn't exist)\nCREATE DATABASE IF NOT EXISTS YourDatabaseName;\n\n-- Use the newly created database\nUSE YourDatabaseName;\n\n-- Create the Event table\nCREATE TABLE IF NOT EXISTS Event (\n    EventKey INT PRIMARY KEY AUTO_INCREMENT,\n    name VARCHAR(255),\n    date DATE,\n    location VARCHAR(255)\n);\n\n-- Create the Participant table\nCREATE TABLE IF NOT EXISTS Participant (\n    ParticipantKey INT PRIMARY KEY AUTO_INCREMENT,\n    name VARCHAR(255),\n    company VARCHAR(255),\n    email VARCHAR(255)\n);\" style=\"color:#abb2bf;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki one-dark-pro\" style=\"background-color: #282c34\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- Create a new database (if it doesn&#39;t exist)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C678DD\">CREATE<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">DATABASE<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #61AFEF\">IF<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">NOT<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">EXISTS<\/span><span style=\"color: #ABB2BF\"> YourDatabaseName;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- Use the newly created database<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C678DD\">USE<\/span><span style=\"color: #ABB2BF\"> YourDatabaseName;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- Create the Event table<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C678DD\">CREATE<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">TABLE<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #61AFEF\">IF<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">NOT<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">EXISTS<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">Event<\/span><span style=\"color: #ABB2BF\"> (<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">    EventKey <\/span><span style=\"color: #C678DD\">INT<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">PRIMARY KEY<\/span><span style=\"color: #ABB2BF\"> AUTO_INCREMENT,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">    <\/span><span style=\"color: #C678DD\">name<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">VARCHAR<\/span><span style=\"color: #ABB2BF\">(<\/span><span style=\"color: #D19A66\">255<\/span><span style=\"color: #ABB2BF\">),<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">    <\/span><span style=\"color: #C678DD\">date<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">DATE<\/span><span style=\"color: #ABB2BF\">,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">    <\/span><span style=\"color: #C678DD\">location<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">VARCHAR<\/span><span style=\"color: #ABB2BF\">(<\/span><span style=\"color: #D19A66\">255<\/span><span style=\"color: #ABB2BF\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">);<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- Create the Participant table<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C678DD\">CREATE<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">TABLE<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #61AFEF\">IF<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">NOT<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">EXISTS<\/span><span style=\"color: #ABB2BF\"> Participant (<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">    ParticipantKey <\/span><span style=\"color: #C678DD\">INT<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">PRIMARY KEY<\/span><span style=\"color: #ABB2BF\"> AUTO_INCREMENT,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">    <\/span><span style=\"color: #C678DD\">name<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">VARCHAR<\/span><span style=\"color: #ABB2BF\">(<\/span><span style=\"color: #D19A66\">255<\/span><span style=\"color: #ABB2BF\">),<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">    company <\/span><span style=\"color: #C678DD\">VARCHAR<\/span><span style=\"color: #ABB2BF\">(<\/span><span style=\"color: #D19A66\">255<\/span><span style=\"color: #ABB2BF\">),<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">    email <\/span><span style=\"color: #C678DD\">VARCHAR<\/span><span style=\"color: #ABB2BF\">(<\/span><span style=\"color: #D19A66\">255<\/span><span style=\"color: #ABB2BF\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">);<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"step-2-creating-the-event-participant-join-table\">Step 2: Creating the EventParticipant Join Table<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Now, let&#8217;s create our join table. The join table has exactly three fields: its own primary key, and the EventKey and ParticipantKey as foreign keys from our Event and Participant table. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s what our database schema will look like with the join table established:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img width=\"1024\" height=\"350\" src=\"https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2023\/10\/image-14-1024x350.png\" alt=\"Five.Co - For a many-to-many relationship a join table is required\" class=\"wp-image-1830\" srcset=\"https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2023\/10\/image-14-1024x350.png 1024w, https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2023\/10\/image-14-300x102.png 300w, https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2023\/10\/image-14-768x262.png 768w, https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2023\/10\/image-14-1536x525.png 1536w, https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2023\/10\/image-14.png 2000w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">Note that our join table has the name EventParticipant: this is easy to understand and descriptive. Usually, join tables follow this naming convention of using the names of the two tables represented in it. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Secondly, our join table has a one-to-many relationship to both Participant and Event. This ensures that whenever a participant joins an event, a new record is created in our join table. In this way, the table keeps track of all events and their participants. A many-to-many relationship is effectively just two one-to-many relationships.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Lastly, also note that the join table does not store any redundant information. It only stores what it needs to know: the primary keys of participants and events. Now, if you would like to write a query such as &#8220;Which events did John Doe participate in?&#8221;, you can <\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>identify John through his ParticipantKey in your Participant table, <\/li>\n\n\n\n<li>identify all events that John participated in from the EventParticipant table. This will enable you to get access to each event&#8217;s EventKey, and<\/li>\n\n\n\n<li>get additional information about each event from the Event table using the EventKey.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">To create this table and its relationships, use this SQL statement:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282c34\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"-- Create the Join Table to establish a many-to-many relationship\nCREATE TABLE EventParticipant (\n    PRIMARY KEY (EventID, ParticipantID),\n    FOREIGN KEY (EventID) REFERENCES Event(EventKey),\n    FOREIGN KEY (ParticipantID) REFERENCES Participant(ParticipantKey)\n);\" style=\"color:#abb2bf;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki one-dark-pro\" style=\"background-color: #282c34\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #7F848E; font-style: italic\">-- Create the Join Table to establish a many-to-many relationship<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C678DD\">CREATE<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">TABLE<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #61AFEF\">EventParticipant<\/span><span style=\"color: #ABB2BF\"> (<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">    <\/span><span style=\"color: #C678DD\">PRIMARY KEY<\/span><span style=\"color: #ABB2BF\"> (EventID, ParticipantID),<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">    <\/span><span style=\"color: #C678DD\">FOREIGN KEY<\/span><span style=\"color: #ABB2BF\"> (EventID) <\/span><span style=\"color: #C678DD\">REFERENCES<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">Event<\/span><span style=\"color: #ABB2BF\">(EventKey),<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">    <\/span><span style=\"color: #C678DD\">FOREIGN KEY<\/span><span style=\"color: #ABB2BF\"> (ParticipantID) <\/span><span style=\"color: #C678DD\">REFERENCES<\/span><span style=\"color: #ABB2BF\"> Participant(ParticipantKey)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">);<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">You&#8217;re already done: to implement your many-to-many relationship, you have created three tables that each store unique records: one in relation to your events, one in relation to your participants, and one that puts them all together by storing the Primary Keys of Participants and Events in a join table.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The logic behind setting up a many-to-many relationship in this way is pretty simple: we can now retrieve from our join table a record. Said record will guide us toward our participant table via the ParticipantKey. And it will also guide us toward our event table using the EventKey. In short, the join table gives us just the right amount of information to match participants with events and vice versa.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"using-five-to-create-my-sql-databases\">Using Five to Create MySQL Databases<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Five is an easy-to-use database application builder that comes with a graphical interface for creating a MySQL database. For free access, <a href=\"https:\/\/preview-php85.dzine.org\/five\/get-started\/\" target=\"_blank\" rel=\"noreferrer noopener\">sign up here<\/a>. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Five makes it easy to create a MySQL database because of its powerful, visual database features. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Creating the three tables to create a database with a many-to-many relationship can accomplished in Five with just a few clicks!<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-visually-create-tables-in-five\">1. Visually Create Tables in Five<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Five lets users create database tables visually and without writing SQL. Simply download Five, create a new app, and go to <strong>Data &gt; Table Wizard.<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Using Five&#8217;s <strong>Table Wizard,<\/strong> creating the Event table, is as easy as adding fields and defining their data type. Five supports all standard SQL data types, from binary to boolean to string.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In short, Five&#8217;s visual table wizard replaces the tedious task of having to write out everything in SQL. It replaces CREATE statements with an intuitive table designer that covers all common SQL data types.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s what creating the Event table looks like in Five. We are adding three fields to the table: Name, Date, and Location. <\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img width=\"1024\" height=\"328\" src=\"https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2023\/10\/image-15-1024x328.png\" alt=\"Five lets users create database tables visually\" class=\"wp-image-1834\" srcset=\"https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2023\/10\/image-15-1024x328.png 1024w, https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2023\/10\/image-15-300x96.png 300w, https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2023\/10\/image-15-768x246.png 768w, https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2023\/10\/image-15-1536x492.png 1536w, https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2023\/10\/image-15.png 2000w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-let-five-auto-generate-primary-keys\">2. Let Five Auto-Generate Primary Keys<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Note how in the screenshot above, there are only three fields being added to my Event table. But where&#8217;s our fourth field, the primary key? <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Five will automatically add a primary key to a newly created table. You don&#8217;t have to worry about doing so yourself. Given the importance of primary keys for database design, this is a great time saver for developers. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Primary keys generated by Five are GUIDs, and the chance of ever having two primary keys being the same is virtually zero.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-use-five-to-create-table-relationships\">3. Use Five to Create Table Relationships<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Creating relationships between tables is also really easy in Five.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When creating a new table using the Table Wizard just mentioned, Five will ask you whether you&#8217;d like to establish any relationships between tables. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Simply select the tables that your new table is related to, and Five will insert a Foreign Key for you. This comes in very handy when creating a join table, which has two one-to-many relations!<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-visually-inspect-your-database-schema\">4. Visually Inspect Your Database Schema <\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Your Entity-Relationship Diagram can be viewed in Five, using Five&#8217;s <strong>visual database modeler<\/strong>. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This feature gives you a visual representation of your database, its tables, and relationships. If you&#8217;re ever in doubt about your database structure, Five&#8217;s database modeler is a handy tool to figure out how different tables are related to each other. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">All of the screenshots shown above of the Event, Participant, and EventParticipant join table were created using Five&#8217;s database modeler.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img width=\"1024\" height=\"702\" src=\"https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2023\/07\/use-cases-database-modeler-1024x702.jpg\" alt=\"Five.Co - Database Modeler\" class=\"wp-image-1328\" srcset=\"https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2023\/07\/use-cases-database-modeler-1024x702.jpg 1024w, https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2023\/07\/use-cases-database-modeler-300x206.jpg 300w, https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2023\/07\/use-cases-database-modeler-768x527.jpg 768w, https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2023\/07\/use-cases-database-modeler-1536x1053.jpg 1536w, https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2023\/07\/use-cases-database-modeler-2048x1405.jpg 2048w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">Overall, Five makes SQL simple, easy, and accessible: you don&#8217;t need to struggle with queries, complicated setups, or credentials. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/preview-php85.dzine.org\/five\/get-started\/\">Sign up for a free trial now<\/a> and create your database, its tables, and their relationships visually. <\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<h2 class=\"wp-block-heading\" id=\"expansion-build-a-web-app-on-a-my-sql-database\">Expansion: Build a Web App on a MySQL Database<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Learn how to build an entire web app on a SQL database by following our <a href=\"https:\/\/preview-php85.dzine.org\/five\/code-along\/code-along-full-stack-web-app\/\" target=\"_blank\" data-type=\"link\" data-id=\"https:\/\/preview-php85.dzine.org\/five\/code-along\/code-along-full-stack-web-app\/\" rel=\"noreferrer noopener\">step-by-step app development guide<\/a>. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The guide starts with the creation of a database from scratch and finishes with a web app that contains a chart, PDF reports, and forms for users to interact with your data. You will learn how to combine all these front-end elements with your back-end database.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Best of all: it&#8217;s free to follow!<\/p>\n\n\n\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-fe48e5de wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link has-white-color has-vivid-cyan-blue-background-color has-text-color has-background has-link-color wp-element-button\" href=\"https:\/\/preview-php85.dzine.org\/five\/code-along\/code-along-full-stack-web-app\/\" target=\"_blank\" rel=\"noreferrer noopener\">Learn How to Build a Web App<\/a><\/div>\n<\/div>\n<\/div><\/div>\n<\/div><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"recap-how-to-implement-many-to-many-relationships-in-sql\">Recap: How to Implement Many-to-Many Relationships in SQL<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s our recap: many-to-many relationships are part and parcel of good relational database design. To implement a many-to-many relationship between two tables, create a new table containing the primary key of each table. Remember: many-to-many relationships are just two <a href=\"https:\/\/preview-php85.dzine.org\/five\/blog\/how-to-create-one-to-many-relationships-in-sql\/\" data-type=\"post\" data-id=\"2373\">one-to-many relationships<\/a>!<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<h2 class=\"wp-block-heading\">FAQ<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<ol class=\"rank-math-list \">\n<li id=\"faq-question-1771985858796\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong><strong><strong>What is a many-to-many relationship in SQL?<\/strong><\/strong><\/strong><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>A many-to-many relationship occurs when multiple records in one table can relate to multiple records in another table, such as students enrolling in many courses while each course has many students.<\/p>\n\n<\/div>\n<\/li>\n<li id=\"faq-question-1771985909936\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong><strong><strong><strong>How do you implement a many-to-many relationship in SQL?<\/strong><\/strong><\/strong><\/strong><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>You implement it by creating a <strong>junction (or join) table<\/strong> that contains foreign keys referencing the primary keys of both related tables. This table stores each relationship pair and connects the two datasets.<\/p>\n\n<\/div>\n<\/li>\n<li id=\"faq-question-1771985985770\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong><strong><strong>Why is a junction table required for many-to-many relationships?<\/strong><\/strong><\/strong><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Relational databases typically cannot represent many-to-many relationships directly, so the junction table breaks the relationship into two one-to-many relationships, ensuring proper normalization and data integrity.<\/p>\n\n<\/div>\n<\/li>\n<li id=\"faq-question-1771985994514\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong><strong><strong><strong>Can a junction table store additional information?<\/strong><\/strong><\/strong><\/strong><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Yes. The article points out that junction tables can include <strong>extra columns<\/strong> such as enrollment date, status, or grades, allowing you to track more details about each relationship beyond just the IDs.<\/p>\n\n<\/div>\n<\/li>\n<li id=\"faq-question-1771986003404\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong><strong><strong><strong>What are common use cases for many-to-many relationships?<\/strong><\/strong><\/strong><\/strong><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Typical examples include students and courses, authors and books, or users and roles\u2014situations where both entities can have multiple associations with each other.<\/p>\n\n<\/div>\n<\/li>\n<\/ol>\n<\/div><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Learn How to Implement Many-To-Many Relationships With an Easy Example Relational database systems, such as MySQL, PostgreSQL, or SQLite make storing and retrieving data quick and efficient. The underlying principle of a relational database is to store data in related tables, which avoids data redundancy. Tables can be related in several ways: in a one-to-one [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":1850,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":true,"_eb_attr":"","_uag_custom_page_level_css":"","footnotes":""},"categories":[21],"tags":[],"class_list":["post-1824","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog"],"acf":[],"uagb_featured_image_src":{"full":["https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2023\/10\/Five.Co-Many-to-Many-Relationships-in-SQL-1.png",1280,720,false],"thumbnail":["https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2023\/10\/Five.Co-Many-to-Many-Relationships-in-SQL-1-150x150.png",150,150,true],"medium":["https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2023\/10\/Five.Co-Many-to-Many-Relationships-in-SQL-1-300x169.png",300,169,true],"medium_large":["https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2023\/10\/Five.Co-Many-to-Many-Relationships-in-SQL-1-768x432.png",768,432,true],"large":["https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2023\/10\/Five.Co-Many-to-Many-Relationships-in-SQL-1-1024x576.png",1024,576,true],"1536x1536":["https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2023\/10\/Five.Co-Many-to-Many-Relationships-in-SQL-1.png",1280,720,false],"2048x2048":["https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2023\/10\/Five.Co-Many-to-Many-Relationships-in-SQL-1.png",1280,720,false]},"uagb_author_info":{"display_name":"Dominik Keller","author_link":"https:\/\/preview-php85.dzine.org\/five\/author\/f1v5_ed3_my\/"},"uagb_comment_info":0,"uagb_excerpt":"Learn How to Implement Many-To-Many Relationships With an Easy Example Relational database systems, such as MySQL, PostgreSQL, or SQLite make storing and retrieving data quick and efficient. The underlying principle of a relational database is to store data in related tables, which avoids data redundancy. Tables can be related in several ways: in a one-to-one&hellip;","_links":{"self":[{"href":"https:\/\/preview-php85.dzine.org\/five\/wp-json\/wp\/v2\/posts\/1824","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/preview-php85.dzine.org\/five\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/preview-php85.dzine.org\/five\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/preview-php85.dzine.org\/five\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/preview-php85.dzine.org\/five\/wp-json\/wp\/v2\/comments?post=1824"}],"version-history":[{"count":0,"href":"https:\/\/preview-php85.dzine.org\/five\/wp-json\/wp\/v2\/posts\/1824\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/preview-php85.dzine.org\/five\/wp-json\/wp\/v2\/media\/1850"}],"wp:attachment":[{"href":"https:\/\/preview-php85.dzine.org\/five\/wp-json\/wp\/v2\/media?parent=1824"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/preview-php85.dzine.org\/five\/wp-json\/wp\/v2\/categories?post=1824"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/preview-php85.dzine.org\/five\/wp-json\/wp\/v2\/tags?post=1824"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}