So we've established that in real life, data can be crazy complex.
Outside of the classroom, things aren't as nice as we'd like them to be.
So we've established that fact.
But now where do we start to kind of wrap our heads around how we represent complex data using MySQL?
And the answer is we're going to start by talking about the relationship basics.
So I know it sounds like Marriage Counseling 101 or something, but this is not it's not that this is
about the different ways that data can be related.
So if we have two tables or two entities, things like orders and customers and students and teachers,
movies and reviews and users, how, how are they related?
And there's a couple of different ways.
There's kind of these big, broad categories we can break relationships down into.
And the terms that I'm going to use that you'll occasionally come across are three.
There's the first one, which is a 1 to 1 relationship, and I'm just going to go through all of them
first before we talk about what they mean and talk about examples.
So 1 to 1 relationship, a one to many relationship, and then a many to many relationship.
So the reality is that 1 to 1 relationships aren't actually that common.
But perhaps another example or one that I've recently used is had a customers table that had the basic
information for a customer.
So things like their username and their password, although again you don't actually store the password
directly, but username and password and email and registration date.
And then there was a separate table where we stored all these details about our customer.
So rather than storing ten or 20 extra columns on the basic customer table, we decided to split it
up based off of what was most commonly needed in the app we were building.
And most commonly all we needed was the username and the password and the email, but there was a whole
bunch of other information that we were storing.
So we created a customer details table and that's a 1 to 1 relationship.
One customer has their own row, one row in the customer details table, and one customer details row
is associated with one customer.
So cold steel has a cold steel customer details, but there's no other user or customer that could be
associated with it because it's a 1 to 1 relationship.
Anyways, like I said, it's not the most common.
What is the most common is actually a one to many relationship, and that's what we'll spend the most
time on in this course because like I said, it's really common.
So here's an example.
The relationship between books and reviews.
One book can have thousands of reviews, right?
Let's say Into Thin Air by Jon Krakauer, could have probably tens of thousands of reviews by this point.
It's been out for a while.
It's pretty popular, but those reviews belong to that book exclusively to one book.
So the relationship is books have many reviews.
But reviews belong to one book.
So it's a one to many.
So that's a really common classic example.
Reviews or ratings and books or some entity.
Okay.
So then the next thing that we'll discuss here is a many to many relationship and many to many is still
relatively common and the way that it works.
So what it sounds like we have two entities in this case books and authors, and you can kind of speak
through it.
You could say, okay, books can have many authors.
That's true.
We could have two authors writing a book together, and those authors can have many books.
There's no rule that says authors can only write one book.
You know, J.K. Rowling, Rowling have already say it has ten plus books.
So it's a two way, a many to many relationship.
So hopefully you can see the difference there.
Reviews in books, reviews belong to one book.
They're associated only with one book.
Whatever the book title is for that review, it can't also be the same review associated for another
book.
So that part of the relationship is one, right?
And then we have the books to reviews, which is a many relationship.
Books have many reviews.
This is different because books can have multiple authors and authors can belong to multiple books or
authors can have multiple books.
And the way that we represent this is actually a little more complex.
So we're going to come back to it.
We're going to start off in this section by talking about the most common relationship and how we represent
that, which is the one to many relationship.
So that's coming up next.