All right.
Moving on to like this.
So we've seen now users photos and comments and likes are a little bit similar.
They are like the comments schema because when we think about how likes work on Instagram at least you
like something you like that particular photo.
Let's say it's corresponding to that photo.
We need to have an association there.
But then there's also the association between me the person liking it and the like itself.
And that might not matter if we allowed for infinite likes you know anybody could like something as
many times as they wanted to say it was a button that clicked and it just kept going every time I clicked
it.
We could get away with not storing user information with a like.
But even then it might be nice to see how many times somebody had like something or how many likes you
know in general.
How often do I click the Like button compared to somebody else.
It might be it might help us figure out who's a bot like on Instagram that's a big problem actually
are these bots that people write they go and just like photos like things all the time create comments
that are just cookie cutter.
It's kind of frustrating actually if you're a photographer because you know you'll post something and
then you get excited that you have you know 100 people who liked it and it turns out a lot of them are
bought.
If you actually go try and look at them they don't have any photos post really.
They always comment the same thing.
Long story short in this giant dataset here I actually coded in some bots that will be working with
basically people who aren't in search who aren't creating much stuff they're not posting many photos
but they are liking a lot of things and commenting on the same thing all the time.
Anyway back to likes.
It's going to look like this will have a user ID and a photo ID and a created app so our user ID is
going to refer to a user's ID and a photo ID refer to a photo ID and there is really nothing else.
Is no information being stored there.
You know aside from the date or the time it was created when the like happened.
But what else is there in store.
You know maybe Instagram is doing stuff like whether it was liked from the web version versus the iOS
version.
Maybe they're storing you know other information here possibly but for us this is the basics.
We just need a user ID and a photo ID and every time no photo is liked by a different user.
We get a new like just like this with the user id and photo ID.
So we'll start by coding this.
You might have noticed that I didn't add an ID to the likes.
Explain why in just a second.
But let's start by creating our table.
So we have create table like this.
And don't forget the semi-colon.
We've got a user ID and a photo ID and are created at headstart was created at we often neglect it.
Time stamp the default now.
OK.
Nice and easy.
User ID is just an integer.
I don't want it to be empty.
Same thing for photo ID.
But then we need to set up our foreign key constraints for key user id references user's ID comma and
other foreign key.
This time photo ID references photos Id.
OK so now let's address why we don't have an ID here.
And the main reason is that we don't need an ID.
We're not going to be referring to like anywhere else.
If we were it would be good to add an ID there maybe but we're not having any table store information
about likes.
The other thing you might be wondering about is how do we ensure that you know there's only one like
per user.
Is there a way to do that in the database.
Because right now if I did this I could just insert let's say my user ID is one and a photo ID is one
I could insert a like with the user id one photo ID one and then insert it again and again and again.
And there's nothing stopping that.
So what we really want to say is we want a unique combination of the two.
There can only be one instance of every combination and there's actually a way of saying that we haven't
seen it but you can do this primary key and then we can just say use your ID comma photo ID.
So what this will do is basically not allow you to insert to you know like that are exactly the same
the same user ID and the same photo ID and I'll demonstrate that if you feel confident if you trust
me there you can skip move on.
We're done with the ski mask here.
Let's just make sure that it runs.
I don't have any syntax errors of course.
OK.
It does.
But if you'd like to see me try it out like the last couple of videos.
I'll do that now.
So if you're still with me I'm going to do is take these photos and these users I'm going to have some
of them like them.
So let's take a blue user id one and that first photo she created it.
That doesn't matter.
You can like your own photo on Instagram.
So will insert into likes
and we have user id come a photo ID.
So the first one will be one coming one so I'm inserting blue is liking this photo.
And then let's have Charlie Brown also like it.
So that would be a weps to come in one.
And then we've got the next photo here which has a photo idea of two not this.
Remember this is the user who created it.
But this has an idea of two.
So let's say blue also likes that one.
So blue idea if one likes photo ID two and she likes photo ID of three.
And then what say I have used already three I like photo with idea of three.
OK.
So let's start there.
We run it.
It works.
There shouldn't be any problems at this point.
But now if I try and do this insert into likes user id come a photo ID values.
And I do one comma one.
Well that's not that.
There you go one come one which already is in there blue.
User ID one.
And then this photo.
And while Bob 5:53 with idea of one we already have that like that if I try it you can see it duplicate
entry 1 dash 1 for key primary.
So the way it does it is basically combined to to keep track of the combination like that the dash between
it.
So that's pretty much it.
I will copy what we just did an ad a note here.
If you're looking at these notes later just saying it won't work because of primary key constraint and
are commented out as well.
We don't want to run that every time.
Great.
And next up we're moving on to a fun one which is relationships following followers all this stuff.