mysql Event Modify Event (ALTER EVENT) Disable Event (DISABLE) Enable Event (ENABLE) Event Rename and Database Event Migration

  • 2021-12-19 07:10:13
  • OfStack

This article provides examples of mysql event modification events (ALTER EVENT), disable events (DISABLE), enable events (ENABLE), event renaming, and database event migration operations. Share it for your reference, as follows:

We should know that MySQL allows us to change various properties of existing events. If we want to change an existing event, we can use the ALTER EVENT statement, as follows:


ALTER EVENT event_name
ON SCHEDULE schedule
ON COMPLETION [NOT] PRESERVE
RENAME TO new_event_name
ENABLE | DISABLE
DO
 event_body

The ALTER EVENT statement only applies to existing events, and if we try to modify an event that does not exist, MySQL will issue an error message, so you should check for the existence of the event using the SHOW EVENTS statement before changing the event:


mysql> SHOW EVENTS FROM testdb;
+--------+---------------+----------------+-----------+----------+---------------------+----------------+----------------+--------+------+----------+------------+----------------------+----------------------+--------------------+
| Db   | Name     | Definer    | Time zone | Type   | Execute at     | Interval value | Interval field | Starts | Ends | Status  | Originator | character_set_client | collation_connection | Database Collation |
+--------+---------------+----------------+-----------+----------+---------------------+----------------+----------------+--------+------+----------+------------+----------------------+----------------------+--------------------+
| testdb | test_event_02 | root@localhost | SYSTEM  | ONE TIME | 2017-08-03 04:24:48 | NULL      | NULL      | NULL  | NULL | DISABLED |     0 | utf8         | utf8_general_ci   | utf8_general_ci  |
+--------+---------------+----------------+-----------+----------+---------------------+----------------+----------------+--------+------+----------+------------+----------------------+----------------------+--------------------+
1 row in set

Create a sample event that inserts 1 new record per minute into the messages table to demonstrate how to use the various features of the ALTER EVENT statement:


USE testdb;
CREATE EVENT test_event_04
ON SCHEDULE EVERY 1 MINUTE
DO
  INSERT INTO messages(message,created_at)
  VALUES('Test ALTER EVENT statement',NOW());

Let's modify the event to run once every 2 minutes:


ALTER EVENT test_event_04
ON SCHEDULE EVERY 2 MINUTE;

We can also change the body code of the event by specifying new logic:


ALTER EVENT test_event_04
DO
  INSERT INTO messages(message,created_at)
  VALUES('Message from event',NOW());
--  Empty the data in the table 
truncate messages;

After the modification is complete, you can wait 2 minutes and look at the messages table again:


mysql> SELECT * FROM messages;
+----+--------------------+---------------------+
| id | message      | created_at     |
+----+--------------------+---------------------+
| 1 | Message from event | 2017-08-03 04:46:47 |
| 2 | Message from event | 2017-08-03 04:48:47 |
+----+--------------------+---------------------+
2 rows in set

We can use the DISABLE keyword after the ALTER EVENT statement to disable an event:


ALTER EVENT test_event_04
DISABLE;

We can also view the status of events by using the SHOW EVENTS statement:


mysql> SHOW EVENTS FROM testdb;
+--------+---------------+----------------+-----------+-----------+---------------------+----------------+----------------+---------------------+------+----------+------------+----------------------+----------------------+--------------------+
| Db   | Name     | Definer    | Time zone | Type   | Execute at     | Interval value | Interval field | Starts       | Ends | Status  | Originator | character_set_client | collation_connection | Database Collation |
+--------+---------------+----------------+-----------+-----------+---------------------+----------------+----------------+---------------------+------+----------+------------+----------------------+----------------------+--------------------+
| testdb | test_event_02 | root@localhost | SYSTEM  | ONE TIME | 2017-08-03 04:24:48 | NULL      | NULL      | NULL        | NULL | DISABLED |     0 | utf8         | utf8_general_ci   | utf8_general_ci  |
| testdb | test_event_04 | root@localhost | SYSTEM  | RECURRING | NULL        | 2       | MINUTE     | 2017-08-03 04:44:47 | NULL | DISABLED |     0 | utf8         | utf8_general_ci   | utf8_general_ci  |
+--------+---------------+----------------+-----------+-----------+---------------------+----------------+----------------+---------------------+------+----------+------------+----------------------+----------------------+--------------------+
2 rows in set

We can use the ENABLE keyword after the ALTER EVENT statement to enable events:


ALTER EVENT test_event_04
ENABLE;

View the following event status:


