MySQL Event schedule lets you set up your MySQL database to perform the actions you want at some time
Create event test1
on schedule every 1 days
starts ' 2007-09-01 12:00:00 '
On completion not preserve
does insert into yyy values (' hhh ', ' UUU ');
or
Create event test
on schedule @ ' 2007-09-01 12:00:00 ' + interval 1 day
On completion not preserve
do insert Into yyy values (' hhh ', ' UUU ')
explains that, starting September 1, 2007, the insert operation is performed on the table yyy at 12:00:00 every day, and only once.
You must ensure that Event_scheduler is turned on before using this feature, and you can execute
set global event_scheduler=1;
or
set global event_scheduler=on;
To open, you can also add--event_scheduler=1 directly to the start command. For example,
Mysqld...--event_scheduler=1
can also be added directly in Mysql.ini or MYSQL.CNF
Event_scheduler=1
To see if the time Scheduler is currently turned on, you can execute the following sql:
Show variables like ' Event-scheduler ';
or
SELECT @ @event_scheduler;
or show processlist;
Two, creation time (create Event)
Create event [if not exists] event_name
on Schedule
[on Completion[not] preserve]
[Enable|disable]
[Comment ' comment ']
do sql_statement;
Schedule
at timestamp [+interval interval]
|every interval [starts timestamp][ends timestamp]
Interval
quantity{year|quarter|month|day|hour|minute|
week|second|year_month|day_hour|day_minute|
Day_second|hour_minute|hour_second|minute_second}
1) First look at a simple example to demonstrate inserting a record into a data table per second:
Use test
CREATE TABLE AAA (timeline timestamp);
Create Event E_test_insert
On schedule every 1 second
Do insert into TEST.AAA values (CURRENT_TIMESTAMP);
Wait three seconds, then execute the query to see:
Mysql>select * from TEST.AAA;
You can see that there are three data present
2) Clear the AAA table after 5 days:
Create Event E_test
On schedule at Current_timestamp+interval 5 day
Do truncate TABLE test.aaa;
3) July 20, 2007 12 o'clock full-empty AAA table:
Create Event E_test
On schedule at timestamp ' 2007-07-20 12:00:00 '
Do truncate TABLE test.aaa;
4) Clear the AAA table every day: after---execution, the event is executed at the current creation time of the day
Create Event E_test
On schedule every 1 day
Do truncate TABLE test.aaa;
5) 5 days after the opening of the daily scheduled emptying AAA table:
Create Event E_test
On schedule every 1 day
Starts Current_timestamp +interval 5 day
Do truncate TABLE test.aaa;
6) Clear the AAA table every day and stop execution after 5 days:
Create Event E_test
On schedule every 1 day
Ends Current_timestamp + interval 5 day
Do truncate TABLE test.aaa;
7) 5 days after the start of the day to clear the AAA table, one months later to stop execution
Create Event E_test
On schedule every 1 day
Starts Current_timestamp + interval 5 day
Ends Current_timestamp + interval 1 month
Do truncate TABLE test.aaa;
Third, modify events (Alter event)
Alter event Event_Name
[on schedule Schedule]
[Rename to New_event_name]
[on completion [not] preserve]
[Comment ' comment ']
[Enable|disable]
[Do sql_statement]
1) Temporary Shutdown event
Alter event E_test Disable
2) Turn on event
Alter event E_test Enable
3) Change the daily empty AAA table to clear every 5 days
Alter event E_test
On schedule every 5 day;
Four, delete events (drop event)
The syntax is simple, as follows:
Drop event [if exists] Event_Name
For example, delete the previously created E_test event
Drop Event E_test
The current premise is that this event exists, otherwise it will generate error 1513 (HY000): Unknown event error, so it is best to add if exists
Drop event if exists e_test
In addition, when the Event_scheduler=1 parameter is added to My.ini or MY.CNF, the existing event will continue to run after MySQL is started.