The event scheduler in MySQL, which can be used to perform timed tasks.
First, open
Event scheduling is turned off by default, and executable is turned on.
To see if the event Scheduler is turned on:
SHOW VARIABLES like ' Event_scheduler '; SELECT @ @event_scheduler;
Turn on Event Scheduler
SET GLOBAL event_scheduler=1;
SET GLOBAL Event_scheduler=on;
Or add event_scheduler=1 to the My.ini file.
or add "-event_scheduler=1" after starting the command
To view an existing event scheduler
Show events;
To view the status of the event scheduler
Show Processlist;
Second, create the event scheduler
CREATE EVENT [IF not EXISTS] event_name on SCHEDULE SCHEDULE [on completion [NOT] PRESERVE] [ENABLE | DISABLE] [COMMENT ' COMMENT '] do sql_statement;
Schedule: Is the execution plan, there are two options, the first is to execute at a certain time, and the second is to be executed at intervals from one time to the next.
at TIMESTAMP [+ INTERVAL INTERVAL] | Every INTERVAL [starts TIMESTAMP] [ENDS TIMESTAMP]
at TIMESTAMP [+ INTERVAL INTERVAL]: executed only at specified point in time;
Every INTERVAL [starts TIMESTAMP] [ENDS TIMESTAMP]: How long is the interval performed;
INTERVAL: Time interval, can be accurate to seconds.
Quantity {Year | QUARTER | MONTH | Day | HOUR | MINUTE |
WEEK | SECOND | Year_month
Event_Name: Is the event name you want to create
On completion [NOT] PRESERVE: Save after end, not save by default, once executed, the event is deleted, so it is strongly recommended that this parameter be set to on completion PRESERVE.
Do sql_statement: Can be a DML statement, a DCL statement, or a call to a stored procedure.
Third, modify the event scheduler
ALTER EVENT event_name [on SCHEDULE SCHEDULE] [RENAME to New_event_name] [on completion [NOT] PRESERVE] [COMME NT ' comment ' [ENABLE | DISABLE] [do sql_statement]
Iv. Example of Event Scheduler
Example 1: Adding data after 1 minutes
Create event if not exists eve_test on SCHEDULE @ current_timestamp () + interval 1 minute on completion PRESERVE Do insert to test_20161107 (T_day) VALUES (now ());
Example 2: Change to add data every 1 minutes
Alter event eve_test on schedule Every 1 minute starts now () on completion preserve enable doing insert into test _20161107 (T_day) VALUES (now ());
Use Show events to view the status of the event scheduler after modification;
Example 3: Calling a stored procedure
Alter event eve_test on schedule Every 1 minute starts today () On completion preserve enable does call Proc_test () ;
V. Delete
DROP EVENT [IF EXISTS] event_name;
This article is from the "three countries Cold jokes" blog, please be sure to keep this source http://myhwj.blog.51cto.com/9763975/1870202
MySQL Create Event Scheduler