mysql> SHOW EVENTS FROM testdb;
+--------+---------------+----------------+-----------+-----------+---------------------+----------------+----------------+---------------------+------+----------+------------+----------------------+----------------------+--------------------+
| Db   | Name     | Definer    | Time zone | Type   | Execute at     | Interval value | Interval field | Starts       | Ends | Status  | Originator | character_set_client | collation_connection | Database Collation |
+--------+---------------+----------------+-----------+-----------+---------------------+----------------+----------------+---------------------+------+----------+------------+----------------------+----------------------+--------------------+
| testdb | test_event_02 | root@localhost | SYSTEM  | ONE TIME | 2017-08-03 04:24:48 | NULL      | NULL      | NULL        | NULL | DISABLED |     0 | utf8         | utf8_general_ci   | utf8_general_ci  |
| testdb | test_event_04 | root@localhost | SYSTEM  | RECURRING | NULL        | 2       | MINUTE     | 2017-08-03 04:44:47 | NULL | ENABLED |     0 | utf8         | utf8_general_ci   | utf8_general_ci  |
+--------+---------------+----------------+-----------+-----------+---------------------+----------------+----------------+---------------------+------+----------+------------+----------------------+----------------------+--------------------+
2 rows in set

Let's try again to rename an existing event using ALTER EVENT:


mysql> SHOW EVENTS FROM testdb;
+--------+---------------+----------------+-----------+----------+---------------------+----------------+----------------+--------+------+----------+------------+----------------------+----------------------+--------------------+
| Db   | Name     | Definer    | Time zone | Type   | Execute at     | Interval value | Interval field | Starts | Ends | Status  | Originator | character_set_client | collation_connection | Database Collation |
+--------+---------------+----------------+-----------+----------+---------------------+----------------+----------------+--------+------+----------+------------+----------------------+----------------------+--------------------+
| testdb | test_event_02 | root@localhost | SYSTEM  | ONE TIME | 2017-08-03 04:24:48 | NULL      | NULL      | NULL  | NULL | DISABLED |     0 | utf8         | utf8_general_ci   | utf8_general_ci  |
+--------+---------------+----------------+-----------+----------+---------------------+----------------+----------------+--------+------+----------+------------+----------------------+----------------------+--------------------+
1 row in set

0

To view the following event status:


mysql> SHOW EVENTS FROM testdb;
+--------+---------------+----------------+-----------+----------+---------------------+----------------+----------------+--------+------+----------+------------+----------------------+----------------------+--------------------+
| Db   | Name     | Definer    | Time zone | Type   | Execute at     | Interval value | Interval field | Starts | Ends | Status  | Originator | character_set_client | collation_connection | Database Collation |
+--------+---------------+----------------+-----------+----------+---------------------+----------------+----------------+--------+------+----------+------------+----------------------+----------------------+--------------------+
| testdb | test_event_02 | root@localhost | SYSTEM  | ONE TIME | 2017-08-03 04:24:48 | NULL      | NULL      | NULL  | NULL | DISABLED |     0 | utf8         | utf8_general_ci   | utf8_general_ci  |
+--------+---------------+----------------+-----------+----------+---------------------+----------------+----------------+--------+------+----------+------------+----------------------+----------------------+--------------------+
1 row in set

1

When you're done, move events from one database to another by using the RENAME TO clause:


mysql> SHOW EVENTS FROM testdb;
+--------+---------------+----------------+-----------+----------+---------------------+----------------+----------------+--------+------+----------+------------+----------------------+----------------------+--------------------+
| Db   | Name     | Definer    | Time zone | Type   | Execute at     | Interval value | Interval field | Starts | Ends | Status  | Originator | character_set_client | collation_connection | Database Collation |
+--------+---------------+----------------+-----------+----------+---------------------+----------------+----------------+--------+------+----------+------------+----------------------+----------------------+--------------------+
| testdb | test_event_02 | root@localhost | SYSTEM  | ONE TIME | 2017-08-03 04:24:48 | NULL      | NULL      | NULL  | NULL | DISABLED |     0 | utf8         | utf8_general_ci   | utf8_general_ci  |
+--------+---------------+----------------+-----------+----------+---------------------+----------------+----------------+--------+------+----------+------------+----------------------+----------------------+--------------------+
1 row in set

2

Look at the event status again:


mysql> SHOW EVENTS FROM newdb;
+-------+---------------+----------------+-----------+-----------+------------+----------------+----------------+---------------------+------+---------+------------+----------------------+----------------------+--------------------+
| Db  | Name     | Definer    | Time zone | Type   | Execute at | Interval value | Interval field | Starts       | Ends | Status | Originator | character_set_client | collation_connection | Database Collation |
+-------+---------------+----------------+-----------+-----------+------------+----------------+----------------+---------------------+------+---------+------------+----------------------+----------------------+--------------------+
| newdb | test_event_05 | root@localhost | SYSTEM  | RECURRING | NULL    | 2       | MINUTE     | 2017-08-03 04:44:47 | NULL | ENABLED |     0 | utf8         | utf8_general_ci   | utf8_general_ci  |
+-------+---------------+----------------+-----------+-----------+------------+----------------+----------------+---------------------+------+---------+------------+----------------------+----------------------+--------------------+
1 row in set

Ok, that's all for this record.

More readers interested in MySQL can check out the topics on this site: "Summary of MySQL Index Operation Skills", "Summary of MySQL Common Functions", "Encyclopedia of MySQL Log Operation Skills", "Encyclopedia of MySQL Transaction Operation Skills", "Encyclopedia of MySQL Stored Procedure Skills" and "Summary of MySQL Database Lock Related Skills"

I hope this article is helpful to everyone's MySQL database.


Related articles: