Move a Scheduled Event to Another Database in MySQL

In MySQL scheduled events are tasks that run according to a given schedule. After creating a scheduled event, we can modify it using the ALTER EVENT statement. This statement allows us to change the event’s definition, change its schedule, enable/disable it, rename it, and more. It also allows us to move the event to another database.

To move an event to another database, we use the ALTER EVENT statement with the RENAME clause, prefixing the event name with the database name (using dot notation).

Continue reading

3 Ways to Check an Event’s Status in MySQL

When we create a scheduled event in MySQL it’s enabled by default. However, we do have the option of creating it in disabled status. We can also go back later and change an event’s status from enabled to disabled.

Given this fact, we may sometimes find ourselves wondering whether an event is currently enabled or disabled.

Below are three ways to go about checking an event for its enabled/disabled status.

Continue reading

How to Prevent a MySQL Event from Disappearing Once it’s Completed

By default, scheduled events in MySQL are dropped from the system once they expire. That means that if the event’s schedule has completed, then the event is dropped.

But we can override this behaviour with the ON COMPLETION clause. Specifically we can specify ON COMPLETION PRESERVE to keep the event in the system after it has expired. By default, events have ON COMPLETION NOT PRESERVE added to their definition, which means they are dropped as soon as they expire. Using ON COMPLETION PRESERVE changes this so that they aren’t dropped when they expire.

Continue reading

How to Change the Comment on a Scheduled Event in MySQL

Suppose you have an existing MySQL event that has a comment. But the comment isn’t as helpful as you’d like it to be, and so you’d like to change it to something else.

This can be done easily with the ALTER EVENT statement along with the COMMENT clause. When we include the COMMENT clause in the ALTER EVENT statement, it replaces whatever comment was in the event with the new comment. If the event didn’t have a comment, then a new comment appears.

Continue reading

Add a Composite Primary Key to an Existing Table in MySQL

In MySQL, adding a composite primary key to an existing table is similar to adding a singular primary key. We define the primary key with the ALTER TABLE statement.

However, when adding a composite primary key we need to use the ADD PRIMARY KEY clause. We can’t define it at the column level. With singular primary keys on the other hand, we have the choice of using the ADD PRIMARY KEY clause or applying it at the column level.

Continue reading

How Scheduled Events Deal with sql_mode in MySQL

When we create a scheduled event in MySQL, the current sql_mode is stored with the event. That causes the event to use the sql_mode that was in effect at the time the event was created. Same with altering an event.

Therefore, if we change the sql_mode after the event was created or altered, it won’t have any impact on the event. In other words, we can safely change our system’s sql_mode without worrying about whether it’s going to mess up any existing scheduled events.

Continue reading