In MySQL we can use the ALTER EVENT
statement to make changes to existing scheduled events, including changing the name of the event.
Example
Suppose we create the following event:
CREATE EVENT Event1
ON SCHEDULE
EVERY 1 MONTH
DO
INSERT INTO EventTest ( EventTimestamp, InsertedBy )
VALUES (NOW(), 'Event1');
We can change its name with the following code:
ALTER EVENT Event1
RENAME TO Event2;
That code changes the event’s name from Event1
to Event2
.
Move to a Different Database
We can also use the RENAME
clause to move events to a different database (even without changing the name of the event itself). To do this, include the database name in the name (using dot notation):
ALTER EVENT PetHotel.Event2
RENAME TO KrankyKranes.Event2;
In this example, I moved the event called Event2
from the PetHotel
database to the KrankyKranes
database.
After running that code, the event no longer exists in the PetHotel
database. If I run SHOW EVENTS
in that database, there’s no event called Event2
:
SHOW EVENTS FROM PetHotel LIKE 'Event%';
Result:
Empty set (0.00 sec)
But if run the same code in the other database, the event is returned:
SHOW EVENTS FROM KrankyKranes LIKE 'Event%';
Result:
+--------------+--------+----------------+-----------+-----------+------------+----------------+----------------+---------------------+------+---------+------------+----------------------+----------------------+--------------------+
| Db | Name | Definer | Time zone | Type | Execute at | Interval value | Interval field | Starts | Ends | Status | Originator | character_set_client | collation_connection | Database Collation |
+--------------+--------+----------------+-----------+-----------+------------+----------------+----------------+---------------------+------+---------+------------+----------------------+----------------------+--------------------+
| krankykranes | Event2 | root@localhost | SYSTEM | RECURRING | NULL | 1 | MONTH | 2024-04-14 08:03:13 | NULL | ENABLED | 1 | utf8mb4 | utf8mb4_0900_ai_ci | utf8mb4_0900_ai_ci |
+--------------+--------+----------------+-----------+-----------+------------+----------------+----------------+---------------------+------+---------+------------+----------------------+----------------------+--------------------+
We can also change the name of the event when we move it. Let’s move it back to the original database and give it its original name:
ALTER EVENT KrankyKranes.Event2
RENAME TO PetHotel.Event1;
And let’s use SHOW EVENTS
to check the result:
SHOW EVENTS FROM PetHotel LIKE 'Event%';
Result:
+----------+--------+----------------+-----------+-----------+------------+----------------+----------------+---------------------+------+---------+------------+----------------------+----------------------+--------------------+
| Db | Name | Definer | Time zone | Type | Execute at | Interval value | Interval field | Starts | Ends | Status | Originator | character_set_client | collation_connection | Database Collation |
+----------+--------+----------------+-----------+-----------+------------+----------------+----------------+---------------------+------+---------+------------+----------------------+----------------------+--------------------+
| PetHotel | Event1 | root@localhost | SYSTEM | RECURRING | NULL | 1 | MONTH | 2024-04-14 08:03:13 | NULL | ENABLED | 1 | utf8mb4 | utf8mb4_0900_ai_ci | utf8mb4_0900_ai_ci |
+----------+--------+----------------+-----------+-----------+------------+----------------+----------------+---------------------+------+---------+------------+----------------------+----------------------+--------------------+
We can see that it is back in its original database with its original name.
The user executing such statements must have the EVENT
privilege on both databases in order to move an event from one database to the other.