Add a Comment to a Scheduled Event in MySQL

MySQL provides us with the ability to create scheduled events – tasks that run according to a specified schedule. Scheduled events allow for the ability to add comments within the event. We can do this with the COMMENT clause.

We can include the comment when creating the event, or we can add one to an existing event.

Create an Event with a Comment

We can include a comment in the event when we create it:

CREATE EVENT Event1
    ON SCHEDULE
        EVERY 1 HOUR
    DISABLE
    COMMENT 'Insert timestamp into table'
    DO
        INSERT INTO EventTest ( EventTimestamp, InsertedBy ) 
            VALUES (NOW(), 'Event1');

Here I’ve added a comment by including a COMMENT clause in the CREATE EVENT statement. This is an optional clause, and if we don’t want to include a comment, we can simply omit the clause altogether (i.e. we can remove the whole line that starts with COMMENT ... ).

The COMMENT clause accepts any string of up to 64 characters. The string must be surrounded by quotation marks.

Add a Comment to an Existing Event

If the event already exists, we can add a comment to it without dropping the event and recreating it with the comment.

To add a comment to an existing event we simply use the ALTER EVENT statement.

Suppose we create the following event without a comment:

CREATE EVENT Event2
    ON SCHEDULE
        EVERY 1 MINUTE
    DISABLE
    DO
        INSERT INTO EventTest ( EventTimestamp, InsertedBy ) 
            VALUES (NOW(), 'Event2');

We can add a comment to the event like this:

ALTER EVENT Event2
    COMMENT 'Insert timestamp into table for Event2';

We can use the SHOW CREATE EVENT statement to check that the comment was added:

SHOW CREATE EVENT Event2;

Result:

+--------+-------------------------------------------------------------------------------------------------------------------------------------------+-----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+--------------------+
| Event | sql_mode | time_zone | Create Event | character_set_client | collation_connection | Database Collation |
+--------+-------------------------------------------------------------------------------------------------------------------------------------------+-----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+--------------------+
| Event2 | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ALLOW_INVALID_DATES,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION | SYSTEM | CREATE DEFINER=`root`@`localhost` EVENT `Event2` ON SCHEDULE EVERY 1 MINUTE STARTS '2024-04-10 16:14:42' ON COMPLETION NOT PRESERVE DISABLE COMMENT 'Insert timestamp into table for Event2' DO INSERT INTO EventTest ( EventTimestamp, InsertedBy )
VALUES (NOW(), 'Event2') | utf8mb4 | utf8mb4_0900_ai_ci | utf8mb4_0900_ai_ci |
+--------+-------------------------------------------------------------------------------------------------------------------------------------------+-----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+--------------------+

We can see that the event’s definition now includes the COMMENT clause that we added.

We can also query the information schema:

SELECT 
    event_comment
FROM information_schema.events
WHERE event_schema = 'krankykranes'
AND event_name = 'Event2';

Result:

+----------------------------------------+
| EVENT_COMMENT |
+----------------------------------------+
| Insert timestamp into table for Event2 |
+----------------------------------------+

Change the Comment

We can also use the ALTER EVENT statement to change an existing comment:

ALTER EVENT Event2
    COMMENT 'New comment for Event2';

Let’s query the information schema again to check that the comment was changed:

SELECT 
    event_comment
FROM information_schema.events
WHERE event_schema = 'krankykranes'
AND event_name = 'Event2';

Result:

+------------------------+
| EVENT_COMMENT |
+------------------------+
| New comment for Event2 |
+------------------------+

The comment was changed as specified.