<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h3 class="title">
      <a name="create-event">
      </a>
      15.1.13 CREATE EVENT Statement
     </h3>
    </div>
   </div>
  </div>
  <a class="indexterm" name="idm46045188279808">
  </a>
  <a class="indexterm" name="idm46045188278320">
  </a>
  <a class="indexterm" name="idm46045188276832">
  </a>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa19569262"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token keyword">CREATE</span>
    <span class="token punctuation">[</span><span class="token keyword">DEFINER</span> <span class="token operator">=</span> <span class="token keyword"><em class="replaceable">user</em></span><span class="token punctuation">]</span>
    <span class="token keyword">EVENT</span>
    <span class="token punctuation">[</span><span class="token keyword">IF</span> <span class="token operator">NOT</span> <span class="token keyword">EXISTS</span><span class="token punctuation">]</span>
    <em class="replaceable">event_name</em>
    <span class="token keyword">ON</span> <span class="token keyword">SCHEDULE</span> <span class="token keyword"><em class="replaceable">schedule</em></span>
    <span class="token punctuation">[</span><span class="token keyword">ON</span> <span class="token keyword">COMPLETION</span> <span class="token punctuation">[</span><span class="token operator">NOT</span><span class="token punctuation">]</span> <span class="token keyword">PRESERVE</span><span class="token punctuation">]</span>
    <span class="token punctuation">[</span><span class="token keyword">ENABLE</span> <span class="token operator">|</span> <span class="token keyword">DISABLE</span> <span class="token operator">|</span> <span class="token keyword">DISABLE</span> <span class="token keyword">ON</span> {<span class="token keyword">REPLICA</span> <span class="token operator">|</span> <span class="token keyword">SLAVE</span>}<span class="token punctuation">]</span>
    <span class="token punctuation">[</span><span class="token keyword">COMMENT</span> <span class="token string">'<em class="replaceable">string</em>'</span><span class="token punctuation">]</span>
    <span class="token keyword">DO</span> <em class="replaceable">event_body</em><span class="token punctuation">;</span>

