WEBVTT

00:00.210 --> 00:02.110
All right you've got one more video here.

00:02.220 --> 00:08.370
And this has to do with managing triggers which sounds a bit like a chapter in a self-help book but

00:08.370 --> 00:14.280
really it has to do with how we make sure our triggers are working and mainly delete them.

00:14.430 --> 00:17.370
So this is where the name of our trigger comes into play.

00:17.520 --> 00:20.650
And then also I'll end with a quick warning about triggers.

00:20.970 --> 00:25.980
So the first thing that we can do is just list the triggers that exist in a database so we can just

00:25.980 --> 00:29.440
use show triggers to see what we have going on.

00:29.490 --> 00:31.890
And I'll warn you this is not my warning.

00:31.890 --> 00:39.330
I already talked about it but I'll warn you that what you get when you run this is kind of a mess to

00:39.330 --> 00:40.100
look at.

00:40.410 --> 00:45.200
But we can see we have in our Instagram data we have to triggers prevent cell follows.

00:45.270 --> 00:48.440
And then you can actually see the code inside of that trigger.

00:48.600 --> 00:52.740
And then we have capture unfollow and you can see that code as well.

00:52.740 --> 01:03.590
And if I switched over to the other database I believe it was use trigger demo and I do show triggers.

01:03.600 --> 01:04.760
We have one here.

01:04.860 --> 01:07.130
This is our must be adult.

01:07.410 --> 01:08.010
All right.

01:08.010 --> 01:12.290
So we have that I'll switch back to my Instagram clone.

01:17.150 --> 01:17.600
OK.

01:17.700 --> 01:20.600
And do our show triggers just to make sure they're still there.

01:20.670 --> 01:22.460
They shouldn't go anywhere anyways.

01:22.470 --> 01:24.060
But we've got two of them.

01:24.510 --> 01:26.640
So that's pretty much it to listing them.

01:26.670 --> 01:27.520
Moving on.

01:27.840 --> 01:31.380
So if I want to remove a trigger it's pretty simple.

01:31.380 --> 01:35.040
It's drop trigger and then the trigger name.

01:35.190 --> 01:36.660
So let's go back.

01:36.670 --> 01:39.850
Let's say I want to remove this prevent cell follows.

01:39.870 --> 01:42.660
I want to allow users to follow themselves.

01:42.780 --> 01:44.290
So I want to get rid of that.

01:44.370 --> 01:46.820
I'll just copy the name just so I don't have to type it.

01:47.430 --> 01:53.340
And we just do drop trigger pre-event cell follows.

01:53.700 --> 01:56.840
And if we do our show triggers again it's gone.

01:56.850 --> 02:04.110
And just to verify that it works it's not just the leading trigger from show triggers lets try and trigger

02:04.110 --> 02:10.060
it if you will with something that would have caused a problem or thrown an error in the past.

02:10.170 --> 02:18.710
So insert into follows and we have follow word I.D. come follow me.

02:18.950 --> 02:19.900
This is the worst part

02:23.640 --> 02:24.320
values.

02:24.360 --> 02:27.960
Let's Insearch one thing where they're the same.

02:27.960 --> 02:30.550
How about seven and seven.

02:32.010 --> 02:32.910
No problem.

02:32.970 --> 02:37.720
So in the past that would have been a problem because that trigger but it's now gone.

02:38.070 --> 02:38.490
OK.

02:38.490 --> 02:42.460
So that's really all there is to managing triggers or at least all that I'm going to talk about here.

02:42.540 --> 02:47.020
We can look at them using show and we can delete them using dropping trigger.

02:47.420 --> 02:53.520
So that brings me to the last thing that I warned you about which is I wanted to just briefly address

02:53.520 --> 02:56.120
a potential pickup with triggers.

02:56.160 --> 03:02.040
This is kind of a concern that a lot of developers have and it has to do with debugging code debugging

03:02.040 --> 03:05.640
applications that make use of triggers in your database.

03:05.640 --> 03:11.190
So imagine that you're a developer on a team with a bunch of people and something's not working how

03:11.190 --> 03:12.270
you expect it to.

