7 hours 36 minutes
I welcome to model for sequel programming to this is less than five sequel events were in the first sub lesson five point to intro to events.
So what is a my sequel event or an event? All the database engines support this kind of concept.
Well, an event is a scheduled action
that you indicate you want the database to take at some
date or time or some interval.
So in this example,
we're gonna show you how to run the schedule, event command and what you need to do to get one going.
So again, I have a sequel script, and at the top, I'm, ah, leading from employees count table. I'm using the count table from the employees that a base that we worked with in previous examples again,
one thing that you need to do is make sure that your global event scheduler is on, um, in my sequel by default, it is off.
Of course, if you're not using events at all, you will want to leave it off as it consumes. Resource is, that could be used for ah, running your application.
So only turn it on if you're running events. But don't be scared to turn it on, either. Um, it's not going to consume too many. Resource is just a good habit to ah, you know, turn on what you're gonna use and turn off what you're not going to use.
So at the top, I was working with another test events. So I'm going to get rid of that,
and the script again will be available for download
to the top. I dropped the event if exists, that so I can run this script as I work with it. I said to the limiter, So it doesn't clash with the limiter in the definition
and the definition we run the create event
of the event name.
I called this second insert count.
It's on a schedule
every one second,
and it starts.
This is the start date, so I've put it in for a past date and time.
Well, it's currently today, but
the past time will make sure that it starts immediately
and the event does something simple again. I set the number to the max
counter from the count table, plus one.
So I am implementing it
now. The coal lease function just as a refresher. What it does is if
this comes back, is no
is if this results in a knoll or nothing,
we instead replace that knoll with zero. So zero plus one would be one. Now, why would we do that? Well, if the tables completely empty as it will be when there's no records, I will get an old back because there's nothing there.
I didn't take that value, and I inserted into the employees count table.
I then end the statement using the DL emitter to find above.
I then need to switch this limiter back to a semicolon.
And then I run the show Events Command, which will show all the events on the database as well as their settings. So if I go ahead and run this,
see? Okay, so at the top, I'm deleting from the table, so I'm resetting its values
right now. You can see I have values in there,
and I'm gonna go ahead and run this.
You can see that it's showing the details of the event just in Stan. She ated
It's got a interval field of second and the interval value is one. So everyone second it runs is enabled.
It started already because that's a past value
but place that in the future in the future, it obviously would not run
until that date and time had passed.
Now, if we take a look at the count table
and refresh it,
okay, goes up to 32
33 34 35 36 I keep hitting Refresh. We can see that it is indeed inserting data every second, and that is events. So what is an event? It's a scheduled action that the database takes
set on your schedule
at your interval. Starting on whatever date you choose, you don't have to run it every second or every month or every week. You could have it run one time in the future two months from now as well,
so there's a lot of flexibility. Their events are good for different types of database cleanup. For example, if you want to make sure a table never gets above a certain size,
you can have a monthly
event that runs and basically cleans up the table for you
or does something similar Now you may be thinking to yourself, Well, you know,
procedures and triggers and all of that stuff I can kind of
do either or
in both. And that's true. Sometimes. Sometimes you could, uh, do what you're doing in a trigger, an event,
and vice versa.
It kind of comes down to what you're trying to achieve
and how you're trying to implement it.
But there is some flexibility and that you could end up using one or the other
to solve the same situation.
So that closes this lesson for events. If you down with this script and were and worked with it, I hope you that you got it working and we're able to see how the event was declared and how it functions.
And that's really the best way to learn is to practice
with that said, That finishes this lesson, and I hope to see you in the next thank you.