MariaDB EVENT: Create, Alter, and Drop Scheduled Events
A MariaDB event schedules SQL to run once or at regular intervals. Use CREATE EVENT to define it, ALTER EVENT to change it, and DROP EVENT to remove it. Both the server’s Event Scheduler and the event itself must be enabled for it to run.
Check the scheduler, privileges, and time zone
To create, change, or drop an event, you need the database’s EVENT privilege. The event’s DEFINER also needs the table privileges required by its SQL. Use a separate MariaDB test server with a learning database. If the example objects already exist, use a fresh practice environment.
SELECT @@global.event_scheduler AS scheduler_status,
@@session.time_zone AS session_time_zone,
CURRENT_TIMESTAMP AS session_now;
SELECT EVENT_SCHEMA, EVENT_NAME, STATUS
FROM information_schema.EVENTS
WHERE STATUS = 'ENABLED' ORDER BY EVENT_SCHEMA, EVENT_NAME;
An administrator can enable the scheduler at runtime if it shows OFF. If it shows DISABLED, check the startup configuration. It cannot be enabled at runtime in that state. Turning on the global scheduler also makes other enabled events eligible to run. Metadata visibility depends on your privileges. Check the session time zone and SQL mode used to define the schedule. MariaDB events overview
Create a recurring event in the disabled state
Run the event statements in the same practice connection using an account with EVENT, CREATE, INSERT, and SELECT. The setup uses UTC (+00:00). EVERY sets the interval, STARTS sets the first scheduled time, and DO supplies the SQL. For multiple statements, use BEGIN ... END and handle the delimiter in your client. CREATE EVENT syntax
USE learning;
SET SESSION time_zone = '+00:00';
CREATE TABLE event_log_demo (
id INT AUTO_INCREMENT PRIMARY KEY,
marker VARCHAR(20) NOT NULL,
executed_at DATETIME NOT NULL
) ENGINE=InnoDB;
CREATE EVENT learning.event_tick_demo
ON SCHEDULE EVERY 1 MINUTE
STARTS CURRENT_TIMESTAMP + INTERVAL 1 MINUTE
ON COMPLETION PRESERVE
DISABLE
DO INSERT INTO learning.event_log_demo(marker, executed_at)
VALUES ('A', CURRENT_TIMESTAMP);
SHOW CREATE EVENT learning.event_tick_demo;
SELECT EVENT_NAME, STATUS, TIME_ZONE, INTERVAL_VALUE, INTERVAL_FIELD,
STARTS, ENDS, LAST_EXECUTED, DEFINER
FROM information_schema.EVENTS
WHERE EVENT_SCHEMA = 'learning' AND EVENT_NAME = 'event_tick_demo';
SELECT COUNT(*) AS log_rows FROM learning.event_log_demo;
Use SHOW CREATE EVENT to inspect the schedule and SQL. Expect DISABLED, +00:00, and a one-minute interval, with NULL in LAST_EXECUTED and zero log rows. Back up an existing definition before using CREATE OR REPLACE and check whether you can alter only the properties you need.
Enable scheduling and verify the output
Only on the separate test server, after reviewing its other events, have an administrator run the global command if the scheduler is OFF. The command applies to the running server. To keep the setting after a restart, add event_scheduler=ON to an appropriate server group such as [mariadb] in the option file the server actually reads. For replication, decide which server should execute the events.
SET GLOBAL event_scheduler = ON;
[mariadb]
event_scheduler=ON
In the practice account’s UTC connection, set the start to eight seconds from now and the end to 45 seconds from now. With a one-minute interval the window contains a single scheduled run, and its end bounds the test even if your session disconnects.
ALTER EVENT learning.event_tick_demo
ON SCHEDULE EVERY 1 MINUTE
STARTS CURRENT_TIMESTAMP + INTERVAL 8 SECOND
ENDS CURRENT_TIMESTAMP + INTERVAL 45 SECOND
ENABLE;
SELECT id, marker, executed_at
FROM learning.event_log_demo ORDER BY id;
SELECT EVENT_NAME, STATUS, LAST_EXECUTED
FROM information_schema.EVENTS
WHERE EVENT_SCHEMA = 'learning'
AND EVENT_NAME IN ('event_tick_demo', 'event_once_demo')
ORDER BY EVENT_NAME;
Run the queries after the start time to look for the A row and a populated LAST_EXECUTED. If no row appears, compare the current time and STARTS, then check the scheduler and event statuses. LAST_EXECUTED records the last execution time. Confirm the job’s result using the output table, counts, or the server error log. A preserved definition can already show DISABLED after the final eligible run when the next scheduled time falls beyond ENDS. EVENTS metadata reference
Change the definition and verify the definer
Back up the definition and disable the event first. The second ALTER changes the interval to two minutes and the marker to B while explicitly keeping the event disabled. Check the resulting definition, time zone, and DEFINER. The account running the change may become the new definer. ALTER EVENT syntax
ALTER EVENT learning.event_tick_demo DISABLE;
ALTER EVENT learning.event_tick_demo
ON SCHEDULE EVERY 2 MINUTE
STARTS CURRENT_TIMESTAMP + INTERVAL 1 MINUTE
ON COMPLETION PRESERVE
DISABLE
DO INSERT INTO learning.event_log_demo(marker, executed_at)
VALUES ('B', CURRENT_TIMESTAMP);
SHOW CREATE EVENT learning.event_tick_demo;
SELECT EVENT_NAME, STATUS, TIME_ZONE, INTERVAL_VALUE, INTERVAL_FIELD,
STARTS, ENDS, LAST_EXECUTED, DEFINER
FROM information_schema.EVENTS
WHERE EVENT_SCHEMA = 'learning' AND EVENT_NAME = 'event_tick_demo';
Enable the revised event with another short window, then repeat the verification queries. The new B row and the existing A row remain separate. Disabling or altering a definition does not stop or roll back SQL already running. If a job might outlast its interval, handle overlap with a unique key, a run record, or a suitable lock.
ALTER EVENT learning.event_tick_demo
ON SCHEDULE EVERY 2 MINUTE
STARTS CURRENT_TIMESTAMP + INTERVAL 8 SECOND
ENDS CURRENT_TIMESTAMP + INTERVAL 45 SECOND
ENABLE;
Schedule one run and preserve its definition
Disable the recurring event and use AT to schedule a one-time event. ON COMPLETION PRESERVE retains the definition for inspection after it runs.
ALTER EVENT learning.event_tick_demo DISABLE;
CREATE EVENT learning.event_once_demo
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 8 SECOND
ON COMPLETION PRESERVE
ENABLE
DO INSERT INTO learning.event_log_demo(marker, executed_at)
VALUES ('once', CURRENT_TIMESTAMP);
Rerun the verification queries. The log gains one once row, and the completed event shows DISABLED. The default ON COMPLETION NOT PRESERVE removes a completed event, and a past schedule can cause immediate removal. Plan separately how to recover work skipped during downtime or due to a privilege error.
Back up and drop the events
Confirm the database and event names, then save the definitions from SHOW CREATE EVENT before running DROP EVENT for the two practice events. DROP EVENT syntax
ALTER EVENT learning.event_tick_demo DISABLE;
SHOW CREATE EVENT learning.event_tick_demo;
SHOW CREATE EVENT learning.event_once_demo;
DROP EVENT learning.event_tick_demo;
DROP EVENT learning.event_once_demo;
SELECT COUNT(*) AS remaining_demo_events
FROM information_schema.EVENTS
WHERE EVENT_SCHEMA = 'learning'
AND EVENT_NAME IN ('event_tick_demo', 'event_once_demo');
SELECT marker, COUNT(*) AS log_rows
FROM learning.event_log_demo
GROUP BY marker ORDER BY marker;
Expect zero remaining_demo_events. The inserts for A, B, and once stay in the log. If you changed the separate test server from OFF to ON, check for dependencies before restoring the setting with the administrator. Check for ongoing work before cleaning up its data.