03:12.480 --> 03:19.260
Let's say when you insert something into a table you realize that actually two items are being created

03:19.260 --> 03:21.460
for some reason and you have no idea why.

03:21.720 --> 03:26.310
And you might think OK maybe that something happening on my side of the application maybe that's my

03:26.310 --> 03:32.860
code that's that something is wrong you know with our logic on the front end with our let's say Objective

03:32.860 --> 03:38.850
C code and you could scour that forever but you would actually never get to the core of the problem

03:39.240 --> 03:46.260
if it was because of a trigger and triggers are kind of stealth little bugs waiting to happen because

03:46.470 --> 03:50.780
you have no way of identifying that something is happening because of a trigger.

03:50.850 --> 03:53.650
It happens kind of behind the scenes like remember.

03:53.680 --> 04:00.570
Go back here with our unfollow if I can go find that way back here.

04:00.600 --> 04:01.820
Oh boy where is that.

04:02.860 --> 04:04.180
Here we go.

04:04.240 --> 04:11.770
Here we had ninety nine entries created automatically when all they did was run a single line of sequel

04:11.770 --> 04:16.360
to actually delete things and ended up creating 99 things.

04:16.360 --> 04:17.830
Now we knew that was going to happen.

04:17.950 --> 04:24.670
But if you have a bunch of triggers and somebody doesn't know about I'm on team or you forget about

04:24.670 --> 04:29.710
them it can make things difficult to debug because it's basically magic happening behind the scenes

04:29.950 --> 04:31.030
things that are hidden.

04:31.180 --> 04:37.030
Typically you know when you're debugging your code is up in front of you you can see where the problems

04:37.030 --> 04:37.810
are happening.

04:38.080 --> 04:43.960
So triggers cause problems sometimes because of that and the other thing I would say is that some people

04:43.960 --> 04:46.970
have a tendency to chain triggers together.

04:47.020 --> 04:51.320
So with unfollowed example you could have another event trigger.

04:51.370 --> 04:56.290
So let's say here were one follow is deleted.

04:56.340 --> 04:58.110
We're inserting into an follows.

04:58.110 --> 05:03.790
We could have another trigger for when something is inserted into unfollowed to do something else.

05:03.900 --> 05:08.460
And you can chain them together and there's actually situations where believe it or not that actually

05:08.460 --> 05:11.900
makes sense but it's kind of frowned upon by a lot of people.

05:11.910 --> 05:14.640
So just be conscientious if you're using a lot of triggers.

05:14.670 --> 05:19.180
Typically there's a better way of doing it without having to make use of triggers.

05:19.230 --> 05:23.310
And if there's not then you know that it's a good use case for triggers.

05:23.310 --> 05:25.320
I'm not saying you should never use them.

05:25.410 --> 05:27.370
By all means they're very useful.

05:27.420 --> 05:31.910
It's just that you should be conscientious about them and try and find ways to not use them.

05:32.100 --> 05:32.360
OK.

05:32.370 --> 05:33.440
So to wrap that up.

05:33.600 --> 05:40.500
Just be aware that triggers can make debugging more difficult more problematic because stuff happens

05:40.500 --> 05:41.320
behind the scenes.

05:41.340 --> 05:43.720
It's not explicit when something happens.

05:43.860 --> 05:50.280
And then also be aware that too many triggers can often be a problem and you want to avoid triggers

05:50.790 --> 05:52.070
as much as you can.

05:52.110 --> 05:54.350
Not at all costs or anything I'm not saying they're bad.

05:54.360 --> 05:55.640
Just be careful with them.

05:55.830 --> 05:56.120
All right.

05:56.130 --> 05:58.610
So that answer some of your questions about triggers.

05:58.740 --> 06:04.200
Hopefully you can see the utility in them and I know a lot of people are asking about preventing people

06:04.200 --> 06:05.410
from following themselves.

06:05.520 --> 06:10.680
So if nothing else hopefully it's nice to see that how you can actually prevent people from following

06:10.680 --> 06:11.720
their stuff.

06:11.730 --> 06:12.310
All right.
