<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h2 class="title">
      <a name="triggers">
      </a>
      27.3 Using Triggers
     </h2>
    </div>
   </div>
  </div>
  <div class="toc">
   <dl class="toc">
    <dt>
     <span class="section">
      <a href="trigger-syntax.html">
       27.3.1 Trigger Syntax and Examples
      </a>
     </span>
    </dt>
    <dt>
     <span class="section">
      <a href="trigger-metadata.html">
       27.3.2 Trigger Metadata
      </a>
     </span>
    </dt>
   </dl>
  </div>
  <a class="indexterm" name="idm46045082430048">
  </a>
  <p>
   A trigger is a named database object that is associated with a
    table, and that activates when a particular event occurs for the
    table. Some uses for triggers are to perform checks of values to be
    inserted into a table or to perform calculations on values involved
    in an update.
  </p>
  <p>
   A trigger is defined to activate when a statement inserts, updates,
    or deletes rows in the associated table. These row operations are
    trigger events. For example, rows can be inserted by
   <a class="link" href="insert.html" title="15.2.7 INSERT Statement">
    <code class="literal">
     INSERT
    </code>
   </a>
   or
   <a class="link" href="load-data.html" title="15.2.9 LOAD DATA Statement">
    <code class="literal">
     LOAD
    DATA
    </code>
   </a>
   statements, and an insert trigger activates for each
    inserted row. A trigger can be set to activate either before or
    after the trigger event. For example, you can have a trigger
    activate before each row that is inserted into a table or after each
    row that is updated.
  </p>
  <div class="important" style="margin-left: 0.5in; margin-right: 0.5in;">
   <div class="admon-title">
    Important
   </div>
   <p>
    MySQL triggers activate only for changes made to tables by SQL
      statements. This includes changes to base tables that underlie
      updatable views. Triggers do not activate for changes to tables
      made by APIs that do not transmit SQL statements to the MySQL
      Server. This means that triggers are not activated by updates made
      using the
    <a class="link" href="mysql-cluster.html" title="Chapter 25 MySQL NDB Cluster 8.4">
     <code class="literal">
      NDB
     </code>
    </a>
    API.
   </p>
   <p>
    Triggers are not activated by changes in
    <code class="literal">
     INFORMATION_SCHEMA
    </code>
    or
    <code class="literal">
     performance_schema
    </code>
    tables. Those tables are
      actually views and triggers are not permitted on views.
   </p>
  </div>
  <p>
   The following sections describe the syntax for creating and dropping
    triggers, show some examples of how to use them, and indicate how to
    obtain trigger metadata.
  </p>
  <h3>
   <a name="idm46045082420032">
   </a>
   Additional Resources
  </h3>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      You may find the
      <a class="ulink" href="https://forums.mysql.com/list.php?20" target="_blank">
       MySQL
        User Forums
      </a>
      helpful when working with triggers.
     </p>
    </li>
    <li class="listitem">
     <p>
      For answers to commonly asked questions regarding triggers in
        MySQL, see
      <a class="xref" href="faqs-triggers.html" title="A.5 MySQL 8.4 FAQ: Triggers">
       Section A.5, “MySQL 8.4 FAQ: Triggers”
      </a>
      .
     </p>
    </li>
    <li class="listitem">
     <p>
      There are some restrictions on the use of triggers; see
      <a class="xref" href="stored-program-restrictions.html" title="27.8 Restrictions on Stored Programs">
       Section 27.8, “Restrictions on Stored Programs”
      </a>
      .
     </p>
    </li>
    <li class="listitem">
     <p>
      Binary logging for triggers takes place as described in
      <a class="xref" href="stored-programs-logging.html" title="27.7 Stored Program Binary Logging">
       Section 27.7, “Stored Program Binary Logging”
      </a>
      .
     </p>
    </li>
   </ul>
  </div>
 </div>
 <br/>
</div>
