All right.
So we've got one more video here.
And this has to do with managing triggers, which sounds a bit like a chapter in a self-help book.
But really, it has to do with how we make sure our triggers are working and mainly delete them.
So this is where the name of our trigger comes into play.
And then also I'll end with a quick warning about triggers.
So the first thing that we can do is just list the triggers that exist in a database so we can just
use show triggers to see what we have going on.
And I'll warn you, this is not my warning I already talked about, but I'll warn you that what you
get when you run this is kind of a mess to look at.
But we can see we have in our Instagram data, we have two triggers prevent self follows, and then
you can actually see the code inside of that trigger.
And then we have captured Unfollow and you can see that code as well.
And if I switched over to the other database, I believe it was used trigger demo.
And I do show triggers.
We have one here.
This is our must be adult.
All right, so we have that.
I'll switch back to my Instagram clone.
OC and do our show triggers just to make sure they're still there.
They shouldn't go anywhere anyways, but we've got two of them, so that's pretty much it to listing
them.
Moving on.
So if I want to remove a trigger, it's pretty simple.
It's drop trigger and then the trigger name.
So let's go back.
Let's say I want to remove this prevent self follows.
I want to allow users to follow themselves.
So I want to get rid of that.
I'll just copy the name just so I don't have to type it.
And we just do drop trigger prevent self follows.
And if we do our show triggers again, it's gone.
And just to verify that it works, it's not just deleting the trigger from show triggers.
Let's try and trigger it, if you will, with something that would have caused a problem or thrown an
error in the past.
So insert into follows and we have follower ID comma follow we just the worst part.
Values.
So let's insert something where they're the same.
How about seven and seven?
No problem.
So in the past, that would have been a problem because of that trigger, but it's now gone.
So that's really all there is to managing triggers or at least all that I'm going to talk about here.
We can look at them using show and we can delete them using drop trigger.
So that brings me to the last thing that I warned you about, which is I wanted to just briefly address
a potential hiccup with triggers.
This is kind of a concern that a lot of developers have, and it has to do with debugging code debugging
applications that make use of triggers in your database.
So imagine that you're a developer on a team with a bunch of people and something's not working how
you would expect it to.
Let's say when you insert something into a table, you realize that actually two items are being created
for some reason and you have no idea why.
And you might think, okay, maybe that's something happening on my side of the application.
Maybe that's my code.
That's, that's something is wrong with our logic on the front end with our, let's say, Objective-C
code.
And you could scour that forever, but you would actually never get to the core of the problem if it
was because of a trigger.
And triggers are kind of stealth little bugs waiting to happen because you have no way of identifying
that something is happening because of a trigger.
It happens kind of behind the scenes.
Like, remember, if we go back here with our unfollow, if I can go find that way back here.
Oh, boy.
Where is that?
Here we go.
Here we had 99 entries created automatically when all I did was run a single line of SQL to actually
delete things.
It ended up creating 99 things.
Now we knew that was going to happen, but if you have a bunch of triggers and somebody doesn't know
about them on the team or you forget about them, it can make things difficult to debug because it's
basically magic happening behind the scenes things that are hidden.
Typically when you're debugging, your code is up in front of you.
You can see where the problems are happening.
So triggers cause problems sometimes because of that.
And the other thing I would say is that some people have a tendency to chain triggers together.
So with our unfollow example, you could have another event trigger.
So let's say.
Here.
When a follow is deleted, we're inserting into unfollow those.
We could have another trigger for when something is inserted into unfollow us to do something else and
you can chain them together.
And there's actually situations where, believe it or not, that actually makes sense, but it's kind
of frowned upon by a lot of people.
So just be conscientious.
If you're using a lot of triggers, typically there's a better way of doing it without having to make
use of triggers.
And if there's not, then you know that it's a good use case for triggers.
I'm not saying you should never use them, but by all means they're very useful.
It's just that you should be conscientious about them and try and find ways to not use them.
So to wrap that up, just be aware that triggers can make debugging more difficult, more problematic,
because stuff happens behind the scenes.
It's not explicit when something happens, and then also be aware that too many triggers can often be
a problem.
And you want to avoid triggers as much as you can.
Not at all costs or anything.
I'm not saying they're bad.
Just be careful with them.
All right.
So hopefully that answers some of your questions about triggers.
Hopefully you can see the utility in them.
And I know a lot of people are asking about preventing people from following themselves.
So if nothing else, hopefully it's nice to see that how you can actually prevent people from following
themselves.
All right.