MySQL 8 - One-to-Many Relationship

submitted by mkenny400 on 03/20/18 1

A One-to-Many relationship is when one side of the relationship is exclusive, but not the other. An example of this given in the last video was a user on a bidding website. The way this app is set up is that the user can post as many items as he wants, but an individual item listing can only be owned by one user. We could say that the listings are an attribute of the user. The incorrect way to store these is to put everything in one table, the users table. Either we will be storing multiple listings in one column, have unneeded columns, or have duplicate data from unneeded extra rows. As you can see, storing one-to-many data is not as easy as creating a column in a table. This calls for a new table. The listings table! This is going to store the data for the listing and also have a column for the owner. The owner is going to be a foreign key to the users table. When we go through an example of data that is stored in this table, you'll see that we've mitigated all of our previous problems when we were trying to store it in one table. So in conclusion, when you have a one-to-many relationships, you take the many side of the relationship, in this case listings, and create a new table for it. You then create a column that references one individual entity from the one side of the relationship, in this case users. Now an important thing for you to understand is that often the relationships in a database can be decided by either you, or the business rules of the database or the application. So in this situation, we have restricted the listings to being owned by only one user. Does it have to be this way? No. This was a decision by us. We could modify the application to allow a listing to be owned by more than one user. This would be introducing co-ownership to the listings inside of to the application. This is something we will do in the next video, so go watch it! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Support me! www.patreon.com/calebcurry Subscribe to my newsletter: eepurl.com/-8qtH Donate!: bit.ly/DonateCTVM2. ~~~~~~~~~~~~~~~Additional Links~~~~~~~~~~~~~~~ More content: CalebCurry.com Facebook: www.facebook.com/CalebTheVideoMaker Google+: plus.google.com/+CalebTheVideoMaker2 Twitter: twitter.com/calebCurry Amazing Web Hosting - www.dreamhost.com/r.cgi?1487063 (The best web hosting for a cheap price!)

Leave a comment

Be the first to comment

Collections with this video
Email
Message
×
Embed video on a website or blog
Width
px
Height
px
×
Join Huzzaz
Start collecting all your favorite videos
×
Log in
Join Huzzaz

facebook login
×
Retrieve username and password
Name
Enter your email address to retrieve your username and password
(Check your spam folder if you don't find it in your inbox)

×