<span class="token keyword"><em class="replaceable">schedule</em></span>: {
    <span class="token keyword">AT</span> <span class="token datatype"><em class="replaceable">timestamp</em></span> <span class="token punctuation">[</span><span class="token operator">+</span> <span class="token keyword">INTERVAL</span> <span class="token keyword"><em class="replaceable">interval</em></span><span class="token punctuation">]</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
  <span class="token operator">|</span> <span class="token keyword">EVERY</span> <span class="token keyword"><em class="replaceable">interval</em></span>
    <span class="token punctuation">[</span><span class="token keyword">STARTS</span> <span class="token datatype"><em class="replaceable">timestamp</em></span> <span class="token punctuation">[</span><span class="token operator">+</span> <span class="token keyword">INTERVAL</span> <span class="token keyword"><em class="replaceable">interval</em></span><span class="token punctuation">]</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">]</span>
    <span class="token punctuation">[</span><span class="token keyword">ENDS</span> <span class="token datatype"><em class="replaceable">timestamp</em></span> <span class="token punctuation">[</span><span class="token operator">+</span> <span class="token keyword">INTERVAL</span> <span class="token keyword"><em class="replaceable">interval</em></span><span class="token punctuation">]</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">]</span>
}

<span class="token keyword"><em class="replaceable">interval</em></span>:
    <em class="replaceable">quantity</em> {<span class="token datatype">YEAR</span> <span class="token operator">|</span> <span class="token keyword">QUARTER</span> <span class="token operator">|</span> <span class="token keyword">MONTH</span> <span class="token operator">|</span> <span class="token keyword">DAY</span> <span class="token operator">|</span> <span class="token keyword">HOUR</span> <span class="token operator">|</span> <span class="token keyword">MINUTE</span> <span class="token operator">|</span>
              <span class="token keyword">WEEK</span> <span class="token operator">|</span> <span class="token keyword">SECOND</span> <span class="token operator">|</span> <span class="token keyword">YEAR_MONTH</span> <span class="token operator">|</span> <span class="token keyword">DAY_HOUR</span> <span class="token operator">|</span> <span class="token keyword">DAY_MINUTE</span> <span class="token operator">|</span>
              <span class="token keyword">DAY_SECOND</span> <span class="token operator">|</span> <span class="token keyword">HOUR_MINUTE</span> <span class="token operator">|</span> <span class="token keyword">HOUR_SECOND</span> <span class="token operator">|</span> <span class="token keyword">MINUTE_SECOND</span>}</code></pre>
  </div>
  <p>
   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
   <a class="xref" href="events-configuration.html" title="27.4.2 Event Scheduler Configuration">
    Section 27.4.2, “Event Scheduler Configuration”
   </a>
   .
  </p>
  <p>
   <a class="link" href="create-event.html" title="15.1.13 CREATE EVENT Statement">
    <code class="literal">
     CREATE EVENT
    </code>
   </a>
   requires the
   <a class="link" href="privileges-provided.html#priv_event">
    <code class="literal">
     EVENT
    </code>
   </a>
   privilege for the schema in
      which the event is to be created. If the
   <code class="literal">
    DEFINER
   </code>
   clause is present, the privileges
      required depend on the
   <em class="replaceable">
    <code>
     user
    </code>
   </em>
   value, as
      discussed in
   <a class="xref" href="stored-objects-security.html" title="27.6 Stored Object Access Control">
    Section 27.6, “Stored Object Access Control”
   </a>
   .
  </p>
  <p>
   The minimum requirements for a valid
   <a class="link" href="create-event.html" title="15.1.13 CREATE EVENT Statement">
    <code class="literal">
     CREATE
      EVENT
    </code>
   </a>
   statement are as follows:
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      The keywords
      <a class="link" href="create-event.html" title="15.1.13 CREATE EVENT Statement">
       <code class="literal">
        CREATE EVENT
       </code>
      </a>
      plus
          an event name, which uniquely identifies the event in a
          database schema.
     </p>
    </li>
    <li class="listitem">
     <p>
      An
      <code class="literal">
       ON SCHEDULE
      </code>
      clause, which determines
          when and how often the event executes.
     </p>
    </li>
    <li class="listitem">
     <p>
      A
      <a class="link" href="do.html" title="15.2.3 DO Statement">
       <code class="literal">
        DO
       </code>
      </a>
      clause, which contains the
          SQL statement to be executed by an event.
     </p>
    </li>
   </ul>
  </div>
  <p>
   This is an example of a minimal
   <a class="link" href="create-event.html" title="15.1.13 CREATE EVENT Statement">
    <code class="literal">
     CREATE
      EVENT
    </code>
   </a>
   statement:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa85567510"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token keyword">CREATE</span> <span class="token keyword">EVENT</span> myevent
    <span class="token keyword">ON</span> <span class="token keyword">SCHEDULE</span> <span class="token keyword">AT</span> <span class="token keyword">CURRENT_TIMESTAMP</span> <span class="token operator">+</span> <span class="token keyword">INTERVAL</span> <span class="token number">1</span> <span class="token keyword">HOUR</span>
    <span class="token keyword">DO</span>
      <span class="token keyword">UPDATE</span> myschema<span class="token punctuation">.</span>mytable <span class="token keyword">SET</span> mycol <span class="token operator">=</span> mycol <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span></code></pre>
  </div>
  <p>
   The previous statement creates an event named
   <code class="literal">
    myevent
   </code>
   . This event executes once—one
      hour following its creation—by running an SQL statement that
      increments the value of the
   <code class="literal">
    myschema.mytable
   </code>
   table's
   <code class="literal">
    mycol
   </code>
   column by 1.
  </p>
  <p>
   The
   <em class="replaceable">
    <code>
     event_name
    </code>
   </em>
   must be a valid MySQL
      identifier with a maximum length of 64 characters. Event names are
      not case-sensitive, so you cannot have two events named
   <code class="literal">
    myevent
   </code>
   and
   <code class="literal">
    MyEvent
   </code>
   in the
      same schema. In general, the rules governing event names are the
      same as those for names of stored routines. See
   <a class="xref" href="identifiers.html" title="11.2 Schema Object Names">
    Section 11.2, “Schema Object Names”
   </a>
   .
  </p>
  <p>
   An event is associated with a schema. If no schema is indicated as
      part of
   <em class="replaceable">
    <code>
     event_name
    </code>
   </em>
   , the default
      (current) schema is assumed. To create an event in a specific
      schema, qualify the event name with a schema using
   <code class="literal">
    <em class="replaceable">
     <code>
      schema_name
     </code>
    </em>
    .
    <em class="replaceable">
     <code>
      event_name
     </code>
    </em>
   </code>
   syntax.
  </p>
  <p>
   The
   <code class="literal">
    DEFINER
   </code>
   clause specifies the MySQL account
      to be used when checking access privileges at event execution
      time. If the
   <code class="literal">
    DEFINER
   </code>
   clause is present, the
   <em class="replaceable">
    <code>
     user
    </code>
   </em>
   value should be a MySQL account
      specified as
   <code class="literal">
    '
    <em class="replaceable">
     <code>
      user_name
     </code>
    </em>
    '@'
    <em class="replaceable">
     <code>
      host_name
     </code>
    </em>
    '
   </code>
   ,
   <a class="link" href="information-functions.html#function_current-user">
    <code class="literal">
     CURRENT_USER
    </code>
   </a>
   , or
   <a class="link" href="information-functions.html#function_current-user">
    <code class="literal">
     CURRENT_USER()
    </code>
   </a>
   . The permitted
   <em class="replaceable">
    <code>
     user
    </code>
   </em>
   values depend on the privileges
      you hold, as discussed in
   <a class="xref" href="stored-objects-security.html" title="27.6 Stored Object Access Control">
    Section 27.6, “Stored Object Access Control”
   </a>
   . Also see that section
      for additional information about event security.
  </p>
  <p>
   If the
   <code class="literal">
    DEFINER
   </code>
   clause is omitted, the default
      definer is the user who executes the
   <a class="link" href="create-event.html" title="15.1.13 CREATE EVENT Statement">
    <code class="literal">
     CREATE
      EVENT
    </code>
   </a>
   statement. This is the same as specifying
   <code class="literal">
    DEFINER = CURRENT_USER
   </code>
   explicitly.
  </p>
  <p>
   Within an event body, the
   <a class="link" href="information-functions.html#function_current-user">
    <code class="literal">
     CURRENT_USER
    </code>
   </a>
   function returns the
      account used to check privileges at event execution time, which is
      the
   <code class="literal">
    DEFINER
   </code>
   user. For information about user
      auditing within events, see
   <a class="xref" href="account-activity-auditing.html" title="8.2.23 SQL-Based Account Activity Auditing">
    Section 8.2.23, “SQL-Based Account Activity Auditing”
   </a>
   .
  </p>
  <p>
   <code class="literal">
    IF NOT EXISTS
   </code>
   has the same meaning for
   <a class="link" href="create-event.html" title="15.1.13 CREATE EVENT Statement">
    <code class="literal">
     CREATE EVENT
    </code>
   </a>
   as for
   <a class="link" href="create-table.html" title="15.1.20 CREATE TABLE Statement">
    <code class="literal">
     CREATE TABLE
    </code>
   </a>
   : If an event named
   <em class="replaceable">
    <code>
     event_name
    </code>
   </em>
   already exists in the same
      schema, no action is taken, and no error results. (However, a
      warning is generated in such cases.)
  </p>
  <p>
   The
   <code class="literal">
    ON SCHEDULE
   </code>
   clause determines when, how
      often, and for how long the
   <em class="replaceable">
    <code>
     event_body
    </code>
   </em>
   defined for the event repeats. This clause takes one of two forms:
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      <code class="literal">
       AT
       <em class="replaceable">
        <code>
         timestamp
        </code>
       </em>
      </code>
      is
          used for a one-time event. It specifies that the event
          executes one time only at the date and time given by
      <em class="replaceable">
       <code>
        timestamp
       </code>
      </em>
      , which must include both
          the date and time, or must be an expression that resolves to a
          datetime value. You may use a value of either the
      <a class="link" href="datetime.html" title="13.2.2 The DATE, DATETIME, and TIMESTAMP Types">
       <code class="literal">
        DATETIME
       </code>
      </a>
      or
      <a class="link" href="datetime.html" title="13.2.2 The DATE, DATETIME, and TIMESTAMP Types">
       <code class="literal">
        TIMESTAMP
       </code>
      </a>
      type for this
          purpose. If the date is in the past, a warning occurs, as
          shown here:
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa88247920"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token function">NOW</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> NOW()               <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> 2006<span class="token punctuation">-</span>02<span class="token punctuation">-</span>10 23:59:01 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output">1 row in set (0.04 sec)</span>

<span class="token prompt">mysql&gt;</span> <span class="token keyword">CREATE</span> <span class="token keyword">EVENT</span> e_totals
<span class="token prompt">    -&gt;</span>     <span class="token keyword">ON</span> <span class="token keyword">SCHEDULE</span> <span class="token keyword">AT</span> <span class="token string">'2006-02-10 23:59:00'</span>
<span class="token prompt">    -&gt;</span>     <span class="token keyword">DO</span> <span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> test<span class="token punctuation">.</span>totals <span class="token keyword">VALUES</span> <span class="token punctuation">(</span><span class="token function">NOW</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token output">Query OK, 0 rows affected, 1 warning (0.00 sec)</span>

<span class="token prompt">mysql&gt;</span> <span class="token keyword">SHOW</span> <span class="token keyword">WARNINGS</span>\G
<span class="token output"><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span> 1. row <span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span>
  Level<span class="token punctuation">:</span> Note
   Code<span class="token punctuation">:</span> 1588
Message<span class="token punctuation">:</span> Event execution time is in the past and ON COMPLETION NOT
         PRESERVE is set. The event was dropped immediately after
         creation.</span></code></pre>
     </div>
     <p>
      <a class="link" href="create-event.html" title="15.1.13 CREATE EVENT Statement">
       <code class="literal">
        CREATE EVENT
       </code>
      </a>
      statements which
          are themselves invalid—for whatever reason—fail
          with an error.
     </p>
     <p>
      You may use
      <a class="link" href="date-and-time-functions.html#function_current-timestamp">
       <code class="literal">
        CURRENT_TIMESTAMP
       </code>
      </a>
      to specify the current date and time. In such a case, the
          event acts as soon as it is created.
     </p>
     <p>
      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
      <span class="quote">
       “
       <span class="quote">
        three weeks from
          now
       </span>
       ”
      </span>
      —you can use the optional clause
      <code class="literal">
       +
          INTERVAL
       <em class="replaceable">
        <code>
         interval
        </code>
       </em>
      </code>
      . The
      <em class="replaceable">
       <code>
        interval
       </code>
      </em>
      portion consists of two
          parts, a quantity and a unit of time, and follows the syntax
          rules described in
      <a class="xref" href="expressions.html#temporal-intervals" title="Temporal Intervals">
       Temporal Intervals
      </a>
      ,
          except that you cannot use any units keywords that involving
          microseconds when defining an event. With some interval types,
          complex time units may be used. For example,
      <span class="quote">
       “
       <span class="quote">
        two
          minutes and ten seconds
       </span>
       ”
      </span>
      can be expressed as
      <code class="literal">
       +
          INTERVAL '2:10' MINUTE_SECOND
      </code>
      .
     </p>
     <p>
      You can also combine intervals. For example,
      <code class="literal">
       AT
          CURRENT_TIMESTAMP + INTERVAL 3 WEEK + INTERVAL 2 DAY
      </code>
      is equivalent to
      <span class="quote">
       “
       <span class="quote">
        three weeks and two days from
          now
       </span>
       ”
      </span>
      . Each portion of such a clause must begin with
      <code class="literal">
       + INTERVAL
      </code>
      .
     </p>
    </li>
    <li class="listitem">
     <p>
      To repeat actions at a regular interval, use an
      <code class="literal">
       EVERY
      </code>
      clause. The
      <code class="literal">
       EVERY
      </code>
      keyword is followed by an
      <em class="replaceable">
       <code>
        interval
       </code>
      </em>
      as described in the previous discussion of the
      <code class="literal">
       AT
      </code>
      keyword. (
      <code class="literal">
       + INTERVAL
      </code>
      is
      <span class="emphasis">
       <em>
        not
       </em>
      </span>
      used with
      <code class="literal">
       EVERY
      </code>
      .) For example,
      <code class="literal">
       EVERY 6
          WEEK
      </code>
      means
      <span class="quote">
       “
       <span class="quote">
        every six weeks
       </span>
       ”
      </span>
      .
     </p>
     <p>
      Although
      <code class="literal">
       + INTERVAL
      </code>
      clauses are not
          permitted in an
      <code class="literal">
       EVERY
      </code>
      clause, you can use
          the same complex time units permitted in a
      <code class="literal">
       +
          INTERVAL
      </code>
      .
     </p>
     <p>
      An
      <code class="literal">
       EVERY
      </code>
      clause may contain an optional
      <code class="literal">
       STARTS
      </code>
      clause.
      <code class="literal">
       STARTS
      </code>
      is
          followed by a
      <em class="replaceable">
       <code>
        timestamp
       </code>
      </em>
      value that
          indicates when the action should begin repeating, and may also
          use
      <code class="literal">
       + INTERVAL
       <em class="replaceable">
        <code>
         interval
        </code>
       </em>
      </code>
      to specify an
          amount of time
      <span class="quote">
       “
       <span class="quote">
        from now
       </span>
       ”
      </span>
      . For example,
      <code class="literal">
       EVERY 3 MONTH STARTS CURRENT_TIMESTAMP + INTERVAL 1
          WEEK
      </code>
      means
      <span class="quote">
       “
       <span class="quote">
        every three months, beginning one
          week from now
       </span>
       ”
      </span>
      . Similarly, you can express
      <span class="quote">
       “
       <span class="quote">
        every
          two weeks, beginning six hours and fifteen minutes from
          now
       </span>
       ”
      </span>
      as
      <code class="literal">
       EVERY 2 WEEK STARTS CURRENT_TIMESTAMP
          + INTERVAL '6:15' HOUR_MINUTE
      </code>
      . Not specifying
      <code class="literal">
       STARTS
      </code>
      is the same as using
      <code class="literal">
       STARTS
          CURRENT_TIMESTAMP
      </code>
      —that is, the action
          specified for the event begins repeating immediately upon
          creation of the event.
     </p>
     <p>
      An
      <code class="literal">
       EVERY
      </code>
      clause may contain an optional
      <code class="literal">
       ENDS
      </code>
      clause. The
      <code class="literal">
       ENDS
      </code>
      keyword is followed by a
      <em class="replaceable">
       <code>
        timestamp
       </code>
      </em>
      value that tells MySQL when the event should stop repeating.
          You may also use
      <code class="literal">
       + INTERVAL
       <em class="replaceable">
        <code>
         interval
        </code>
       </em>
      </code>
      with
      <code class="literal">
       ENDS
      </code>
      ; for instance,
      <code class="literal">
       EVERY 12 HOUR
          STARTS CURRENT_TIMESTAMP + INTERVAL 30 MINUTE ENDS
          CURRENT_TIMESTAMP + INTERVAL 4 WEEK
      </code>
      is equivalent to
      <span class="quote">
       “
       <span class="quote">
        every twelve hours, beginning thirty minutes from now,
          and ending four weeks from now
       </span>
       ”
      </span>
      . Not using
      <code class="literal">
       ENDS
      </code>
      means that the event continues
          executing indefinitely.
     </p>
     <p>
      <code class="literal">
       ENDS
      </code>
      supports the same syntax for complex
          time units as
      <code class="literal">
       STARTS
      </code>
      does.
     </p>
     <p>
      You may use
      <code class="literal">
       STARTS
      </code>
      ,
      <code class="literal">
       ENDS
      </code>
      , both, or neither in an
      <code class="literal">
       EVERY
      </code>
      clause.
     </p>
     <p>
      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
      <a class="link" href="locking-functions.html#function_get-lock">
       <code class="literal">
        GET_LOCK()
       </code>
      </a>
      function, or row or
          table locking.
     </p>
    </li>
   </ul>
  </div>
  <p>
   The
   <code class="literal">
    ON SCHEDULE
   </code>
   clause may use expressions
      involving built-in MySQL functions and user variables to obtain
      any of the
   <em class="replaceable">
    <code>
     timestamp
    </code>
   </em>
   or
   <em class="replaceable">
    <code>
     interval
    </code>
   </em>
   values which it contains. You
      may not use stored functions or loadable functions in such
      expressions, nor may you use any table references; however, you
      may use
   <code class="literal">
    SELECT FROM DUAL
   </code>
   . This is true for both
   <a class="link" href="create-event.html" title="15.1.13 CREATE EVENT Statement">
    <code class="literal">
     CREATE EVENT
    </code>
   </a>
   and
   <a class="link" href="alter-event.html" title="15.1.3 ALTER EVENT Statement">
    <code class="literal">
     ALTER EVENT
    </code>
   </a>
   statements. References
      to stored functions, loadable functions, and tables in such cases
      are specifically not permitted, and fail with an error (see Bug
      #22830).
  </p>
  <p>
   Times in the
   <code class="literal">
    ON SCHEDULE
   </code>
   clause are interpreted
      using the current session
   <a class="link" href="server-system-variables.html#sysvar_time_zone">
    <code class="literal">
     time_zone
    </code>
   </a>
   value. 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. 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
   <a class="xref" href="events-metadata.html" title="27.4.4 Event Metadata">
    Section 27.4.4, “Event Metadata”
   </a>
   . See also
   <a class="xref" href="show-events.html" title="15.7.7.19 SHOW EVENTS Statement">
    Section 15.7.7.19, “SHOW EVENTS Statement”
   </a>
   , and
   <a class="xref" href="information-schema-events-table.html" title="28.3.14 The INFORMATION_SCHEMA EVENTS Table">
    Section 28.3.14, “The INFORMATION_SCHEMA EVENTS Table”
   </a>
   .
  </p>
  <p>
   Normally, once an event has expired, it is immediately dropped.
      You can override this behavior by specifying
   <code class="literal">
    ON
      COMPLETION PRESERVE
   </code>
   . Using
   <code class="literal">
    ON COMPLETION NOT
      PRESERVE
   </code>
   merely makes the default nonpersistent behavior
      explicit.
  </p>
  <p>
   You can create an event but prevent it from being active using the
   <code class="literal">
    DISABLE
   </code>
   keyword. Alternatively, you can use
   <code class="literal">
    ENABLE
   </code>
   to make explicit the default status,
      which is active. This is most useful in conjunction with
   <a class="link" href="alter-event.html" title="15.1.3 ALTER EVENT Statement">
    <code class="literal">
     ALTER EVENT
    </code>
   </a>
   (see
   <a class="xref" href="alter-event.html" title="15.1.3 ALTER EVENT Statement">
    Section 15.1.3, “ALTER EVENT Statement”
   </a>
   ).
  </p>
  <p>
   A third value may also appear in place of
   <code class="literal">
    ENABLE
   </code>
   or
   <code class="literal">
    DISABLE
   </code>
   ;
   <code class="literal">
    DISABLE ON REPLICA
   </code>
   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. See
   <a class="xref" href="replication-features-invoked.html" title="19.5.1.16 Replication of Invoked Features">
    Section 19.5.1.16, “Replication of Invoked Features”
   </a>
   .
  </p>
  <p>
   <code class="literal">
    DISABLE ON REPLICA
   </code>
   replaces
   <code class="literal">
    DISABLE ON
      SLAVE
   </code>
   , which is deprecated, and thus subject to removal
      in a future version of MySQL.
  </p>
  <p>
   You may supply a comment for an event using a
   <code class="literal">
    COMMENT
   </code>
   clause.
   <em class="replaceable">
    <code>
     comment
    </code>
   </em>
   may be any string of up to 64
      characters that you wish to use for describing the event. The
      comment text, being a string literal, must be surrounded by
      quotation marks.
  </p>
  <p>
   The
   <a class="link" href="do.html" title="15.2.3 DO Statement">
    <code class="literal">
     DO
    </code>
   </a>
   clause specifies an action
      carried by the event, and consists of an SQL statement. 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. (See
   <a class="xref" href="stored-program-restrictions.html" title="27.8 Restrictions on Stored Programs">
    Section 27.8, “Restrictions on Stored Programs”
   </a>
   .) For example, the
      following event
   <code class="literal">
    e_hourly
   </code>
   deletes all rows from
      the
   <code class="literal">
    sessions
   </code>
   table once per hour, where this
      table is part of the
   <code class="literal">
    site_activity
   </code>
   schema:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa77594483"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token keyword">CREATE</span> <span class="token keyword">EVENT</span> e_hourly
    <span class="token keyword">ON</span> <span class="token keyword">SCHEDULE</span>
      <span class="token keyword">EVERY</span> <span class="token number">1</span> <span class="token keyword">HOUR</span>
    <span class="token keyword">COMMENT</span> <span class="token string">'Clears out sessions table each hour.'</span>
    <span class="token keyword">DO</span>
      <span class="token keyword">DELETE</span> <span class="token keyword">FROM</span> site_activity<span class="token punctuation">.</span>sessions<span class="token punctuation">;</span></code></pre>
  </div>
  <p>
   MySQL stores the
   <a class="link" href="server-system-variables.html#sysvar_sql_mode">
    <code class="literal">
     sql_mode
    </code>
   </a>
   system
      variable setting in effect when an event is created or altered,
      and always executes the event with this setting in force,
   <span class="emphasis">
    <em>
     regardless of the current server SQL mode when the event
      begins executing
    </em>
   </span>
   .
  </p>
  <p>
   A
   <a class="link" href="create-event.html" title="15.1.13 CREATE EVENT Statement">
    <code class="literal">
     CREATE EVENT
    </code>
   </a>
   statement that
      contains an
   <a class="link" href="alter-event.html" title="15.1.3 ALTER EVENT Statement">
    <code class="literal">
     ALTER EVENT
    </code>
   </a>
   statement
      in its
   <a class="link" href="do.html" title="15.2.3 DO Statement">
    <code class="literal">
     DO
    </code>
   </a>
   clause appears to
      succeed; however, when the server attempts to execute the
      resulting scheduled event, the execution fails with an error.
  </p>
  <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
   <div class="admon-title">
    Note
   </div>
   <p>
    Statements such as
    <a class="link" href="select.html" title="15.2.13 SELECT Statement">
     <code class="literal">
      SELECT
     </code>
    </a>
    or
    <a class="link" href="show.html" title="15.7.7 SHOW Statements">
     <code class="literal">
      SHOW
     </code>
    </a>
    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.
        However, you can use statements such as
    <a class="link" href="select.html" title="15.2.13 SELECT Statement">
     <code class="literal">
      SELECT ...
        INTO
     </code>
    </a>
    and
    <a class="link" href="insert-select.html" title="15.2.7.1 INSERT ... SELECT Statement">
     <code class="literal">
      INSERT INTO ...
        SELECT
     </code>
    </a>
    that store a result. (See the next example in
        this section for an instance of the latter.)
   </p>
  </div>
  <p>
   The schema to which an event belongs is the default schema for
      table references in the
   <a class="link" href="do.html" title="15.2.3 DO Statement">
    <code class="literal">
     DO
    </code>
   </a>
   clause.
      Any references to tables in other schemas must be qualified with
      the proper schema name.
  </p>
  <p>
   As with stored routines, you can use compound-statement syntax in
      the
   <a class="link" href="do.html" title="15.2.3 DO Statement">
    <code class="literal">
     DO
    </code>
   </a>
   clause by using the
   <code class="literal">
    BEGIN
   </code>
   and
   <code class="literal">
    END
   </code>
   keywords, as
      shown here:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa38352100"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token keyword">delimiter</span> <span class="token operator">|</span>

<span class="token keyword">CREATE</span> <span class="token keyword">EVENT</span> e_daily
    <span class="token keyword">ON</span> <span class="token keyword">SCHEDULE</span>
      <span class="token keyword">EVERY</span> <span class="token number">1</span> <span class="token keyword">DAY</span>
    <span class="token keyword">COMMENT</span> <span class="token string">'Saves total number of sessions then clears the table each day'</span>
    <span class="token keyword">DO</span>
      <span class="token keyword">BEGIN</span>
        <span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> site_activity<span class="token punctuation">.</span>totals <span class="token punctuation">(</span><span class="token datatype">time</span><span class="token punctuation">,</span> total<span class="token punctuation">)</span>
          <span class="token keyword">SELECT</span> <span class="token keyword">CURRENT_TIMESTAMP</span><span class="token punctuation">,</span> <span class="token function">COUNT</span><span class="token punctuation">(</span><span class="token operator">*</span><span class="token punctuation">)</span>
            <span class="token keyword">FROM</span> site_activity<span class="token punctuation">.</span>sessions<span class="token punctuation">;</span>
        <span class="token keyword">DELETE</span> <span class="token keyword">FROM</span> site_activity<span class="token punctuation">.</span>sessions<span class="token punctuation">;</span>
      <span class="token keyword">END</span> <span class="token operator">|</span>

<span class="token keyword">delimiter</span> <span class="token punctuation">;</span></code></pre>
  </div>
  <p>
   This example uses the
   <code class="literal">
    delimiter
   </code>
   command to
      change the statement delimiter. See
   <a class="xref" href="stored-programs-defining.html" title="27.1 Defining Stored Programs">
    Section 27.1, “Defining Stored Programs”
   </a>
   .
  </p>
  <p>
   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:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa95430080"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token keyword">delimiter</span> <span class="token operator">|</span>

<span class="token keyword">CREATE</span> <span class="token keyword">EVENT</span> e
    <span class="token keyword">ON</span> <span class="token keyword">SCHEDULE</span>
      <span class="token keyword">EVERY</span> <span class="token number">5</span> <span class="token keyword">SECOND</span>
    <span class="token keyword">DO</span>
      <span class="token keyword">BEGIN</span>
        <span class="token keyword">DECLARE</span> v <span class="token datatype">INTEGER</span><span class="token punctuation">;</span>
        <span class="token keyword">DECLARE</span> <span class="token keyword">CONTINUE</span> <span class="token keyword">HANDLER</span> <span class="token keyword">FOR</span> <span class="token keyword">SQLEXCEPTION</span> <span class="token keyword">BEGIN</span> <span class="token keyword">END</span><span class="token punctuation">;</span>

        <span class="token keyword">SET</span> v <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>

        <span class="token keyword">WHILE</span> v <span class="token operator">&lt;</span> <span class="token number">5</span> <span class="token keyword">DO</span>
          <span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> t1 <span class="token keyword">VALUES</span> <span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
          <span class="token keyword">UPDATE</span> t2 <span class="token keyword">SET</span> s1 <span class="token operator">=</span> s1 <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span>
          <span class="token keyword">SET</span> v <span class="token operator">=</span> v <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span>
        <span class="token keyword">END</span> <span class="token keyword">WHILE</span><span class="token punctuation">;</span>
    <span class="token keyword">END</span> <span class="token operator">|</span>

<span class="token keyword">delimiter</span> <span class="token punctuation">;</span></code></pre>
  </div>
  <p>
   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:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa66877583"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token keyword">CREATE</span> <span class="token keyword">EVENT</span> e_call_myproc
    <span class="token keyword">ON</span> <span class="token keyword">SCHEDULE</span>
      <span class="token keyword">AT</span> <span class="token keyword">CURRENT_TIMESTAMP</span> <span class="token operator">+</span> <span class="token keyword">INTERVAL</span> <span class="token number">1</span> <span class="token keyword">DAY</span>
    <span class="token keyword">DO</span> <span class="token keyword">CALL</span> myproc<span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">27</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
  </div>
  <p>
   If an event's definer has privileges sufficient to set global
      system variables (see
   <a class="xref" href="system-variable-privileges.html" title="7.1.9.1 System Variable Privileges">
    Section 7.1.9.1, “System Variable Privileges”
   </a>
   ), the event can read
      and write global variables. As granting such privileges entails a
      potential for abuse, extreme care must be taken in doing so.
  </p>
  <p>
   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
   <a class="xref" href="stored-routines-syntax.html" title="27.2.1 Stored Routine Syntax">
    Section 27.2.1, “Stored Routine Syntax”
   </a>
   . It is not possible
      to create an event as part of a stored routine or to create an
      event by another event.
  </p>
 </div>
 <br/>
</div>
