1
00:00:00,210 --> 00:00:08,100
All right moving on to likes so we've seen now users photos and comments and likes are a little bit

2
00:00:08,100 --> 00:00:15,750
similar they are like the comment schema because when we think about how likes work on Instagram, at

3
00:00:15,750 --> 00:00:22,230
least you like something you like the particular photo, let's say its corresponding to that photo.

4
00:00:22,230 --> 00:00:24,030
We need to have an association there.

5
00:00:24,390 --> 00:00:33,330
But then there's also the association between me, the person liking it and the like itself and that

6
00:00:33,330 --> 00:00:36,810
might not matter if we allowed for infinite likes.

7
00:00:36,810 --> 00:00:41,010
You know, if anybody could like something as many times as they wanted, just it was a button that

8
00:00:41,010 --> 00:00:42,600
clicked and it just kept going.

9
00:00:42,600 --> 00:00:48,450
Every time I clicked it, we could get away with not storing user information with a like.

10
00:00:48,750 --> 00:00:53,790
But even then it might be nice to see how many times somebody had like something or how many likes,

11
00:00:53,790 --> 00:00:59,190
you know, I in general, how often do I click the like button compared to somebody else?

12
00:00:59,280 --> 00:01:02,790
It might be it might help us figure out who's a bot like on Instagram.

13
00:01:02,790 --> 00:01:04,050
That's a big problem, actually.

14
00:01:04,050 --> 00:01:11,460
Are these bots that people write that go in just like photos, like things all the time, create comments

15
00:01:11,460 --> 00:01:12,720
that are just cookie cutter?

16
00:01:13,230 --> 00:01:18,060
It's kind of frustrating actually, if you're a photographer because you know, you'll post something

17
00:01:18,060 --> 00:01:22,680
and then you get excited that you have, you know, a hundred people who liked it.

18
00:01:22,740 --> 00:01:24,540
And then it turns out a lot of them are.

19
00:01:24,540 --> 00:01:28,530
But if you actually go try and look at them, they don't have any photos, posts, really.

20
00:01:28,710 --> 00:01:30,680
They always comment the same thing.

21
00:01:30,690 --> 00:01:38,040
Long story short, in this giant data set here, I actually coded in some bots that will be working

22
00:01:38,040 --> 00:01:42,870
with basically people who aren't who aren't creating much stuff.

23
00:01:42,870 --> 00:01:47,850
They're not posting many photos, but they are liking a lot of things in commenting a lot of the same

24
00:01:47,850 --> 00:01:49,290
thing a lot of the time.

25
00:01:49,290 --> 00:01:57,330
Anyway, back to likes it's going to look like this will have a user ID and a photo ID and a created

26
00:01:57,330 --> 00:01:57,680
app.

27
00:01:57,810 --> 00:02:06,720
So our user ID is going to refer to a user's ID and a photo ID refers to a photo ID and there's really

28
00:02:06,720 --> 00:02:07,650
nothing else.

29
00:02:07,860 --> 00:02:10,080
There's no information being stored there.

30
00:02:10,080 --> 00:02:14,490
Maybe, you know, aside from the date or the time it was created when the like happened.

31
00:02:14,910 --> 00:02:16,920
But what else is there to store?

32
00:02:16,950 --> 00:02:22,920
You know, maybe Instagram is doing stuff like whether it was liked from the Web version versus the

33
00:02:22,920 --> 00:02:24,090
iOS version.

34
00:02:24,090 --> 00:02:26,700
Maybe they're storing other information here.

35
00:02:26,700 --> 00:02:27,510
Possibly.

36
00:02:27,510 --> 00:02:29,850
But for us, this is the basics.

37
00:02:29,850 --> 00:02:35,580
We just need a user ID and a photo ID and every time you know, a photo is liked by a different user,

38
00:02:35,580 --> 00:02:40,290
we get a new like just like this with the user ID and photo ID.

39
00:02:40,290 --> 00:02:41,910
So we'll start by coding this.

40
00:02:41,910 --> 00:02:45,360
You might have noticed that I didn't add an ID to likes.

41
00:02:45,510 --> 00:02:47,190
I'll explain why in just a second.

42
00:02:47,190 --> 00:02:50,550
But let's start by creating our table.

43
00:02:50,760 --> 00:02:56,910
So we have create table likes and don't forget the semicolon.

44
00:02:56,910 --> 00:03:01,670
We've got a user ID and a photo ID and are created at.

45
00:03:02,040 --> 00:03:03,660
Let's start with create that.

46
00:03:03,900 --> 00:03:05,400
We often neglect it.

47
00:03:05,400 --> 00:03:10,440
Timestamp default now nice and easy.

48
00:03:10,830 --> 00:03:12,970
User ID is just an integer.

49
00:03:13,100 --> 00:03:15,870
I don't want it to be empty.

50
00:03:16,860 --> 00:03:23,010
Same thing for photo id, but then we need to set up our foreign key constraints.

51
00:03:23,790 --> 00:03:29,040
Foreign key user id references.

52
00:03:29,040 --> 00:03:40,860
Users ID comma another foreign key this time photo ID references photos ID.

53
00:03:42,570 --> 00:03:47,010
Okay, so now let's address why we don't have an ID here.

54
00:03:47,280 --> 00:03:50,280
And the main reason is that we don't need an ID.

55
00:03:50,310 --> 00:03:53,430
We're not going to be referring to likes anywhere else.

