mysql opens the event scheduler (Event Scheduler)

Event Scheduler (Event Scheduler) has only been added since MySQL5.1.6, so first of all you need to make sure that you use mysql version not lower than 5.1.6. Check Version You can check the "Server Version" using the command line or the connection information in a connection tool such as Navicat.

1. MySQL enables event_scheduler

In the command line interface, use the following commands to view and set.View and enable the event_scheduler switch of mysql

2. Create a new timed event

1、使用命令行
语法:
CREATE EVENT [IFNOT EXISTS] event_name
ONSCHEDULE schedule
[ONCOMPLETION [NOT] PRESERVE]
[ENABLE | DISABLE]
[COMMENT ‘comment’]
DO sql_statement;

[ENABLE | DISABLE] can set whether the status of the event is turned on or off after the event is created, and the default is ENABLE.
[COMMENT 'comment'] can add comments to the event.

Example 1: Insert a record into the data table every second
CREATE EVENT 'NewEvent'
ON SCHEDULE EVERY 1 SECOND
ON COMPLETION NOT PRESERVE
ENABLE
DO
insert aaa value('a');

Example 2: Clear the aaa table at 12:00 on May 31, 2020:
CREATE EVENT 'NewEvent'
ON SCHEDULE AT '2020-05-31 12:00:00'
ON COMPLETION NOT PRESERVE
ENABLE
DO
TRUNCATE TABLE aaa;

Example 3: 5 Tenhou Qingku aaa table:
CREATE EVENT 'NewEvent'
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 5 DAY
ON COMPLETION NOT PRESERVE
ENABLE
DO
TRUNCATE TABLE aaa;

Example 4: After 5 days, clear the aaa table every day.
CREATE EVENT 'NewEvent'
ON SCHEDULE EVERY 1 DAY
STARTS CURRENT_TIMESTAMP + INTERVAL 5 DAY
ON COMPLETION NOT PRESERVE
ENABLE
DO
TRUNCATE TABLE aaa;

Example 5: Start clearing the aaa table every day after 5 days, and stop the execution after one month:
CREATE EVENT NewEvent
ON SCHEDULE EVERY 1 DAY
STARTS CURRENT_TIMESTAMP + INTERVAL 5 DAY
ENDS CURRENT_TIMESTAMP + INTERVAL 1 MONTH
ON COMPLETION NOT PRESERVE
ENABLE
DO
TRUNCATE TABLE aaa;

2. Using Navicat to create a new event
Using Navicat to create a new event is much simpler.
First, find "Event" in Navicat, right-click and select "New Event"
insert image description here
and then enter the sql statement to be executed and the time and date to be set in the interface.
insert image description here
Finally, click "Save" to complete adding an event.
Note: If event_scheduler=off is prompted when saving, you need to enable event_scheduler according to the steps in the first step.

Guess you like

Origin blog.csdn.net/babdpfi/article/details/106423577