When we create a scheduled event in MySQL, it’s enabled by default. But we can change this.
We can create an event in disabled status, then enable it later. Or we can do the opposite – create it as an enabled event and then disable it later.
Create a Disabled Event
Here’s an example of creating an event in disabled status:
CREATE EVENT Event1
ON SCHEDULE
EVERY 1 MINUTE
DISABLE
DO
INSERT INTO EventTest ( EventTimestamp, InsertedBy )
VALUES (NOW(), 'Event1');
So, although this event is set up to run every minute, it’s currently disabled and won’t run at all.
Enable an Event
We can enable the event with the ALTER EVENT
statement:
ALTER EVENT Event1
ENABLE;
In this case, enabling my event runs it immediately, and then it runs every minute thereafter. But this will depend on the event’s schedule.
Either way, it’s now enabled and will run as per schedule.
Disable an Event
We can again use the ALTER EVENT
statement to disable the event:
ALTER EVENT Event1
DISABLE;
The event remains in the system but in disabled status. Therefore the event won’t run as per schedule until we enable it again.
We don’t need to include the rest of the event’s definition when enabling or disabling events. We can do that if we want, but it’s not required. Of course, if we wanted to change the event’s definition, schedule, etc then we can include those changes in our ALTER EVENT
statement.
Check an Event’s Status
We can query the events
table of the information schema to see whether or not an event is enabled:
SELECT status
FROM information_schema.events
WHERE event_schema = 'test'
AND event_name = 'Event1';
Result:
+----------+
| STATUS |
+----------+
| DISABLED |
+----------+
In particular, we check the status column of the information_schema.events
table to see if the event is enabled or disabled. In our case, the event is disabled.
We can include other columns as required:
SELECT status, event_definition
FROM information_schema.events
WHERE event_schema = 'test'
AND event_name = 'Event1';
Result:
+----------+--------------------------------------------------------------------------------------------+
| STATUS | EVENT_DEFINITION |
+----------+--------------------------------------------------------------------------------------------+
| DISABLED | INSERT INTO EventTest ( EventTimestamp, InsertedBy )
VALUES (NOW(), 'Event1') |
+----------+--------------------------------------------------------------------------------------------+
Here I included the event’s definition in my query. So this query tells me the event’s enabled/disabled status as well as what the scheduled event actually does.