ALTER
[DEFINER = user]
EVENT event_name
[ON SCHEDULE schedule]
[ON COMPLETION [NOT] PRESERVE]
[RENAME TO new_event_name]
[ENABLE | DISABLE | DISABLE ON {REPLICA | SLAVE}]
[COMMENT 'string']
[DO event_body]
ALTER EVENT 语句在不需要删除和重新创建事件的情况下,修改现有事件的一个或多个特性。对于 DEFINER、ON SCHEDULE、ON COMPLETION、COMMENT、ENABLE / DISABLE 以及 DO 子句的语法,与在 CREATE EVENT 中使用时完全相同。(参见 第 15.1.13 节,“CREATE EVENT 语句”。)The ALTER EVENT statement changes
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 15.1.13, “CREATE EVENT Statement”.)
任何用户都可以修改其在数据库上拥有EVENT权限的事件。当用户成功执行ALTER
EVENT语句后,该用户将成为受影响事件的定义者。Any user can alter an event defined on a database for which that
user has the EVENT privilege. When
a user executes a successful ALTER
EVENT statement, that user becomes the definer for the
affected event.
ALTER EVENT仅适用于已存在的事件: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'
在以下每个示例中,假设名为myevent的事件定义如下所示: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;
下面的语句将myevent的调度从立即开始的每六小时一次更改为从语句执行时间起四小时开始的每十二小时一次: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;
可以在一条语句中更改事件的多个特性。此示例将 myevent 执行的 SQL 语句更改为删除 mytable 中的所有记录;同时还更改了事件的调度,使其在此 ALTER EVENT 语句执行后的第二天执行一次。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 EVENT myevent
ON SCHEDULE
AT CURRENT_TIMESTAMP + INTERVAL 1 DAY
DO
TRUNCATE TABLE myschema.mytable;
在 ALTER
EVENT 语句中,只为您希望更改的特性指定选项;省略的选项将保持现有值。这包括任何 CREATE
EVENT 的默认值,例如 ENABLE。Specify the options in an ALTER
EVENT statement only for those characteristics that you
want to change; omitted options keep their existing values. This
includes any default values for CREATE
EVENT such as ENABLE.
要禁用 myevent,使用此 ALTER EVENT 语句:To disable myevent, use this
ALTER EVENT statement:
ALTER EVENT myevent
DISABLE;
ON SCHEDULE 子句可以使用涉及内置 MySQL 函数和用户变量的表达式来获取其中包含的任何 timestamp 或 interval 值。您不能在这些表达式中使用存储过程或可加载函数,也不能使用任何表引用;然而,您可以使用 SELECT FROM DUAL。这对于 ALTER EVENT 和 CREATE EVENT 语句均适用。在这种情况下引用存储过程、可加载函数以及表是明确不允许的,并会导致错误(参见 Bug #22830)。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
cannot use stored routines or loadable functions in such
expressions, and you cannot use any table references; however, you
can use SELECT FROM DUAL. This is true for both
ALTER EVENT and
CREATE EVENT statements. References
to stored routines, loadable functions, and tables in such cases
are specifically not permitted, and fail with an error (see Bug
#22830).
虽然包含另一个 ALTER EVENT 语句的 ALTER EVENT 语句在其 DO 子句中看起来执行成功,但当服务器尝试执行生成的计划事件时,执行会失败并报错。Although an ALTER EVENT statement
that contains another ALTER EVENT
statement in its DO clause appears
to succeed, when the server attempts to execute the resulting
scheduled event, the execution fails with an error.
要重命名事件,请使用 ALTER
EVENT 语句的 RENAME TO 子句。此语句将事件 myevent 重命名为 yourevent: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;
你也可以使用 ALTER EVENT ... RENAME TO ... 和 记法将事件移动到不同的数据库,如下所示:You can also move an event to a different database using
db_name.event_nameALTER EVENT ... RENAME TO ... and
notation, as shown here:db_name.event_name
ALTER EVENT olddb.myevent
RENAME TO newdb.myevent;
要执行前面的语句,执行该语句的用户必须对 olddb 和 newdb 数据库都有EVENT 权限。To execute the previous statement, the user executing it must have
the EVENT privilege on both the
olddb and newdb databases.
没有 RENAME EVENT 语句。There is no RENAME EVENT statement.
值 DISABLE ON REPLICA 在副本上使用,而不是 ENABLE 或 DISABLE,用于表示事件是在复制源服务器上创建并复制到副本的,但不会在副本上执行。通常,DISABLE ON REPLICA 会根据需要自动设置;然而,在某些情况下,您可能希望或需要手动更改它。有关更多信息,请参见 第 19.5.1.16 节,“调用功能的复制”。The value DISABLE ON REPLICA is used on a
replica instead of ENABLE or
DISABLE to indicate an event that was created
on the replication source server and replicated to the replica,
but that is not executed on the replica. Normally,
DISABLE ON REPLICA is set automatically as
required; however, there are some circumstances under which you
may want or need to change it manually. See
Section 19.5.1.16, “Replication of Invoked Features”, for more
information.
DISABLE ON REPLICA 替代了已经被弃用的 DISABLE ON
SLAVE,并可能在未来的 MySQL 版本中被移除。DISABLE ON REPLICA replaces DISABLE ON
SLAVE, which is deprecated, and subject to removal in a
future version of MySQL.