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