CREATE [DEFINER =user
] EVENT [IF NOT EXISTS]event_name
ON SCHEDULEschedule
[ON COMPLETION [NOT] PRESERVE] [ENABLE | DISABLE | DISABLE ON SLAVE] [COMMENT 'string
'] DOevent_body
;schedule
: { ATtimestamp
[+ INTERVALinterval
] ... | EVERYinterval
[STARTStimestamp
[+ INTERVALinterval
] ...] [ENDStimestamp
[+ INTERVALinterval
] ...] }interval
:quantity
{YEAR | QUARTER | MONTH | DAY | HOUR | MINUTE | WEEK | SECOND | YEAR_MONTH | DAY_HOUR | DAY_MINUTE | DAY_SECOND | HOUR_MINUTE | HOUR_SECOND | MINUTE_SECOND}
This statement creates and schedules a new event. 此语句创建并计划一个新事件。The event does not run unless the Event Scheduler is enabled. 除非启用了事件计划程序,否则事件不会运行。For information about checking Event Scheduler status and enabling it if necessary, see Section 25.4.2, “Event Scheduler Configuration”.有关检查事件计划程序状态并在必要时启用它的信息,请参阅第25.4.2节,“事件计划程序配置”。
CREATE EVENT
requires the EVENT
privilege for the schema in which the event is to be created. CREATE EVENT
要求要在其中创建事件的架构具有EVENT
权限。If the 如果存在DEFINER
clause is present, the privileges required depend on the user
value, as discussed in Section 25.6, “Stored Object Access Control”.DEFINER
子句,则所需的权限取决于user
值,如第25.6节,“存储对象访问控制”所述。
The minimum requirements for a valid 有效的CREATE EVENT
statement are as follows:CREATE EVENT
语句的最低要求如下:
The keywords 关键字CREATE EVENT
plus an event name, which uniquely identifies the event in a database schema.CREATE EVENT
加上事件名称,该名称在数据库架构中唯一标识事件。
An 一个ON SCHEDULE
clause, which determines when and how often the event executes.ON SCHEDULE
子句,用于确定事件执行的时间和频率。
A DO
clause, which contains the SQL statement to be executed by an event.DO
子句,其中包含要由事件执行的SQL语句。
This is an example of a minimal 这是最小CREATE EVENT
statement:CREATE EVENT
语句的示例:
CREATE EVENT myevent ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 HOUR DO UPDATE myschema.mytable SET mycol = mycol + 1;
The previous statement creates an event named 前面的语句创建了一个名为myevent
. myevent
的事件。This event executes once—one hour following its creation—by running an SQL statement that increments the value of the 此事件在创建后一小时执行一次,方法是运行SQL语句,将myschema.mytable
table's mycol
column by 1.myschema.mytable
表的mycol
列的值递增1。
The event_name
must be a valid MySQL identifier with a maximum length of 64 characters. event_name
必须是有效的MySQL标识符,最大长度为64个字符。Event names are not case-sensitive, so you cannot have two events named 事件名称不区分大小写,因此在同一架构中不能有两个名为myevent
and MyEvent
in the same schema. myevent
和MyEvent
的事件。In general, the rules governing event names are the same as those for names of stored routines. 通常,控制事件名称的规则与存储例程名称的规则相同。See Section 9.2, “Schema Object Names”.请参阅第9.2节,“模式对象名称”。
An event is associated with a schema. 事件与架构相关联。If no schema is indicated as part of 如果未将任何模式指示为event_name
, the default (current) schema is assumed. event_name
的一部分,则假定为默认(当前)模式。To create an event in a specific schema, qualify the event name with a schema using 要在特定架构中创建事件,请使用
syntax.schema_name
.event_name
schema_name.event_name
语法使用架构限定事件名称。
The DEFINER
clause specifies the MySQL account to be used when checking access privileges at event execution time. DEFINER
子句指定在事件执行时检查访问权限时要使用的MySQL帐户。If the 如果存在DEFINER
clause is present, the user
value should be a MySQL account specified as '
, user_name
'@'host_name
'CURRENT_USER
, or CURRENT_USER()
. DEFINER
子句,则user
值应为指定为'
、user_name
'@'host_name
'CURRENT_USER
或CURRENT_user()
的MySQL帐户。The permitted 允许的user
values depend on the privileges you hold, as discussed in Section 25.6, “Stored Object Access Control”. user
值取决于您拥有的权限,如第25.6节,“存储对象访问控制”所述。Also see that section for additional information about event security.有关事件安全性的更多信息,请参阅该部分。
If the 如果省略DEFINER
clause is omitted, the default definer is the user who executes the CREATE EVENT
statement. DEFINER
子句,则默认的DEFINER
是执行CREATE EVENT
语句的用户。This is the same as specifying 这与显式指定DEFINER = CURRENT_USER
explicitly.DEFINER=CURRENT_USER
相同。
Within an event body, the 在事件体中,CURRENT_USER
function returns the account used to check privileges at event execution time, which is the DEFINER
user. CURRENT_USER
函数返回用于在事件执行时检查权限的帐户,即DEFINER
用户。For information about user auditing within events, see Section 6.2.22, “SQL-Based Account Activity Auditing”.有关事件中用户审核的信息,请参阅第6.2.22节,“基于SQL的帐户活动审核”。
IF NOT EXISTS
has the same meaning for CREATE EVENT
as for CREATE TABLE
: If an event named event_name
already exists in the same schema, no action is taken, and no error results. IF NOT EXISTS
对于CREATE EVENT
和CREATE TABLE
具有相同的含义:如果名为event_name
的事件已存在于同一架构中,则不会执行任何操作,也不会产生错误结果。(However, a warning is generated in such cases.)(但是,在这种情况下会生成警告。)
The ON SCHEDULE
clause determines when, how often, and for how long the event_body
defined for the event repeats. ON SCHEDULE
子句确定为事件定义的event_body
重复的时间、频率和持续时间。This clause takes one of two forms:本条款采用以下两种形式之一:
AT
timestamp
is used for a one-time event. 用于一次性事件。It specifies that the event executes one time only at the date and time given by 它指定事件仅在timestamp
, which must include both the date and time, or must be an expression that resolves to a datetime value. timestamp
给定的日期和时间执行一次,时间戳必须同时包含日期和时间,或者必须是解析为日期时间值的表达式。You may use a value of either the 为此,可以使用DATETIME
or TIMESTAMP
type for this purpose. DATETIME
或TIMESTAMP
类型的值。If the date is in the past, a warning occurs, as shown here:如果日期在过去,则会出现警告,如下所示:
mysql>SELECT NOW();
+---------------------+ | NOW() | +---------------------+ | 2006-02-10 23:59:01 | +---------------------+ 1 row in set (0.04 sec) mysql>CREATE EVENT e_totals
->ON SCHEDULE AT '2006-02-10 23:59:00'
->DO INSERT INTO test.totals VALUES (NOW());
Query OK, 0 rows affected, 1 warning (0.00 sec) mysql>SHOW WARNINGS\G
*************************** 1. row *************************** Level: Note Code: 1588 Message: Event execution time is in the past and ON COMPLETION NOT PRESERVE is set. The event was dropped immediately after creation.
CREATE EVENT
statements which are themselves invalid—for whatever reason—fail with an error.无论出于何种原因,本身无效的语句都会因错误而失败。
You may use 您可以使用CURRENT_TIMESTAMP
to specify the current date and time. CURRENT_TIMESTAMP
戳指定当前日期和时间。In such a case, the event acts as soon as it is created.在这种情况下,事件一经创建就立即生效。
To create an event which occurs at some point in the future relative to the current date and time—such as that expressed by the phrase “three weeks from now”—you can use the optional clause 要创建相对于当前日期和时间在未来某个时间发生的事件,如短语“从现在起三周”所表示的事件,可以使用可选子句+ INTERVAL
. interval
+ INTERVAL
。interval
The interval
portion consists of two parts, a quantity and a unit of time, and follows the syntax rules described in Temporal Intervals, except that you cannot use any units keywords that involving microseconds when defining an event.
部分由两部分组成,即数量和时间单位,并遵循时间间隔中描述的语法规则,但定义事件时不能使用任何涉及微秒的单位关键字。interval
With some interval types, complex time units may be used. 对于某些间隔类型,可以使用复杂的时间单位。For example, “two minutes and ten seconds” can be expressed as 例如,“2分10秒”可以表示为+ INTERVAL '2:10' MINUTE_SECOND
.+ INTERVAL '2:10' MINUTE_SECOND
。
You can also combine intervals. 您还可以组合间隔。For example, 例如,AT CURRENT_TIMESTAMP + INTERVAL 3 WEEK + INTERVAL 2 DAY
is equivalent to “three weeks and two days from now”. AT CURRENT_TIMESTAMP + INTERVAL 3 WEEK + INTERVAL 2 DAY
相当于“从现在起三周零两天”。Each portion of such a clause must begin with 此类子句的每一部分都必须以+ INTERVAL
.+ INTERVAL
开头。
To repeat actions at a regular interval, use an 要定期重复操作,请使用EVERY
clause. EVERY
子句。The EVERY
keyword is followed by an interval
as described in the previous discussion of the AT
keyword. EVERY
关键字后面跟一个interval
,如前面讨论AT
关键字时所述。((+ INTERVAL
is not used with EVERY
.) + INTERVAL
不用于EVERY
。)For example, 例如,EVERY 6 WEEK
means “every six weeks”.EVERY 6 WEEK
意味着“每6周”。
Although 虽然不允许在+ INTERVAL
clauses are not permitted in an EVERY
clause, you can use the same complex time units permitted in a + INTERVAL
.EVERY
子句中使用+ INTERVAL
子句,但可以使用+ INTERVAL
中允许的相同的复杂时间单位。
An EVERY
clause may contain an optional STARTS
clause. EVERY
子句可以包含可选的STARTS
子句。STARTS
is followed by a timestamp
value that indicates when the action should begin repeating, and may also use + INTERVAL
to specify an amount of time “from now”. interval
STARTS
后跟一个timestamp
,该值指示操作何时开始重复,还可以使用+ INTERVAL
指定“从现在开始”的时间量。interval
For example, 例如,EVERY 3 MONTH STARTS CURRENT_TIMESTAMP + INTERVAL 1 WEEK
means “every three months, beginning one week from now”. EVERY 3 MONTH STARTS CURRENT_TIMESTAMP + INTERVAL 1 WEEK
表示“每三个月,从现在开始一周”。Similarly, you can express “every two weeks, beginning six hours and fifteen minutes from now” as 类似地,您可以将“每两周,从现在开始六小时十五分钟”表示为EVERY 2 WEEK STARTS CURRENT_TIMESTAMP + INTERVAL '6:15' HOUR_MINUTE
. EVERY 2 WEEK STARTS CURRENT_TIMESTAMP + INTERVAL '6:15' HOUR_MINUTE
。Not specifying 不指定STARTS
is the same as using STARTS CURRENT_TIMESTAMP
—that is, the action specified for the event begins repeating immediately upon creation of the event.STARTS
与使用STARTS CURRENT_TIMESTAMP
相同,也就是说,为事件指定的操作在创建事件时立即开始重复。
An EVERY
clause may contain an optional ENDS
clause. EVERY
子句可以包含可选的ENDS
子句。The ENDS
keyword is followed by a timestamp
value that tells MySQL when the event should stop repeating. ENDS
关键字后面跟着一个timestamp
,它告诉MySQL事件何时应该停止重复。You may also use 您还可以使用+ INTERVAL
with interval
ENDS
; for instance, EVERY 12 HOUR STARTS CURRENT_TIMESTAMP + INTERVAL 30 MINUTE ENDS CURRENT_TIMESTAMP + INTERVAL 4 WEEK
is equivalent to “every twelve hours, beginning thirty minutes from now, and ending four weeks from now”. + INTERVAL
配合interval
ENDS
;例如,EVERY 12 HOUR STARTS CURRENT_TIMESTAMP + INTERVAL 30 MINUTE ENDS CURRENT_TIMESTAMP + INTERVAL 4 WEEK
相当于“每12小时,从现在开始30分钟,从现在开始四周后结束”。Not using 不使用ENDS
means that the event continues executing indefinitely.ENDS
意味着事件将无限期地继续执行。
ENDS
supports the same syntax for complex time units as STARTS
does.ENDS
支持与STARTS
相同的复杂时间单位语法。
You may use 您可以在STARTS
, ENDS
, both, or neither in an EVERY
clause.EVERY
子句中使用STARTS
、ENDS
、使用两者或两者都不使用。
If a repeating event does not terminate within its scheduling interval, the result may be multiple instances of the event executing simultaneously. 如果重复事件未在其调度间隔内终止,则结果可能是该事件的多个实例同时执行。If this is undesirable, you should institute a mechanism to prevent simultaneous instances. 如果这是不需要的,您应该建立一种机制来防止同时发生实例。For example, you could use the 例如,可以使用GET_LOCK()
function, or row or table locking.GET_LOCK()
函数或行或表锁定。
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. ON SCHEDULE
子句可以使用包含内置MySQL函数和用户变量的表达式来获取它包含的任何时间戳或间隔值。You may not use stored functions or loadable functions in such expressions, nor may you use any table references; however, you may use 不得在此类表达式中使用存储函数或可加载函数,也不得使用任何表引用;但是,您可以使用SELECT FROM DUAL
. SELECT FROM DUAL
。This is true for both CREATE EVENT
and ALTER EVENT
statements. CREATE EVENT
和ALTER EVENT
语句都是如此。References to stored functions, loadable functions, and tables in such cases are specifically not permitted, and fail with an error (see Bug #22830).在这种情况下,特别不允许引用存储函数、可加载函数和表,并且会因错误而失败(请参阅Bug#22830)。
Times in the 使用当前会话ON SCHEDULE
clause are interpreted using the current session time_zone
value. time_zone
解释ON SCHEDULE
子句中的时间。This becomes the event time zone; that is, the time zone that is used for event scheduling and is in effect within the event as it executes. 这成为事件时区;即,用于事件调度且在事件执行时在事件内有效的时区。These times are converted to UTC and stored along with the event time zone internally. 这些时间转换为UTC,并与事件时区一起存储在内部。This enables event execution to proceed as defined regardless of any subsequent changes to the server time zone or daylight saving time effects. 这使事件执行能够按照定义继续进行,而不管服务器时区或夏令时效果的任何后续更改。For additional information about representation of event times, see Section 25.4.4, “Event Metadata”. 有关事件时间表示的更多信息,请参阅第25.4.4节,“事件元数据”。See also Section 13.7.7.18, “SHOW EVENTS Statement”, and Section 26.3.14, “The INFORMATION_SCHEMA EVENTS Table”.另请参阅第13.7.7.18节,“SHOW EVENTS语句”和第26.3.14节,“INFORMATION_SCHEMA事件表”。
Normally, once an event has expired, it is immediately dropped. 通常,一旦事件过期,它将立即被删除。You can override this behavior by specifying 您可以通过指定ON COMPLETION PRESERVE
. ON COMPLETION PRESERVE
来覆盖此行为。Using 使用ON COMPLETION NOT PRESERVE
merely makes the default nonpersistent behavior explicit.ON COMPLETION NOT PRESERVE
只会使默认的非持久性行为显式化。
You can create an event but prevent it from being active using the 您可以创建一个事件,但可以使用DISABLE
keyword. DISABLE
关键字阻止它处于活动状态。Alternatively, you can use 或者,您可以使用ENABLE
to make explicit the default status, which is active. ENABLE
将默认状态显式设置为激活。This is most useful in conjunction with 这在与ALTER EVENT
(see Section 13.1.3, “ALTER EVENT Statement”).ALTER EVENT
结合使用时最为有用(请参阅第13.1.3节,“ALTER EVENT语句”)。
A third value may also appear in place of 第三个值也可能出现在ENABLE
or DISABLE
; DISABLE ON SLAVE
is set for the status of an event on a replica to indicate that the event was created on the replication source server and replicated to the replica, but is not executed on the replica. ENABLE
或DISABLE
的位置;为复制副本上的事件状态设置了DISABLE ON SLAVE
,以指示该事件是在复制源服务器上创建并复制到复制副本上的,但未在复制副本上执行。See Section 17.5.1.16, “Replication of Invoked Features”.请参阅第17.5.1.16节,“调用功能的复制”。
You may supply a comment for an event using a 您可以使用comment子句为事件提供COMMENT
clause. COMMENT
。comment
may be any string of up to 64 characters that you wish to use for describing the event. comment
可以是您希望用于描述事件的最多64个字符的任何字符串。The comment text, being a string literal, must be surrounded by quotation marks.注释文本是字符串文字,必须用引号括起来。
The DO
clause specifies an action carried by the event, and consists of an SQL statement. DO
子句指定事件执行的操作,并由SQL语句组成。Nearly any valid MySQL statement that can be used in a stored routine can also be used as the action statement for a scheduled event. 几乎任何可以在存储例程中使用的有效MySQL语句都可以用作调度事件的操作语句。(See Section 25.8, “Restrictions on Stored Programs”.) (请参阅第25.8节,“存储程序的限制”。)For example, the following event 例如,以下事件e_hourly
deletes all rows from the sessions
table once per hour, where this table is part of the site_activity
schema:e_hourly
每小时从sessions
表中删除一次所有行,其中该表是site_activity
架构的一部分:
CREATE EVENT e_hourly ON SCHEDULE EVERY 1 HOUR COMMENT 'Clears out sessions table each hour.' DO DELETE FROM site_activity.sessions;
MySQL stores the MySQL存储在创建或更改事件时生效的sql_模式系统变量设置,并始终在该设置生效的情况下执行事件,而不管事件开始执行时的当前服务器sql模式如何。sql_mode
system variable setting in effect when an event is created or altered, and always executes the event with this setting in force, regardless of the current server SQL mode when the event begins executing.
A 在CREATE EVENT
statement that contains an 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.DO
子句中包含ALTER EVENT
语句的CREATE EVENT
语句似乎成功;但是,当服务器尝试执行生成的计划事件时,执行失败并出现错误。
Statements such as 仅返回结果集的SELECT
or SHOW
that merely return a result set have no effect when used in an event; the output from these is not sent to the MySQL Monitor, nor is it stored anywhere. SELECT
或SHOW
语句在事件中使用时无效;这些文件的输出不会发送到MySQL监视器,也不会存储在任何地方。However, you can use statements such as 但是,您可以使用SELECT ... INTO
and INSERT INTO ... SELECT
that store a result. SELECT ... INTO
和INSERT INTO ... SELECT
用于存储结果的选项。(See the next example in this section for an instance of the latter.)(有关后者的实例,请参阅本节中的下一个示例。)
The schema to which an event belongs is the default schema for table references in the 事件所属的模式是DO
clause. DO
子句中表引用的默认模式。Any references to tables in other schemas must be qualified with the proper schema name.对其他架构中表的任何引用都必须使用正确的架构名称进行限定。
As with stored routines, you can use compound-statement syntax in the 与存储例程一样,您可以使用DO
clause by using the BEGIN
and END
keywords, as shown here:BEGIN
和END
关键字在DO
子句中使用复合语句语法,如下所示:
delimiter | CREATE EVENT e_daily ON SCHEDULE EVERY 1 DAY COMMENT 'Saves total number of sessions then clears the table each day' DO BEGIN INSERT INTO site_activity.totals (time, total) SELECT CURRENT_TIMESTAMP, COUNT(*) FROM site_activity.sessions; DELETE FROM site_activity.sessions; END | delimiter ;
This example uses the 此示例使用delimiter
command to change the statement delimiter. delimiter
命令更改语句分隔符。See Section 25.1, “Defining Stored Programs”.请参阅第25.1节,“定义存储程序”。
More complex compound statements, such as those used in stored routines, are possible in an event. 在事件中可以使用更复杂的复合语句,如存储例程中使用的语句。This example uses local variables, an error handler, and a flow control construct:此示例使用局部变量、错误处理程序和流控制结构:
delimiter | CREATE EVENT e ON SCHEDULE EVERY 5 SECOND DO BEGIN DECLARE v INTEGER; DECLARE CONTINUE HANDLER FOR SQLEXCEPTION BEGIN END; SET v = 0; WHILE v < 5 DO INSERT INTO t1 VALUES (0); UPDATE t2 SET s1 = s1 + 1; SET v = v + 1; END WHILE; END | delimiter ;
There is no way to pass parameters directly to or from events; however, it is possible to invoke a stored routine with parameters within an event:无法直接向事件传递参数或从事件传递参数;但是,可以使用事件中的参数调用存储的例程:
CREATE EVENT e_call_myproc ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 DAY DO CALL myproc(5, 27);
If an event's definer has privileges sufficient to set global system variables (see Section 5.1.9.1, “System Variable Privileges”), the event can read and write global variables. 如果事件的定义人具有足够的权限设置全局系统变量(请参阅第5.1.9.1节,“系统变量权限”),则事件可以读取和写入全局变量。As granting such privileges entails a potential for abuse, extreme care must be taken in doing so.由于授予这种特权有可能被滥用,因此在这样做时必须格外小心。
Generally, any statements that are valid in stored routines may be used for action statements executed by events. 通常,在存储例程中有效的任何语句都可以用于事件执行的操作语句。For more information about statements permissible within stored routines, see Section 25.2.1, “Stored Routine Syntax”. 有关存储例程中允许的语句的更多信息,请参阅第25.2.1节,“存储例程语法”。You can create an event as part of a stored routine, but an event cannot be created by another event.您可以创建一个事件作为存储例程的一部分,但另一个事件不能创建一个事件。