MySQL Tutorial

MySQL Installation and Configuration

MySQL Database Operations

Database Design

MySQL Data Types

MySQL Storage Engines

MySQL Basic Operations of Tables

MySQL Constraints

MySQL Operators

MySQL Function

MySQL Manipulate Table Data

MySQL View

MySQL Indexes

MySQL Stored Procedure

MySQL Trigger

MySQL Transactions

MySQL Character Set

MySQL User Management

MySQL Database Backup and Recovery

MySQL Log

MySQL Performance Optimization

MySQL create events (CREATE EVENT)

In MySQL, an event is a task that runs based on a schedule. Therefore, it's also known as a scheduled event. Events are similar to cron jobs in UNIX or tasks in Task Scheduler in Windows.

The event scheduler is a MySQL feature that allows you to schedule tasks that run within the database environment. The tasks, also known as events, are executed according to a specific schedule.

Let's see how we can create an event in MySQL:

Enable Event Scheduler

First, make sure that event scheduler is enabled. You can check its status using the following command:

SHOW VARIABLES LIKE 'event_scheduler';

If the event scheduler is disabled, you can turn it on by running this command:

SET GLOBAL event_scheduler = ON;

Syntax to CREATE EVENT

Here is the basic syntax for creating an event:

CREATE EVENT event_name
ON SCHEDULE schedule
DO
event_body;

Where:

  • event_name: The name of the event.
  • schedule: Defines when and how often an event is executed.
  • event_body: The SQL statement to be executed.

The schedule can be a single occurrence or a recurring event. For example, AT timestamp [+ INTERVAL] or EVERY interval STARTS timestamp [+ INTERVAL] ENDS timestamp [+ INTERVAL].

Example

Let's consider an example where we create an event that deletes all rows from a log_table every day at 3 AM.

CREATE EVENT clear_log_table
ON SCHEDULE
  EVERY 1 DAY
  STARTS (TIMESTAMP(CURRENT_DATE) + INTERVAL 3 HOUR)
DO
  DELETE FROM log_table;

In this example, clear_log_table is an event that is scheduled to run every day (EVERY 1 DAY), starting at 3 AM (STARTS (TIMESTAMP(CURRENT_DATE) + INTERVAL 3 HOUR)). The action of this event is to delete all rows from log_table.

Note:

  • You must have the EVENT privilege for the database to create an event.
  • The event scheduler thread runs as the 'mysql' system user, so make sure this user has the necessary privileges to execute the event's SQL statement.
  • To see all events in the database, you can use the SHOW EVENTS statement.
  • To drop an event, use the DROP EVENT event_name statement.
  • To alter an event, use the ALTER EVENT event_name statement.
  1. Creating scheduled events in MySQL:

    • Scheduled events in MySQL allow you to automate tasks that occur at specified intervals.
    -- Create a scheduled event in MySQL
    CREATE EVENT event_name
    ON SCHEDULE EVERY 1 DAY
    DO
    BEGIN
        -- Event logic
    END;
    
  2. How to use CREATE EVENT in MySQL:

    • Use the CREATE EVENT statement in MySQL to define and create a new scheduled event.
    -- Create a scheduled event in MySQL
    CREATE EVENT event_name
    ON SCHEDULE EVERY 1 HOUR
    DO
    BEGIN
        -- Event logic
    END;
    
  3. MySQL event scheduling example:

    • Example of creating a scheduled event in MySQL that updates a timestamp every day.
    -- Create a daily event to update timestamp
    CREATE EVENT update_timestamp_event
    ON SCHEDULE EVERY 1 DAY
    DO
    UPDATE my_table SET timestamp_column = NOW();
    
  4. Defining events with MySQL:

    • Define events in MySQL using the CREATE EVENT statement, specifying the event name, schedule, and the event logic.
    -- Create a scheduled event in MySQL
    CREATE EVENT event_name
    ON SCHEDULE EVERY 1 WEEK
    DO
    BEGIN
        -- Event logic
    END;
    
  5. Creating and managing scheduled events in MySQL:

    • Create and manage scheduled events in MySQL by using the CREATE EVENT statement and altering or dropping existing events.
    -- Create a scheduled event in MySQL
    CREATE EVENT event_name
    ON SCHEDULE EVERY 30 MINUTE
    DO
    BEGIN
        -- Event logic
    END;
    
    -- Alter a scheduled event in MySQL
    ALTER EVENT event_name
    ON SCHEDULE EVERY 1 HOUR;
    
  6. Using parameters in MySQL events:

    • Use parameters in MySQL events to customize the behavior of the event logic.
    -- Create an event with parameters in MySQL
    CREATE EVENT event_name
    ON SCHEDULE EVERY 2 DAYS
    DO
    BEGIN
        -- Event logic using parameters
    END;
    
  7. Executing and testing MySQL events:

    • Execute and test MySQL events by observing their execution at scheduled intervals and verifying the expected results.
    -- Manually execute the event
    CALL event_name;