Documentation

SQL Events

SQL Events

The Events activity manages scheduled SQL jobs — server-side routines triggered on a recurring interval or a one-off timestamp. It is designed for the MySQL/MariaDB event scheduler and equivalent time-driven mechanisms on other dialects. Use it to automate housekeeping (log pruning, cache rebuilds, aggregate rollups) without leaving VisuaLeaf.

Quick Start

  1. Open the Events activity from a database's context menu.
  2. Click Create Event to reveal the new-event form.
  3. Enter a Name, pick a Schedule (recurring EVERY or one-time AT), and fill in the timing fields.
  4. Paste the SQL body — a single statement or a BEGIN ... END block.
  5. Click Create Event to submit; the row appears in the list on success.

Creating an Event

The form exposes every option the underlying CREATE EVENT DDL accepts:

FieldPurpose
NameIdentifier used to drop or ALTER the event later.
ScheduleRecurring (EVERY) or One-time (AT).
EveryNumeric interval + unit (SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, YEAR).
StartsOptional first execution timestamp (recurring events).
EndsOptional last-run boundary.
Execute atSingle-shot execution timestamp (one-time events).
SQL BodyThe statement(s) to run when the event fires.

Recurring Example

CREATE EVENT purge_stale_logs
ON SCHEDULE EVERY 1 DAY
STARTS '2026-07-15 02:00:00'
ENDS   '2027-07-15 02:00:00'
DO
    DELETE FROM logs WHERE created_at < NOW() - INTERVAL 30 DAY;

One-time Example

CREATE EVENT nightly_reindex_2026_07_15
ON SCHEDULE AT '2026-07-15 03:00:00'
DO
    OPTIMIZE TABLE orders;

Managing Existing Events

Every event in the database appears as its own card in the list. Each card shows the event name and a Drop action. Dropping asks for confirmation, then executes DROP EVENT.

Prerequisites

  • MySQL / MariaDB: the event scheduler must be running. Verify with SHOW VARIABLES LIKE 'event_scheduler'; and enable it with SET GLOBAL event_scheduler = ON;.
  • Your user needs the EVENT privilege on the database.
  • Other dialects vary: PostgreSQL relies on pg_cron, SQL Server uses SQL Agent jobs, Oracle uses DBMS_SCHEDULER. Where the driver exposes these as events, they are surfaced here; otherwise the activity is disabled.

Tips

  1. Prefix event names to make cleanup easier: evt_daily_..., evt_hourly_....
  2. Wrap multi-statement bodies in BEGIN ... END; the editor accepts either form.
  3. For destructive events (deletes, drops), consider adding a DISABLE state via the SQL Editor while you validate — you can re-enable with ALTER EVENT ... ENABLE.
  4. Combine with the Procedures activity: have the event call a stored procedure to keep the schedule and the logic decoupled.

Ready to try VisuaLeaf?

Download and start managing your MongoDB databases with ease.

Download Free Trial