All pages
Powered by GitBook
1 of 1

Loading...

Trigger Overview

A comprehensive guide to triggers, explaining their execution timing (BEFORE/AFTER), supported events, and how they interact with storage engines.

A trigger is a set of statements that run when, or are triggered by, an event that occurs on a table.

Events

The event can be an INSERT, an UPDATE, or a DELETE. The trigger can be executed BEFORE or AFTER the event. A table can have multiple triggers defined for the same event and timing combination.

The and statements invoke INSERT triggers for each row that is being inserted.

The statement is executed with the following workflow:

  • BEFORE INSERT;

  • BEFORE DELETE (only if a row is being deleted);

  • AFTER DELETE (only if a row is being deleted);

The statement, when a row already exists, follows the following workflow:

  • BEFORE INSERT;

  • BEFORE UPDATE;

  • AFTER UPDATE.

Otherwise, it works like a normal INSERT statement.

Note that does not activate any triggers.

Triggers and Errors

With non-transactional storage engines, if a BEFORE statement produces an error, the statement isn't executed. Statements that affect multiple rows fail before inserting the current row.

With transactional engines, triggers are executed in the same transaction as the statement that invoked them.

If a warning is issued with the SIGNAL or RESIGNAL statement (that is, an error with an SQLSTATE starting with '01'), it is treated like an error.

Creating a Trigger

Here's a simple example to demonstrate a trigger in action. Using these two tables as an example:

We want to increment a counter each time a new animal is added. Here's what the trigger looks like:

