<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h2 class="title">
      <a name="faqs-triggers">
      </a>
      A.5 MySQL 8.4 FAQ: Triggers
     </h2>
    </div>
   </div>
  </div>
  <div class="qandaset">
   <a name="idm46045056252976">
   </a>
   <dl>
    <dt>
     A.5.1.
     <a href="faqs-triggers.html#faq-mysql-where-triggers-docs">
      Where can I find the documentation for MySQL 8.4
        triggers?
     </a>
    </dt>
    <dt>
     A.5.2.
     <a href="faqs-triggers.html#faq-mysql-where-triggers-forum">
      Is there a discussion forum for MySQL Triggers?
     </a>
    </dt>
    <dt>
     A.5.3.
     <a href="faqs-triggers.html#faq-mysql-have-trigger-levels">
      Does MySQL have statement-level or row-level triggers?
     </a>
    </dt>
    <dt>
     A.5.4.
     <a href="faqs-triggers.html#faq-mysql-have-trigger-defaults">
      Are there any default triggers?
     </a>
    </dt>
    <dt>
     A.5.5.
     <a href="faqs-triggers.html#faq-mysql-how-triggers-managed">
      How are triggers managed in MySQL?
     </a>
    </dt>
    <dt>
     A.5.6.
     <a href="faqs-triggers.html#faq-mysql-can-view-all-triggers">
      Is there a way to view all triggers in a given database?
     </a>
    </dt>
    <dt>
     A.5.7.
     <a href="faqs-triggers.html#faq-mysql-how-triggers-stored">
      Where are triggers stored?
     </a>
    </dt>
    <dt>
     A.5.8.
     <a href="faqs-triggers.html#faq-mysql-can-trigger-procedure">
      Can a trigger call a stored procedure?
     </a>
    </dt>
    <dt>
     A.5.9.
     <a href="faqs-triggers.html#faq-mysql-can-triggers-tables">
      Can triggers access tables?
     </a>
    </dt>
    <dt>
     A.5.10.
     <a href="faqs-triggers.html#faq-mysql-can-triggers-same-events">
      Can a table have multiple triggers with the same trigger event
        and action time?
     </a>
    </dt>
    <dt>
     A.5.11.
     <a href="faqs-triggers.html#faq-mysql-can-triggers-remote">
      Is it possible for a trigger to update tables on a remote
        server?
     </a>
    </dt>
    <dt>
     A.5.12.
     <a href="faqs-triggers.html#faq-mysql-can-triggers-replication">
      Do triggers work with replication?
     </a>
    </dt>
    <dt>
     A.5.13.
     <a href="faqs-triggers.html#faq-mysql-how-triggers-source-replica">
      How are actions carried out through triggers on a source
        replicated to a replica?
     </a>
    </dt>
   </dl>
   <table border="0" style="width: 100%;">
    <colgroup>
     <col align="left" width="1%"/>
     <col/>
    </colgroup>
    <tbody>
     <tr class="question">
      <td align="left" valign="top">
       <a name="faq-mysql-where-triggers-docs">
       </a>
       <a name="idm46045056252224">
       </a>
       <p>
        <b>
         A.5.1.
        </b>
       </p>
      </td>
      <td align="left" valign="top">
       <p>
        Where can I find the documentation for MySQL 8.4
        triggers?
       </p>
      </td>
     </tr>
     <tr class="answer">
      <td align="left" valign="top">
      </td>
      <td align="left" valign="top">
       <p>
        See
        <a class="xref" href="triggers.html" title="27.3 Using Triggers">
         Section 27.3, “Using Triggers”
        </a>
        .
       </p>
      </td>
     </tr>
     <tr class="question">
      <td align="left" valign="top">
       <a name="faq-mysql-where-triggers-forum">
       </a>
       <a name="idm46045056249568">
       </a>
       <p>
        <b>
         A.5.2.
        </b>
       </p>
      </td>
      <td align="left" valign="top">
       <p>
        Is there a discussion forum for MySQL Triggers?
       </p>
      </td>
     </tr>
     <tr class="answer">
      <td align="left" valign="top">
      </td>
      <td align="left" valign="top">
       <p>
        Yes. It is available at
        <a class="ulink" href="https://forums.mysql.com/list.php?99" target="_blank">
         https://forums.mysql.com/list.php?99
        </a>
        .
       </p>
      </td>
     </tr>
     <tr class="question">
      <td align="left" valign="top">
       <a name="faq-mysql-have-trigger-levels">
       </a>
       <a name="idm46045056247008">
       </a>
       <p>
        <b>
         A.5.3.
        </b>
       </p>
      </td>
      <td align="left" valign="top">
       <p>
        Does MySQL have statement-level or row-level triggers?
       </p>
      </td>
     </tr>
     <tr class="answer">
      <td align="left" valign="top">
      </td>
      <td align="left" valign="top">
       <p>
        All triggers are
        <code class="literal">
         FOR EACH ROW
        </code>
        ; that is, the
        trigger is activated for each row that is inserted, updated, or
        deleted. MySQL does not support triggers using
        <code class="literal">
         FOR EACH
        STATEMENT
        </code>
        .
       </p>
      </td>
     </tr>
     <tr class="question">
      <td align="left" valign="top">
       <a name="faq-mysql-have-trigger-defaults">
       </a>
       <a name="idm46045056243376">
       </a>
       <p>
        <b>
         A.5.4.
        </b>
       </p>
      </td>
      <td align="left" valign="top">
       <p>
        Are there any default triggers?
       </p>
      </td>
     </tr>
     <tr class="answer">
      <td align="left" valign="top">
      </td>
      <td align="left" valign="top">
       <p>
        Not explicitly. MySQL does have specific special behavior for
        some
        <a class="link" href="datetime.html" title="13.2.2 The DATE, DATETIME, and TIMESTAMP Types">
         <code class="literal">
          TIMESTAMP
         </code>
        </a>
        columns, as well
        as for columns which are defined using
        <code class="literal">
         AUTO_INCREMENT
        </code>
        .
       </p>
      </td>
     </tr>
     <tr class="question">
      <td align="left" valign="top">
       <a name="faq-mysql-how-triggers-managed">
       </a>
       <a name="idm46045056239344">
       </a>
       <p>
        <b>
         A.5.5.
        </b>
       </p>
      </td>
      <td align="left" valign="top">
       <p>
        How are triggers managed in MySQL?
       </p>
      </td>
     </tr>
     <tr class="answer">
      <td align="left" valign="top">
      </td>
      <td align="left" valign="top">
       <p>
        Triggers can be created using the
        <a class="link" href="create-trigger.html" title="15.1.22 CREATE TRIGGER Statement">
         <code class="literal">
          CREATE
        TRIGGER
         </code>
        </a>
        statement, and dropped using
        <a class="link" href="drop-trigger.html" title="15.1.34 DROP TRIGGER Statement">
         <code class="literal">
          DROP TRIGGER
         </code>
        </a>
        . See
        <a class="xref" href="create-trigger.html" title="15.1.22 CREATE TRIGGER Statement">
         Section 15.1.22, “CREATE TRIGGER Statement”
        </a>
        , and
        <a class="xref" href="drop-trigger.html" title="15.1.34 DROP TRIGGER Statement">
         Section 15.1.34, “DROP TRIGGER Statement”
        </a>
        , for more about these statements.
       </p>
       <p>
        Information about triggers can be obtained by querying the
        <a class="link" href="information-schema-triggers-table.html" title="28.3.44 The INFORMATION_SCHEMA TRIGGERS Table">
         <code class="literal">
          INFORMATION_SCHEMA.TRIGGERS
         </code>
        </a>
        table.
        See
        <a class="xref" href="information-schema-triggers-table.html" title="28.3.44 The INFORMATION_SCHEMA TRIGGERS Table">
         Section 28.3.44, “The INFORMATION_SCHEMA TRIGGERS Table”
        </a>
        .
       </p>
      </td>
     </tr>
     <tr class="question">
      <td align="left" valign="top">
       <a name="faq-mysql-can-view-all-triggers">
       </a>
       <a name="idm46045056230976">
       </a>
       <p>
        <b>
         A.5.6.
        </b>
       </p>
      </td>
      <td align="left" valign="top">
       <p>
        Is there a way to view all triggers in a given database?
       </p>
      </td>
     </tr>
     <tr class="answer">
      <td align="left" valign="top">
      </td>
      <td align="left" valign="top">
       <p>
        Yes. You can obtain a listing of all triggers defined on
        database
        <code class="literal">
         dbname
        </code>
        using a query on the
        <a class="link" href="information-schema-triggers-table.html" title="28.3.44 The INFORMATION_SCHEMA TRIGGERS Table">
         <code class="literal">
          INFORMATION_SCHEMA.TRIGGERS
         </code>
        </a>
        table
        such as the one shown here:
       </p>
       <div class="copytoclipboard-wrapper">
        <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa63395923"><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">SELECT</span> TRIGGER_NAME<span class="token punctuation">,</span> EVENT_MANIPULATION<span class="token punctuation">,</span> EVENT_OBJECT_TABLE<span class="token punctuation">,</span> ACTION_STATEMENT
    <span class="token keyword">FROM</span> INFORMATION_SCHEMA<span class="token punctuation">.</span><span class="token keyword">TRIGGERS</span>
    <span class="token keyword">WHERE</span> TRIGGER_SCHEMA<span class="token operator">=</span><span class="token string">'<em class="replaceable">dbname</em>'</span><span class="token punctuation">;</span></code></pre>
       </div>
       <p>
        For more information about this table, see
        <a class="xref" href="information-schema-triggers-table.html" title="28.3.44 The INFORMATION_SCHEMA TRIGGERS Table">
         Section 28.3.44, “The INFORMATION_SCHEMA TRIGGERS Table”
        </a>
        .
       </p>
       <p>
        You can also use the
        <a class="link" href="show-triggers.html" title="15.7.7.40 SHOW TRIGGERS Statement">
         <code class="literal">
          SHOW
        TRIGGERS
         </code>
        </a>
        statement, which is specific to MySQL. See
        <a class="xref" href="show-triggers.html" title="15.7.7.40 SHOW TRIGGERS Statement">
         Section 15.7.7.40, “SHOW TRIGGERS Statement”
        </a>
        .
       </p>
      </td>
     </tr>
     <tr class="question">
      <td align="left" valign="top">
       <a name="faq-mysql-how-triggers-stored">
       </a>
       <a name="idm46045056221808">
       </a>
       <p>
        <b>
         A.5.7.
        </b>
       </p>
      </td>
      <td align="left" valign="top">
       <p>
        Where are triggers stored?
       </p>
      </td>
     </tr>
     <tr class="answer">
      <td align="left" valign="top">
      </td>
      <td align="left" valign="top">
       <p>
        Triggers are stored in the
        <code class="literal">
         mysql.triggers
        </code>
        system table, which is part of the data dictionary.
       </p>
      </td>
     </tr>
     <tr class="question">
      <td align="left" valign="top">
       <a name="faq-mysql-can-trigger-procedure">
       </a>
       <a name="idm46045056219008">
       </a>
       <p>
        <b>
         A.5.8.
        </b>
       </p>
      </td>
      <td align="left" valign="top">
       <p>
        Can a trigger call a stored procedure?
       </p>
      </td>
     </tr>
     <tr class="answer">
      <td align="left" valign="top">
      </td>
      <td align="left" valign="top">
       <p>
        Yes.
       </p>
      </td>
     </tr>
     <tr class="question">
      <td align="left" valign="top">
       <a name="faq-mysql-can-triggers-tables">
       </a>
       <a name="idm46045056216992">
       </a>
       <p>
        <b>
         A.5.9.
        </b>
       </p>
      </td>
      <td align="left" valign="top">
       <p>
        Can triggers access tables?
       </p>
      </td>
     </tr>
     <tr class="answer">
      <td align="left" valign="top">
      </td>
      <td align="left" valign="top">
       <p>
        A trigger can access both old and new data in its own table. A
        trigger can also affect other tables, but it is not permitted to
        modify a table that is already being used (for reading or
        writing) by the statement that invoked the function or trigger.
       </p>
      </td>
     </tr>
     <tr class="question">
      <td align="left" valign="top">
       <a name="faq-mysql-can-triggers-same-events">
       </a>
       <a name="idm46045056214704">
       </a>
       <p>
        <b>
         A.5.10.
        </b>
       </p>
      </td>
      <td align="left" valign="top">
       <p>
        Can a table have multiple triggers with the same trigger event
        and action time?
       </p>
      </td>
     </tr>
     <tr class="answer">
      <td align="left" valign="top">
      </td>
      <td align="left" valign="top">
       <p>
        In MySQL 8.4, it is possible to define multiple
        triggers for a given table that have the same trigger event and
        action time. For example, you can have two
        <code class="literal">
         BEFORE
        UPDATE
        </code>
        triggers for a table. By default, triggers that
        have the same trigger event and action time activate in the
        order they were created. To affect trigger order, specify a
        clause after
        <code class="literal">
         FOR EACH ROW
        </code>
        that indicates
        <code class="literal">
         FOLLOWS
        </code>
        or
        <code class="literal">
         PRECEDES
        </code>
        and
        the name of an existing trigger that also has the same trigger
        event and action time. With
        <code class="literal">
         FOLLOWS
        </code>
        , the new
        trigger activates after the existing trigger. With
        <code class="literal">
         PRECEDES
        </code>
        , the new trigger activates before
        the existing trigger.
       </p>
      </td>
     </tr>
     <tr class="question">
      <td align="left" valign="top">
       <a name="faq-mysql-can-triggers-remote">
       </a>
       <a name="idm46045056207744">
       </a>
       <p>
        <b>
         A.5.11.
        </b>
       </p>
      </td>
      <td align="left" valign="top">
       <p>
        Is it possible for a trigger to update tables on a remote
        server?
       </p>
      </td>
     </tr>
     <tr class="answer">
      <td align="left" valign="top">
      </td>
      <td align="left" valign="top">
       <p>
        Yes. A table on a remote server could be updated using the
        <code class="literal">
         FEDERATED
        </code>
        storage engine. (See
        <a class="xref" href="federated-storage-engine.html" title="18.8 The FEDERATED Storage Engine">
         Section 18.8, “The FEDERATED Storage Engine”
        </a>
        ).
       </p>
      </td>
     </tr>
     <tr class="question">
      <td align="left" valign="top">
       <a name="faq-mysql-can-triggers-replication">
       </a>
       <a name="idm46045056204240">
       </a>
       <p>
        <b>
         A.5.12.
        </b>
       </p>
      </td>
      <td align="left" valign="top">
       <p>
        Do triggers work with replication?
       </p>
      </td>
     </tr>
     <tr class="answer">
      <td align="left" valign="top">
      </td>
      <td align="left" valign="top">
       <p>
        Yes. However, the way in which they work depends whether you are
        using MySQL's
        <span class="quote">
         “
         <span class="quote">
          classic
         </span>
         ”
        </span>
        statement-based or
        row-based replication format.
       </p>
       <p>
        When using statement-based replication, triggers on the replica
        are executed by statements that are executed on the source (and
        replicated to the replica).
       </p>
       <p>
        When using row-based replication, triggers are not executed on
        the replica due to statements that were run on the source and
        then replicated to the replica. Instead, when using row-based
        replication, the changes caused by executing the trigger on the
        source are applied on the replica.
       </p>
       <p>
        For more information, see
        <a class="xref" href="replication-features-triggers.html" title="19.5.1.36 Replication and Triggers">
         Section 19.5.1.36, “Replication and Triggers”
        </a>
        .
       </p>
      </td>
     </tr>
     <tr class="question">
      <td align="left" valign="top">
       <a name="faq-mysql-how-triggers-source-replica">
       </a>
       <a name="idm46045056199168">
       </a>
       <p>
        <b>
         A.5.13.
        </b>
       </p>
      </td>
      <td align="left" valign="top">
       <p>
        How are actions carried out through triggers on a source
        replicated to a replica?
       </p>
      </td>
     </tr>
     <tr class="answer">
      <td align="left" valign="top">
      </td>
      <td align="left" valign="top">
       <p>
        Again, this depends on whether you are using statement-based or
        row-based replication.
       </p>
       <p>
        <b>
         Statement-based replication.
        </b>
        First, the triggers that exist on a source must be re-created
          on the replica server. Once this is done, the replication flow
          works as any other standard DML statement that participates in
          replication. For example, consider a table
        <code class="literal">
         EMP
        </code>
        that has an
        <code class="literal">
         AFTER
        </code>
        insert trigger, which exists on a replication source server.
          The same
        <code class="literal">
         EMP
        </code>
        table and
        <code class="literal">
         AFTER
        </code>
        insert trigger exist on the replica
          server as well. The replication flow would be:
       </p>
       <div class="orderedlist">
        <ol class="orderedlist" type="1">
         <li class="listitem">
          <p>
           An
           <a class="link" href="insert.html" title="15.2.7 INSERT Statement">
            <code class="literal">
             INSERT
            </code>
           </a>
           statement is made
            to
           <code class="literal">
            EMP
           </code>
           .
          </p>
         </li>
         <li class="listitem">
          <p>
           The
           <code class="literal">
            AFTER
           </code>
           trigger on
           <code class="literal">
            EMP
           </code>
           activates.
          </p>
         </li>
         <li class="listitem">
          <p>
           The
           <a class="link" href="insert.html" title="15.2.7 INSERT Statement">
            <code class="literal">
             INSERT
            </code>
           </a>
           statement is
            written to the binary log.
          </p>
         </li>
         <li class="listitem">
          <p>
           The replica picks up the
           <a class="link" href="insert.html" title="15.2.7 INSERT Statement">
            <code class="literal">
             INSERT
            </code>
           </a>
           statement to
           <code class="literal">
            EMP
           </code>
           and executes it.
          </p>
         </li>
         <li class="listitem">
          <p>
           The
           <code class="literal">
            AFTER
           </code>
           trigger on
           <code class="literal">
            EMP
           </code>
           that exists on the replica activates.
          </p>
         </li>
        </ol>
       </div>
       <p>
        <b>
         Row-based replication.
        </b>
        When you use row-based replication, the changes caused by
          executing the trigger on the source are applied on the
          replica. However, the triggers themselves are not actually
          executed on the replica under row-based replication. This is
          because, if both the source and the replica applied the
          changes from the source and, in addition, the trigger causing
          these changes were applied on the replica, the changes would
          in effect be applied twice on the replica, leading to
          different data on the source and the replica.
       </p>
       <p>
        In most cases, the outcome is the same for both row-based and
        statement-based replication. However, if you use different
        triggers on the source and replica, you cannot use row-based
        replication. (This is because the row-based format replicates
        the changes made by triggers executing on the source to the
        replicas, rather than the statements that caused the triggers to
        execute, and the corresponding triggers on the replica are not
        executed.) Instead, any statements causing such triggers to be
        executed must be replicated using statement-based replication.
       </p>
       <p>
        For more information, see
        <a class="xref" href="replication-features-triggers.html" title="19.5.1.36 Replication and Triggers">
         Section 19.5.1.36, “Replication and Triggers”
        </a>
        .
       </p>
      </td>
     </tr>
    </tbody>
   </table>
  </div>
 </div>
 <br/>
</div>
