MySQL - CREATE EVENT Statement

A MySQL Event is nothing but a task that execute at a particular schedule. An event can contain one or more MySQL statements these statements are stored in the databases and gets executed at the specified schedule.
The CREATE EVENT statement is used to create and schedule an MYSQL event.
Following is the syntax of the MySQL CREATE EVENT statement −
CREATE EVENT [IF NOT EXISTS] event_name
   ON SCHEDULE time_stamp
   DO event_body;

Where, event_name is the name of the event you need to create, time_stamp is the time at which the statements should be executed and event_body is the set of statement to be executed.
Assume we have created a table with name data using the CREATE TABLE statement as shown below −
mysql> CREATE TABLE Data (Name VARCHAR(255), age INT);
Query OK, 0 rows affected (2.70 sec)

Following query creates an event with name test_event, it inserts a record in the above created table one minute after the execution −
mysql> CREATE EVENT example_event1 ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 Minute DO INSERT INTO new.Data VALUES('Rahman', 25);
Query OK, 0 rows affected (0.20 sec)

You can also pass the exact time to execute the event. First of all let us retrieve the current time using the Now() function as shown below −
mysql> Select Now();
+---------------------+
| Now()               |
+---------------------+
| 2021-02-10 12:09:30 |
+---------------------+ MySQL CREATE EVENT Statement
1 row in set (0.09 sec)
Following query creates another event to which we pass the current time stamp as schedule −
mysql> CREATE EVENT example_event2 ON SCHEDULE AT '2021-02-10 12:09:30' DO INSERT INTO new.Data VALUES('Raju', 30);
Query OK, 0 rows affected (0.17 sec)

After one minute if you verify the contents of the table data using the SELECT statement you can observe the inserted records as shown below −
mysql> SELECT * from data;
+--------+------+
| Name   | age  |
+--------+------+
| Rahman | 25   |
| Raju   | 30   |
+--------+------+
2 rows in set (0.00 sec)

If you use the IF NOT EXISTS clause along with the CREATE EVENT statement as shown below a new event will be created and if an event with the given name, already exists the query will be ignored.
mysql> CREATE EVENT IF NOT EXISTS example_event2 ON SCHEDULE AT CURRENT_TIMESTAMP DO DROP TABLE Data;
Query OK, 0 rows affected (0.36 sec)

Using EVERY clause, you can create an event with an interval as a schedule following is the syntax to do so −
CREATE EVENT event_name ON SCHEDULE EVERY interval quantity DO event_body;

Following query creates an event which truncates the table data every month −
mysql> CREATE EVENT event_hourly ON SCHEDULE EVERY 1 MONTH DO TRUNCATE TABLE data;
Query OK, 0 rows affected (0.27 sec)

You can add a comment while creating an event using this clause Following is the syntax to do so −
CREATE EVENT event_name ON SCHEDULE schedule COMMENT 'comment_string' DO event_body;

Where comment_string is the comment you need to add. Following query adds a comment while creating an event −
mysql> CREATE EVENT
event_hourly1
ON SCHEDULE
EVERY 1 MONTH
COMMENT 'This truncates the data table each month'
DO
TRUNCATE TABLE data;
Query OK, 0 rows affected (0.25 sec)

    31 Lectures 
     6 hours 

 Eduonix Learning Solutions

    84 Lectures 
     5.5 hours 

 Frahaan Hussain

    6 Lectures 
     3.5 hours 

 DATAhill Solutions Srinivas Reddy

    60 Lectures 
     10 hours 

 Vijay Kumar Parvatha Reddy

    10 Lectures 
     1 hours 

 Harshit Srivastava

    25 Lectures 
     4 hours 

 Trevoir Williams
 Print
 Add Notes
 Bookmark this page