The trigger has:

  • a name (in this case, increment_animal),

  • a trigger time (in this case, after the specified trigger event),

  • a trigger event (an INSERT),

  • a table with which it is associated (animals

AFTER INSERT specifies that the trigger runs after an INSERT. The trigger could also be set to run before, and the statement causing the trigger could be a DELETE or an UPDATE as well. You can also have multiple triggers for an action. In this case, you can use FOLLOWS | PRECEDES`` other_trigger_name to specify the order of the triggers.

The set of statements to run are the statements on the table of the trigger; therefore, columns/values that change are always just a column name or an expression like NEW.column_name. Table references of other tables must come from explicit table references.

Now, if we insert a record into the animals table, the trigger runs, incrementing the animal_count table.

For more details on the syntax, see .

Dropping Triggers

To drop a trigger, use the statement. Triggers are also dropped if the table with which they are associated is also dropped.

Triggers Metadata

The stores information about triggers.

The statement returns similar information.

The statement returns a CREATE TRIGGER statement that creates the given trigger.

More Complex Triggers

Triggers can consist of multiple statements enclosed by a . If you're entering multiple statements on the command line, temporarily set a new delimiter so that you can use a semicolon to delimit the statements inside your trigger. See for more.

Trigger Errors

If a trigger contains an error and the engine is transactional, or it is a BEFORE trigger, the trigger will not run, and will prevent the original statement from running as well. If the engine is non-transactional, and it is an AFTER trigger, the trigger will not run, but the original statement will.

Here, we'll drop the above examples and then recreate the trigger with an error, a field that doesn't exist, first using the default , a transactional engine, and then again using , a non-transactional engine.

And now the identical procedure, but with a MyISAM table.

The following example shows how to use a trigger to validate data. The statement is used to intentionally produce an error if the email field is not a valid email. As the example shows, in that case, the new row is not inserted (because it is a BEFORE trigger).

See Also

This page is licensed: CC BY-SA / Gnu FDL

AFTER INSERT.
),
  • a set of statements to run (here, just the one UPDATE statement).

  • SHOW CREATE TRIGGER
  • Trigger Limitations

  • Creative uses of triggers: Things you people wouldn't believe (video)

  • LOAD DATA INFILE
    LOAD XML
    REPLACE
    INSERT ... ON DUPLICATE KEY UPDATE
    TRUNCATE TABLE
    CREATE TRIGGER
    DROP TRIGGER
    Information Schema TRIGGERS Table
    SHOW TRIGGERS
    SHOW CREATE TRIGGER
    BEGIN and END
    Delimiters in the mariadb client
    InnoDB
    MyISAM
    SIGNAL
    CREATE TRIGGER
    DROP TRIGGER
    Information Schema TRIGGERS Table
    SHOW TRIGGERS
    CREATE TABLE animals (id MEDIUMINT(9) 
    NOT NULL AUTO_INCREMENT, 
    name CHAR(30) NOT NULL, 
    PRIMARY KEY (`id`));
    
    CREATE TABLE animal_count (animals INT);
    
    INSERT INTO animal_count (animals) VALUES(0);
    CREATE TRIGGER increment_animal 
    AFTER INSERT ON animals 
    FOR EACH ROW 
    UPDATE animal_count SET animal_count.animals = animal_count.animals+1;
    SELECT * FROM animal_count;
    +---------+
    | animals |
    +---------+
    |       0 |
    +---------+
    
    INSERT INTO animals (name) VALUES('aardvark');
    INSERT INTO animals (name) VALUES('baboon');
    
    SELECT * FROM animal_count;
    +---------+
    | animals |
    +---------+
    |       2 |
    +---------+
    DROP TRIGGER increment_animal;
    DROP TABLE animals;
    
    UPDATE animal_count SET animals=0;
    
    CREATE TABLE animals (id MEDIUMINT(9) NOT NULL AUTO_INCREMENT, 
    name CHAR(30) NOT NULL, 
    PRIMARY KEY (`id`)) 
    ENGINE=InnoDB;
    
    DELIMITER //
    CREATE TRIGGER the_mooses_are_loose
    AFTER INSERT ON animals
    FOR EACH ROW
    BEGIN
     IF NEW.name = 'Moose' THEN
      UPDATE animal_count SET animal_count.animals = animal_count.animals+100;
     ELSE 
      UPDATE animal_count SET animal_count.animals = animal_count.animals+1;
     END IF;
    END; //
    
    DELIMITER ;
    
    INSERT INTO animals (name) VALUES('Aardvark');
    
    SELECT * FROM animal_count;
    +---------+
    | animals |
    +---------+
    |       1 |
    +---------+
    
    INSERT INTO animals (name) VALUES('Moose');
    
    SELECT * FROM animal_count;
    +---------+
    | animals |
    +---------+
    |     101 |
    +---------+
    DROP TABLE animals;
    
    CREATE TABLE animals (id MEDIUMINT(9) NOT NULL AUTO_INCREMENT, 
    name CHAR(30) NOT NULL, 
    PRIMARY KEY (`id`)) 
    ENGINE=InnoDB;
    
    CREATE TRIGGER increment_animal 
    AFTER INSERT ON animals 
    FOR EACH ROW 
    UPDATE animal_count SET animal_count.id = animal_count_id+1;
    
    INSERT INTO animals (name) VALUES('aardvark');
    ERROR 1054 (42S22): Unknown column 'animal_count.id' in 'field list'
    
    SELECT * FROM animals;
    Empty set (0.00 sec)
    DROP TABLE animals;
    
    CREATE TABLE animals (id MEDIUMINT(9) NOT NULL AUTO_INCREMENT, 
    name CHAR(30) NOT NULL, 
    PRIMARY KEY (`id`)) 
    ENGINE=MyISAM;
    
    CREATE TRIGGER increment_animal 
    AFTER INSERT ON animals 
    FOR EACH ROW 
    UPDATE animal_count SET animal_count.id = animal_count_id+1;
    
    INSERT INTO animals (name) VALUES('aardvark');
    ERROR 1054 (42S22): Unknown column 'animal_count.id' in 'field list'
    
    SELECT * FROM animals;
    +----+----------+
    | id | name     |
    +----+----------+
    |  1 | aardvark |
    +----+----------+
    CREATE TABLE user (
    	id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    	first_name CHAR(20),
    	last_name CHAR(20),
    	email CHAR(100)
    )
    	ENGINE = MyISAM;
    
    DELIMITER //
    CREATE TRIGGER bi_user
      BEFORE INSERT ON user
      FOR EACH ROW
    BEGIN
      IF NEW.email NOT LIKE '_%@_%.__%' THEN
        SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Email field IS NOT valid';
      END IF;
    END; //
    DELIMITER ;
    
    INSERT INTO user (first_name, last_name, email) VALUES ('John', 'Doe', 'john_doe.example.net');
    ERROR 1644 (45000): Email field is not valid
    
    SELECT * FROM user;
    Empty set (0.00 sec)