56
00:03:53,850 --> 00:03:57,360
If we were, it would be good to add an ID there.

57
00:03:57,360 --> 00:04:01,320
Maybe, but we're not having any table store information about likes.

58
00:04:01,320 --> 00:04:08,520
The other thing you might be wondering about is how do we ensure that there's only one like per user?

59
00:04:08,520 --> 00:04:10,200
Is there a way to do that in the database?

60
00:04:10,200 --> 00:04:17,700
Because right now, if I did this, I could just insert let's say my user ID is one and a photo ID is

61
00:04:17,700 --> 00:04:18,240
one.

62
00:04:18,240 --> 00:04:23,520
I could insert a like with the user ID one, photo ID one, and then insert it again and again and again.

63
00:04:23,520 --> 00:04:25,140
And there's nothing stopping that.

64
00:04:25,140 --> 00:04:30,510
So what we really want to say is we want a unique combination of the two.

65
00:04:30,510 --> 00:04:35,510
There can only be one instance of every combination, and there's actually a way of saying that and

66
00:04:35,550 --> 00:04:36,480
we haven't seen it.

67
00:04:36,480 --> 00:04:44,520
But you can do this primary key and then we can just say user ID, comma, photo ID.

68
00:04:45,600 --> 00:04:52,770
So what this will do is basically not allow you to insert two likes that are exactly the same, the

69
00:04:52,770 --> 00:04:54,930
same user ID and the same photo ID.

70
00:04:55,590 --> 00:04:59,550
And I'll demonstrate that if you feel confident, if you trust me, there you can.

71
00:04:59,750 --> 00:05:00,080
Skip.

72
00:05:00,080 --> 00:05:00,770
Move on.

73
00:05:00,830 --> 00:05:02,330
We're done with the schema here.

74
00:05:02,360 --> 00:05:04,190
Let's just make sure that it runs.

75
00:05:04,220 --> 00:05:05,980
I don't have any syntax errors.

76
00:05:05,990 --> 00:05:08,960
Of course it does.

77
00:05:08,960 --> 00:05:13,610
But if you'd like to see me try it out like the last couple of videos, I'll do that now.

78
00:05:14,000 --> 00:05:20,240
So if you're still with me, all I'm going to do is take these photos and these users, and I'm going

79
00:05:20,240 --> 00:05:22,100
to have some of them like them.

80
00:05:22,100 --> 00:05:28,340
So let's take blue user ID one and that first photo, she created it.

81
00:05:28,340 --> 00:05:29,180
That doesn't matter.

82
00:05:29,180 --> 00:05:31,040
You can like your own photo on Instagram.

83
00:05:31,190 --> 00:05:33,290
So we'll insert into likes.

84
00:05:39,600 --> 00:05:46,740
And we have user ID come a photo ID, so the first one will be one comma one.

85
00:05:47,580 --> 00:05:53,970
So I'm inserting a blue is liking this photo and then let's have Charlie Brown also like it.

86
00:05:53,970 --> 00:05:58,620
So that would be a oops to come on one.

87
00:05:59,610 --> 00:06:05,760
And then we've got the next photo here which has a photo ID of two, not this.

88
00:06:05,760 --> 00:06:10,230
Remember, this is the user ID who created it, but this has an idea of two.

89
00:06:10,500 --> 00:06:12,840
So let's say Blue also likes that one.

90
00:06:12,840 --> 00:06:19,680
So blue ID of one likes photo ID of two and she likes photo ID of three.

91
00:06:19,680 --> 00:06:26,700
And then let's say I have user ID three, I like photo with ID of three.

92
00:06:29,010 --> 00:06:30,420
So let's start there.

93
00:06:32,640 --> 00:06:33,120
We run it.

94
00:06:33,120 --> 00:06:33,960
It works.

95
00:06:34,380 --> 00:06:36,240
There shouldn't be any problems at this point.

96
00:06:36,240 --> 00:06:45,900
But now if I try and do this insert into likes, user ID, a photo ID values and I do one comma one.

97
00:06:45,900 --> 00:06:47,130
Well, that's not that.

98
00:06:47,130 --> 00:06:47,580
There we go.

99
00:06:47,580 --> 00:06:48,450
One, come on, one.

100
00:06:48,450 --> 00:06:52,710
Which already is in there, blue user ID one.

101
00:06:53,340 --> 00:06:58,770
And then this photo adds blah blah blah 76 with ID of one.

102
00:06:58,950 --> 00:07:00,300
We already have that like.

103
00:07:00,300 --> 00:07:06,000
But if I try it you can see I get duplicate entry 1-14 key primary.

104
00:07:06,090 --> 00:07:11,640
So the way it does it is basically combines it to keep track of the combination like that with the dash

105
00:07:11,640 --> 00:07:12,240
between it.

106
00:07:12,240 --> 00:07:13,740
So that's pretty much it.

107
00:07:14,010 --> 00:07:18,270
I will copy what we just did and add a note here.

108
00:07:18,960 --> 00:07:29,760
If you're looking at these notes later, just saying won't work because of primary key constraint and

109
00:07:29,760 --> 00:07:30,780
I'll comment it out as well.

110
00:07:30,780 --> 00:07:32,190
We don't want to run that every time.

111
00:07:32,400 --> 00:07:33,030
Great.

112
00:07:33,030 --> 00:07:38,970
And next up, we're moving on to a fun one, which is relationships following followers, all that stuff.

