Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>MySQL trigger syntax</strong></p> <p>In order to create a trigger you use CREATE TRIGGER statement. The following illustrates the syntax of the CREATE TRIGGER statement:</p> <pre><code>CREATE TRIGGER trigger_name trigger_time trigger_event ON table_name FOR EACH ROW BEGIN ... END </code></pre> <p>Let’s examine the syntax above in more detail.</p> <p>You put the trigger name after the CREATE TRIGGER statement. The trigger name should follow the naming convention [trigger time]<em>[table name]</em>[trigger event], for example before_employees_update.</p> <p>Trigger activation time can be BEFORE or AFTER. You must specify the activation time when you define a trigger. You use BEFORE keyword if you want to process action prior to the change is made on the table and AFTER if you need to process action after change are made.</p> <p>Trigger event can be INSERT, UPDATE and DELETE. These events cause triggers to be invoked. A trigger only can be invoked by one event. To define a trigger that is invoked by multiple events, you have to define multiple triggers, one for each event.</p> <p>A trigger must be associated with a specific table. Without a table trigger would not exist therefore you have to specify the table name after the ON keyword.</p> <p>The SQL code is placed between BEGIN and END keywords.</p> <p>The OLD and NEW keywords help you develop trigger more efficient. The OLD keyword refers to the existing record before you change the data and the NEW keyword refers to the new row after you change the data.</p> <p><strong>MySQL trigger example</strong></p> <p>Let’s start creating a trigger in MySQL to audit the changes of the employees table.</p> <p>First, we have employees table in our MySQL sample database as follows:</p> <p><img src="https://i.stack.imgur.com/fhdkf.png" alt="enter image description here"></p> <p>Second, we create a new table named employees_audit to keep the change of the employee records. The following script creates the employee_audit table.</p> <pre><code>CREATE TABLE employees_audit ( id int(11) NOT NULL AUTO_INCREMENT, employeeNumber int(11) NOT NULL, lastname varchar(50) NOT NULL, changedon datetime DEFAULT NULL, action varchar(50) DEFAULT NULL, PRIMARY KEY (id) ) </code></pre> <p>Third, we create the BEFORE UPDATE trigger to be invoked before a change is made to the employee records.</p> <pre><code>DELIMITER $$ CREATE TRIGGER before_employee_update BEFORE UPDATE ON employees FOR EACH ROW BEGIN INSERT INTO employees_audit SET action = 'update', employeeNumber = OLD.employeeNumber, lastname = OLD.lastname, changedon = NOW(); END$$ DELIMITER ; </code></pre> <p>If you take a look at the schema, you will see before_employee_update trigger under the employees table as follows:</p> <p><img src="https://i.stack.imgur.com/uyVII.png" alt="enter image description here"></p> <p>Now it’s time to update an employee record to test if the trigger is really invoked.</p> <pre><code>UPDATE employees SET lastName = 'Phan' WHERE employeeNumber = 1056 </code></pre> <p>To check if the trigger was invoked by the UPDATE statement, we can query the employees_audit table by using the following query:</p> <pre><code>SELECT * FROM employees_audit </code></pre> <p>The following is the output of the query:</p> <p><img src="https://i.stack.imgur.com/uESAG.png" alt="enter image description here"></p> <p>As you see, our trigger was really invoked so that we have a new record in the employees_audit table.</p> <p>In this tutorial, you have learned how to create a trigger in MySQL. You’ve written a trigger to audit the changes of the employees table.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload