MySQL Events Event

Source: Internet
Author: User

I. Use
Used to execute an event at a certain time or to perform an event periodically.


Two. Syntax
CREATE
[definer = {User | Current_User}]
EVENT
[IF not EXISTS]
Event_Name
On SCHEDULE SCHEDULE
[on completion [NOT] PRESERVE]
[ENABLE | DISABLE | DISABLE on SLAVE]
[COMMENT ' COMMENT ']
Do event_body;


Basic points
Schedule
at timestamp [+ INTERVAL INTERVAL] ...
| Every interval
[Starts timestamp [+ INTERVAL INTERVAL] ...]
[ENDS timestamp [+ INTERVAL INTERVAL] ...]

Interval
Quantity {Year | QUARTER | MONTH | Day | HOUR | MINUTE | WEEK | SECOND | Year_month | Day_hour | Day_minute | Day_second | Hour_minute | Hour_second | Minute_second}


A.event_name represents the name of the event
B.schedule represents the trigger point, "at timestamp" is typically used only once, and can be used in general using the current time plus a deferred period of time, for example: at Current_timestamp + INTERVAL 1 HOUR. You can also define a time constant, for example: at ' 2006-02-10 23:59:00 '; "Every interval" is typically used for periodic execution and can set the start time and end time.
C.on completion [NOT] PRESERVE, by default, is automatically deleted after the event expires. If you want to keep the event use on completion PRESERVE, you can set on completion [NOT] PRESERVE if you do not want to keep it.
d.enable | Disable indicates that the setting enables or disables this event.
E.comment indicates an increase in comments.


Three. Event Open
MSYQL The event is turned off by default.
Show global variables like '%event% ';
Show:
Event_scheduleroff

Open:
Set global Event_scheduler=on (or 1);

To view a process:
Show Processlist;

You will see a user for Event_scheduler, performing a status of waiting on the empty queue process


Four. Example
1. Insert one piece of data to Test2 per second
DELIMITER//
CREATE EVENT Test2_insert on SCHEDULE every 1 SECOND
Do
BEGIN
INSERT test2 (UID) VALUES (10);
END;
//


2. Insert a piece of data into test2 every 5 minutes starting every 0 o'clock today
DELIMITER//
CREATE EVENT Test2_insert_5_minute on SCHEDULE every 5 minute starts curdate ()
Do
BEGIN
INSERT test2 (' time ') VALUES (now ());
END;
//

For manually changing the date, such as changing the current event 18:08:05 to 19:09:55, the actual inserted record is delayed, and the record that should have been inserted at 19:10:00 may be postponed to 19:10:50

Five. viewing and deleting events
Show events;
Show Create EVENT Test2_insert_5_minute;

Drop event Test2_insert_5_minute;

MySQL Events Event

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.