Purpose
You can use this statement to create and schedule an event.
Notice
By default, an event is not enabled after it is created. You need to execute SET GLOBAL event_scheduler = 1; to enable it. For more information about the event_scheduler parameter, see event_scheduler.
Syntax
CREATE
[DEFINER = user]
EVENT
[IF NOT EXISTS]
event_name
ON SCHEDULE schedule
[ON COMPLETION [NOT] PRESERVE]
[ENABLE | DISABLE | DISABLE ON SLAVE] // By default, an event is enabled after being created.
[COMMENT 'string']
DO event_body;
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}
Considerations
- If
START TIMEandEND TIMEare not specified forEVERY interval, the event will be executed infinitely. - For a repeatable event, if its execution duration exceeds the scheduling interval, multiple identical events will be executed at the same time. You need to make sure that the execution duration is shorter than the scheduling interval.
- You cannot set a maximum execution duration. The maximum execution duration of one-off events is 24 hours.
- We recommend that you do not create an event based on another.
- You cannot modify the scheduling plan of an ongoing event.
- You cannot rename an ongoing event. Otherwise, the event cannot be correctly executed.
- If an event fails for 16 consecutive times, it will be stopped.
Parameters
| Parameter | Description |
|---|---|
| IF NOT EXISTS | Specifies not to create the event if it already exists. If the event already exists and IF NOT EXISTS is not specified, an error is returned. |
| event_name | The name of the event to be created. |
| DEFINER | Optional. The user that executes the event. You cannot define the user. The default value is the current user. |
| ON SCHEDULE schedule | The time and frequency to execute the event. The value is in the format of "a timestamp + INTERVAL", for example, "2020-12-01 00:00:00 + INTERVAL 1 DAY". The interval value must be an integer and the time unit can be YEAR, MONTH, DAY, HOUR, MINUTE, or SECOND. |
| ON COMPLETION [NOT] PRESERVE | Specifies whether to retain the event after it is executed. The default value is NOT PRESERVE, which specifies to automatically delete the event after it is executed. |
| ENABLE, DISABLE, DISABLE ON SLAVE | The status of the event. The default value is ENABLE, which specifies to immediately enable an event after it is created. DISABLE specifies to disable an event after it is created. The event will not be automatically executed until it is manually enabled. DISABLE ON SLAVE specifies not to execute the event on the server. This value is used only in an environment that involves replication. |
| COMMENT 'string' | Optional. The comment on the event. |
| DO event_body | The SQL statement to be executed. You can specify only one SQL statement here. BEGIN ... END supports multiple SQL statements. |
Examples
obclient> CREATE EVENT myevent
ON SCHEDULE AT '2024-07-01 00:00:00' + INTERVAL 1 HOUR
DO
UPDATE myschema.mytable SET mycol = mycol + 1;