ALTER [DEFINER = {user
| CURRENT_USER }] EVENTevent_name
[ON SCHEDULEschedule
] [ON COMPLETION [NOT] PRESERVE] [RENAME TOnew_event_name
] [ENABLE | DISABLE | DISABLE ON SLAVE] [COMMENT 'comment
'] [DOsql_statement
]
The ALTER EVENT
statement is used
to change one or more of the characteristics of an existing event
without the need to drop and recreate it. The syntax for each of
the DEFINER
, ON SCHEDULE
,
ON COMPLETION
, COMMENT
,
ENABLE
/ DISABLE
, and
DO
clauses is exactly the same as
when used with CREATE EVENT
. (See
Section 12.1.11, “CREATE EVENT
Syntax”.)
Support for the DEFINER
clause was added in
MySQL 5.1.17.
Beginning with MySQL 5.1.12, this statement requires the
EVENT
privilege. When a user
executes a successful ALTER EVENT
statement, that user becomes the definer for the affected event.
(In MySQL 5.1.11 and earlier, an event could be altered only by
its definer, or by a user having the
SUPER
privilege.)
ALTER EVENT
works only with an
existing event:
mysql>ALTER EVENT no_such_event
>ON SCHEDULE
>EVERY '2:3' DAY_HOUR;
ERROR 1517 (HY000): Unknown event 'no_such_event'
In each of the following examples, assume that the event named
myevent
is defined as shown here:
CREATE EVENT myevent ON SCHEDULE EVERY 6 HOUR COMMENT 'A sample comment.' DO UPDATE myschema.mytable SET mycol = mycol + 1;
The following statement changes the schedule for
myevent
from once every six hours starting
immediately to once every twelve hours, starting four hours from
the time the statement is run:
ALTER EVENT myevent ON SCHEDULE EVERY 12 HOUR STARTS CURRENT_TIMESTAMP + INTERVAL 4 HOUR;
It is possible to change multiple characteristics of an event in a
single statement. This example changes the SQL statement executed
by myevent
to one that deletes all records from
mytable
; it also changes the schedule for the
event such that it executes once, one day after this
ALTER EVENT
statement is run.
ALTER TABLE myevent ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 DAY DO TRUNCATE TABLE myschema.mytable;
It is necessary to include only those options in an
ALTER EVENT
statement which
correspond to characteristics that you actually wish to change;
options which are omitted retain their existing values. This
includes any default values for CREATE
EVENT
such as ENABLE
.
To disable myevent
, use this
ALTER EVENT
statement:
ALTER EVENT myevent DISABLE;
The ON SCHEDULE
clause may use expressions
involving built-in MySQL functions and user variables to obtain
any of the timestamp
or
interval
values which it contains. You
may not use stored routines or user-defined functions in such
expressions, nor may you use any table references; however, you
may use SELECT FROM DUAL
. This is true for both
ALTER EVENT
and
CREATE EVENT
statements. Beginning
with MySQL 5.1.13, references to stored routines, user-defined
functions, and tables in such cases are specifically disallowed,
and fail with an error (see Bug#22830).
An ALTER EVENT
statement that
contains another ALTER EVENT
statement in its DO
clause appears
to succeed; however, when the server attempts to execute the
resulting scheduled event, the execution fails with an error.
To rename an event, use the ALTER
EVENT
statement's RENAME TO
clause.
This statement renames the event myevent
to
yourevent
:
ALTER EVENT myevent RENAME TO yourevent;
You can also move an event to a different database using
ALTER EVENT ... RENAME TO ...
and
notation, as shown here:
db_name.event_name
ALTER EVENT olddb.myevent RENAME TO newdb.myevent;
To execute the previous statement, the user executing it must have
the EVENT
privilege on both the
olddb
and newdb
databases.
There is no RENAME EVENT
statement.
Beginning with MySQL 5.1.18, a third value may also appear in
place of ENABLED
or
DISABLED
; DISABLE ON SLAVE
is used on a replication slave to indicate an event which was
created on the master and replicated to the slave, but which is
not executed on the slave. Normally, DISABLE ON
SLAVE
is set automatically as required; however, there
are some circumstances under which you may want or need to change
it manually. See Section 16.4.1.8, “Replication of Invoked Features”,
for more information.
User Comments
Add your own comment.