1 00:00:00,150 --> 00:00:00,540 All right. 2 00:00:00,600 --> 00:00:02,070 So we've got one more video here. 3 00:00:02,070 --> 00:00:07,890 And this has to do with managing triggers, which sounds a bit like a chapter in a self-help book. 4 00:00:07,890 --> 00:00:14,150 But really, it has to do with how we make sure our triggers are working and mainly delete them. 5 00:00:14,160 --> 00:00:17,250 So this is where the name of our trigger comes into play. 6 00:00:17,250 --> 00:00:20,490 And then also I'll end with a quick warning about triggers. 7 00:00:20,730 --> 00:00:25,920 So the first thing that we can do is just list the triggers that exist in a database so we can just 8 00:00:25,920 --> 00:00:29,280 use show triggers to see what we have going on. 9 00:00:29,280 --> 00:00:35,790 And I'll warn you, this is not my warning I already talked about, but I'll warn you that what you 10 00:00:35,790 --> 00:00:39,960 get when you run this is kind of a mess to look at. 11 00:00:40,140 --> 00:00:45,390 But we can see we have in our Instagram data, we have two triggers prevent self follows, and then 12 00:00:45,390 --> 00:00:48,330 you can actually see the code inside of that trigger. 13 00:00:48,330 --> 00:00:51,960 And then we have captured Unfollow and you can see that code as well. 14 00:00:52,530 --> 00:00:59,790 And if I switched over to the other database, I believe it was used trigger demo. 15 00:01:01,710 --> 00:01:03,480 And I do show triggers. 16 00:01:03,480 --> 00:01:04,650 We have one here. 17 00:01:04,650 --> 00:01:07,020 This is our must be adult. 18 00:01:07,290 --> 00:01:08,880 All right, so we have that. 19 00:01:08,910 --> 00:01:12,060 I'll switch back to my Instagram clone. 20 00:01:17,080 --> 00:01:20,440 OC and do our show triggers just to make sure they're still there. 21 00:01:20,470 --> 00:01:26,020 They shouldn't go anywhere anyways, but we've got two of them, so that's pretty much it to listing 22 00:01:26,020 --> 00:01:26,490 them. 23 00:01:26,500 --> 00:01:27,310 Moving on. 24 00:01:27,550 --> 00:01:30,820 So if I want to remove a trigger, it's pretty simple. 25 00:01:31,210 --> 00:01:34,330 It's drop trigger and then the trigger name. 26 00:01:34,930 --> 00:01:36,570 So let's go back. 27 00:01:36,580 --> 00:01:39,760 Let's say I want to remove this prevent self follows. 28 00:01:39,760 --> 00:01:42,280 I want to allow users to follow themselves. 29 00:01:42,550 --> 00:01:44,050 So I want to get rid of that. 30 00:01:44,230 --> 00:01:46,690 I'll just copy the name just so I don't have to type it. 31 00:01:47,140 --> 00:01:52,990 And we just do drop trigger prevent self follows. 32 00:01:53,470 --> 00:01:56,620 And if we do our show triggers again, it's gone. 33 00:01:56,620 --> 00:02:01,570 And just to verify that it works, it's not just deleting the trigger from show triggers. 34 00:02:01,960 --> 00:02:09,039 Let's try and trigger it, if you will, with something that would have caused a problem or thrown an 35 00:02:09,039 --> 00:02:09,970 error in the past. 36 00:02:09,970 --> 00:02:20,140 So insert into follows and we have follower ID comma follow we just the worst part. 37 00:02:23,570 --> 00:02:24,190 Values. 38 00:02:24,190 --> 00:02:26,330 So let's insert something where they're the same. 39 00:02:27,880 --> 00:02:30,550 How about seven and seven? 40 00:02:31,820 --> 00:02:32,730 No problem. 41 00:02:32,750 --> 00:02:37,550 So in the past, that would have been a problem because of that trigger, but it's now gone. 42 00:02:38,360 --> 00:02:42,320 So that's really all there is to managing triggers or at least all that I'm going to talk about here. 43 00:02:42,320 --> 00:02:46,670 We can look at them using show and we can delete them using drop trigger. 44 00:02:47,300 --> 00:02:53,450 So that brings me to the last thing that I warned you about, which is I wanted to just briefly address 45 00:02:53,450 --> 00:02:55,790 a potential hiccup with triggers. 46 00:02:55,910 --> 00:03:01,970 This is kind of a concern that a lot of developers have, and it has to do with debugging code debugging 47 00:03:01,970 --> 00:03:05,360 applications that make use of triggers in your database. 48 00:03:05,360 --> 00:03:11,120 So imagine that you're a developer on a team with a bunch of people and something's not working how 49 00:03:11,120 --> 00:03:12,260 you would expect it to. 50 00:03:12,290 --> 00:03:19,190 Let's say when you insert something into a table, you realize that actually two items are being created 51 00:03:19,190 --> 00:03:21,440 for some reason and you have no idea why. 52 00:03:21,440 --> 00:03:25,430 And you might think, okay, maybe that's something happening on my side of the application. 53 00:03:25,430 --> 00:03:26,780 Maybe that's my code. 54 00:03:26,780 --> 00:03:32,990 That's, that's something is wrong with our logic on the front end with our, let's say, Objective-C 55 00:03:33,020 --> 00:03:33,560 code. 56 00:03:33,800 --> 00:03:39,500 And you could scour that forever, but you would actually never get to the core of the problem if it 57 00:03:39,500 --> 00:03:41,090 was because of a trigger. 58 00:03:41,090 --> 00:03:48,470 And triggers are kind of stealth little bugs waiting to happen because you have no way of identifying 59 00:03:48,470 --> 00:03:50,660 that something is happening because of a trigger. 60 00:03:50,660 --> 00:03:52,460 It happens kind of behind the scenes. 61 00:03:52,460 --> 00:04:00,470 Like, remember, if we go back here with our unfollow, if I can go find that way back here. 62 00:04:00,470 --> 00:04:00,920 Oh, boy. 63 00:04:00,920 --> 00:04:01,760 Where is that? 64 00:04:02,730 --> 00:04:03,450 Here we go. 65 00:04:04,080 --> 00:04:12,510 Here we had 99 entries created automatically when all I did was run a single line of SQL to actually 66 00:04:12,510 --> 00:04:13,740 delete things. 67 00:04:14,280 --> 00:04:16,170 It ended up creating 99 things. 68 00:04:16,200 --> 00:04:22,200 Now we knew that was going to happen, but if you have a bunch of triggers and somebody doesn't know 69 00:04:22,200 --> 00:04:27,750 about them on the team or you forget about them, it can make things difficult to debug because it's 70 00:04:27,750 --> 00:04:30,910 basically magic happening behind the scenes things that are hidden. 71 00:04:30,930 --> 00:04:35,580 Typically when you're debugging, your code is up in front of you. 72 00:04:35,580 --> 00:04:37,710 You can see where the problems are happening. 73 00:04:37,920 --> 00:04:41,460 So triggers cause problems sometimes because of that. 74 00:04:41,460 --> 00:04:46,590 And the other thing I would say is that some people have a tendency to chain triggers together. 75 00:04:46,770 --> 00:04:51,150 So with our unfollow example, you could have another event trigger. 76 00:04:51,330 --> 00:04:52,350 So let's say. 77 00:04:53,510 --> 00:04:54,000 Here. 78 00:04:54,020 --> 00:04:57,860 When a follow is deleted, we're inserting into unfollow those. 79 00:04:57,860 --> 00:05:03,890 We could have another trigger for when something is inserted into unfollow us to do something else and 80 00:05:03,890 --> 00:05:05,090 you can chain them together. 81 00:05:05,090 --> 00:05:10,190 And there's actually situations where, believe it or not, that actually makes sense, but it's kind 82 00:05:10,190 --> 00:05:11,720 of frowned upon by a lot of people. 83 00:05:11,720 --> 00:05:12,920 So just be conscientious. 84 00:05:12,920 --> 00:05:18,140 If you're using a lot of triggers, typically there's a better way of doing it without having to make 85 00:05:18,140 --> 00:05:18,980 use of triggers. 86 00:05:18,980 --> 00:05:23,210 And if there's not, then you know that it's a good use case for triggers. 87 00:05:23,210 --> 00:05:27,230 I'm not saying you should never use them, but by all means they're very useful. 88 00:05:27,230 --> 00:05:31,940 It's just that you should be conscientious about them and try and find ways to not use them. 89 00:05:32,270 --> 00:05:39,200 So to wrap that up, just be aware that triggers can make debugging more difficult, more problematic, 90 00:05:39,320 --> 00:05:41,210 because stuff happens behind the scenes. 91 00:05:41,210 --> 00:05:47,810 It's not explicit when something happens, and then also be aware that too many triggers can often be 92 00:05:47,810 --> 00:05:48,440 a problem. 93 00:05:48,440 --> 00:05:51,860 And you want to avoid triggers as much as you can. 94 00:05:51,860 --> 00:05:53,330 Not at all costs or anything. 95 00:05:53,330 --> 00:05:54,230 I'm not saying they're bad. 96 00:05:54,230 --> 00:05:55,640 Just be careful with them. 97 00:05:55,640 --> 00:05:56,060 All right. 98 00:05:56,060 --> 00:05:58,520 So hopefully that answers some of your questions about triggers. 99 00:05:58,520 --> 00:06:00,590 Hopefully you can see the utility in them. 100 00:06:01,010 --> 00:06:05,330 And I know a lot of people are asking about preventing people from following themselves. 101 00:06:05,330 --> 00:06:10,610 So if nothing else, hopefully it's nice to see that how you can actually prevent people from following 102 00:06:10,610 --> 00:06:11,270 themselves. 103 00:06:11,570 --> 00:06:12,170 All right.