MySQL: Creating Simple Event with MySQL

In this second article of this series, I am going to cover creation of basic MySQL events and how to check the status of the same. If you have not read the first article of this series, then you can read about it here MySQL Events.

Creating a First Event

Creating an Event is somewhat like creating stored procedure or user defined function in MySQL. You will have DELIMITER, BEGIN, DO and END keywords.

As mentioned earlier we can define the execution time for the event while creating it. So now let’s create a basic event which executes after 5 minutes of creating event. Have a look at the below block of queries for the same.

1

2

3

4

5

6

7

8

DELIMITER$$

CREATE EVENT first_event

ON SCHEDULE AT CURRENT_TIMESTAMP+INTERVAL5MINUTE

DO

BEGIN

UPDATE table_name SET field_name=field_name+1;

END;

$$;

Now once you have created the event you must make sure that event is created or not. So to make sure you will have to execute a single query to get all events which your MySQL server has.

1

SHOW EVENTS;

You can also write multiple SQL queries between BEGIN and END block of event. And yes you will have to separate multiple queries separated with ‘;’

So this is the basic step to create a simple event in MySQL. This is the basic and one-time event, which means this will be scheduled only once and after the execution is completed this event will be deleted from the server.

As I have already mentioned that this event will get removed once it is executed unless you have mentioned anything on ON COMPLETION block. If you write ON COMPLETION PRESERVE then event will not get deleted after execution is completed. Let’s create one event which will not get deleted after completion.

1

2

3

4

5

6

7

8

9

DELIMITER$$

CREATE EVENT first_event

ON SCHEDULE AT CURRENT_TIMESTAMP+INTERVAL5MINUTE

ON COMPLETION PRESERVE

DO

BEGIN

UPDATE table_name SET field_name=field_name+1;

END;

$$;

Edit the Existing Events

You can edit events normally with the ALTER EVENT clause. Have a look at the below query block which shows the editing of the existing event in MySQL.

1

2

3

4

5

6

7

8

9

DELIMITER$$

ALTER EVENT first_event

ON SCHEDULE AT CURRENT_TIMESTAMP+INTERVAL2MINUTE

ON COMPLETION PRESERVE

DO

BEGIN

UPDATE table_name SET field_name=field_name+1;

END;

$$;

After executing above query your event will be set to execute after 2 minute from current time.

Rename Events in MySQL

Renaming event is very easy here. Have a look at below query and I think you will not require any explanation,

1

2

ALTER EVENT first_event

RENAME TOfirst_event_edited;

Delete Events in MySQL

Deleting events are simple as renaming the event.

1

DROP EVENT first_event;

What’s Next?

After reading this article I hope you will be efficiently able to create a onetime event, edit event, rename events, drop events and also preserve the events. In next article we will see about how to create the repeating events in MySQL i.e. these events will keep running until it is dropped.