MySQL tutorial: CREATE EVENT [EN]
top of page
CerebroSQL

MySQL: 

CREATE EVENT

Syntax:
CREATE
[DEFINER = user]
EVENT
[IF NOT EXISTS]
event_name
ON SCHEDULE schedule
[ON COMPLETION [NOT] PRESERVE]
[ENABLE | DISABLE | DISABLE ON SLAVE]
[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}

This statement creates and schedules a new event. The event will not
run unless the Event Scheduler is enabled. For information about
checking Event Scheduler status and enabling it if necessary, see
https://dev.mysql.com/doc/refman/8.0/en/events-configuration.html.

CREATE EVENT requires the EVENT privilege for the schema in which the
event is to be created. If the DEFINER clause is present, the
privileges required depend on the user value, as discussed in
https://dev.mysql.com/doc/refman/8.0/en/stored-objects-security.html.

The minimum requirements for a valid CREATE EVENT statement are as
follows:

o The keywords CREATE EVENT plus an event name, which uniquely
identifies the event in a database schema.

o An ON SCHEDULE clause, which determines when and how often the event
executes.

o A DO clause, which contains the SQL statement to be executed by an
event.

This is an example of a minimal CREATE EVENT statement:

CREATE EVENT myevent
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 HOUR
DO
UPDATE myschema.mytable SET mycol = mycol + 1;

The previous statement creates an event named myevent. This event
executes once---one hour following its creation---by running an SQL
statement that increments the value of the myschema.mytable table's
mycol column by 1.

The event_name must be a valid MySQL identifier with a maximum length
of 64 characters. Event names are not case-sensitive, so you cannot
have two events named myevent and MyEvent in the same schema. In
general, the rules governing event names are the same as those for
names of stored routines. See
https://dev.mysql.com/doc/refman/8.0/en/identifiers.html.

An event is associated with a schema. If no schema is indicated as part
of event_name, the default (current) schema is assumed. To create an
event in a specific schema, qualify the event name with a schema using
schema_name.event_name syntax.

URL: https://dev.mysql.com/doc/refman/8.0/en/create-event.html

Example

bottom of page