11. MySQL Triggers

  MySQL triggers, like stored procedures, are a program embedded in MySQL.Triggers are actions triggered by time, including INSERT, UODATE, and DELETE statements.If triggers are defined, they trigger actions when the database executes these statements. A trigger is a named database object related to a table that is activated when a specific event occurs on the table.


11.1. Create triggers

 A trigger is a special stored procedure, except that the execution stored procedure is called with a CALL statement, and the execution of the trigger does not need to be called with a CALL statement or started manually, as long as a predefined time occurs, it will be called automatically by MySQL.

 The syntax for creating a trigger is:

CREATE TRIGGER trigger_name trigger_time trigger_event
    ON tbl_name FOR EACH ROW trigger_body

trigger_name: The trigger name, specified by the user

trigger_time: The trigger time, which can be specified as before or after 

trigger_event: Identifies the trigger event

tbl_name: Table name identifying the trigger

trigger_body: Trigger execution statement

Create a trigger for single-execution statements:

mysql> CREATE TABLE account (acc_num INT,amount DECIMAL(10,2));
Query OK, 0 rows affected (0.06 sec)

mysql> CREATE TRIGGER ins_sum BEFORE INSERT ON account
    -> FOR EACH ROW SET @sum = @sum +NEW.amount;
Query OK, 0 rows affected (0.02 sec)

mysql> SET @sum=0;
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO account VALUES (1,1.00),(2,2.00);
Query OK, 2 rows affected (0.02 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> SELECT @sum;
+------+
| @sum |
+------+
| 3.00 |
+------+
1 row in set (0.00 sec)


Syntax for creating triggers to execute multiple statements:

CREATE TRIGGER trigger_name trigger_time trigger_event
	ON tbl_name FOR EACH ROW 
	BEGIN
	  trigger_stmt
	END


11.2. View triggers

 Viewing triggers refers to viewing the definition, status, and trigger information of a trigger that already exists in the database through SHOW TRUGGERS and in the triggers table.

mysql> CREATE TABLE myevent
    -> (
    -> id int(11) DEFAULT NULL,
    -> evt_name char(20) DEFAULT NULL
    -> );
Query OK, 0 rows affected (0.03 sec)

mysql> CREATE TRIGGER trig_update AFTER UPDATE ON account
    -> FOR EACH ROW INSERT INTO myevent VALUES (1,'AFTER UPDATE');
Query OK, 0 rows affected (0.05 sec)

mysql> SHOW TRIGGERS \G
*************************** 1. row ***************************
             Trigger: ins_sum
               Event: INSERT
               Table: account
           Statement: SET @sum = @sum +NEW.amount
              Timing: BEFORE
             Created: NULL
            sql_mode: 
             Definer: root@localhost
character_set_client: utf8
collation_connection: utf8_general_ci
  Database Collation: utf8_general_ci
*************************** 2. row ***************************
             Trigger: trig_update
               Event: UPDATE
               Table: account
           Statement: INSERT INTO myevent VALUES (1,'AFTER UPDATE')
              Timing: AFTER
             Created: NULL
            sql_mode: 
             Definer: root@localhost
character_set_client: utf8
collation_connection: utf8_general_ci
  Database Collation: utf8_general_ci
2 rows in set (0.00 sec)


  The TRIGGERS table in the INFORMATION_SCHEMA database exists for all trigger definitions in MySQL and can be viewed by SELECT in the syntax format:

SELECT * FROM INFORMATION_SCHEMA.TRIGGERS WHERE condition;
mysql> SELECT * FROM INFORMATION_SCHEMA.TRIGGERS WHERE TRIGGER_NAME='trig_update' \G
*************************** 1. row ***************************
           TRIGGER_CATALOG: def
            TRIGGER_SCHEMA: test
              TRIGGER_NAME: trig_update
        EVENT_MANIPULATION: UPDATE
      EVENT_OBJECT_CATALOG: def
       EVENT_OBJECT_SCHEMA: test
        EVENT_OBJECT_TABLE: account
              ACTION_ORDER: 0
          ACTION_CONDITION: NULL
          ACTION_STATEMENT: INSERT INTO myevent VALUES (1,'AFTER UPDATE')
        ACTION_ORIENTATION: ROW
             ACTION_TIMING: AFTER
ACTION_REFERENCE_OLD_TABLE: NULL
ACTION_REFERENCE_NEW_TABLE: NULL
  ACTION_REFERENCE_OLD_ROW: OLD
  ACTION_REFERENCE_NEW_ROW: NEW
                   CREATED: NULL
                  SQL_MODE: 
                   DEFINER: root@localhost
      CHARACTER_SET_CLIENT: utf8
      COLLATION_CONNECTION: utf8_general_ci
        DATABASE_COLLATION: utf8_general_ci
1 row in set (0.00 sec)


11.3. Use of triggers

  A trigger is a named database object associated with a table that is activated when a specific time occurs on the table.

Create a trigger to update the myevent table after the account table inserts data.

mysql> CREATE TRIGGER trig_inster AFTER INSERT ON account 
    -> FOR EACH ROW INSERT INTO myevent VALUES (2,'AFTER INSERT'); 
Query OK, 0 rows affected (0.02 sec)

mysql> INSERT INTO account VALUES (1,1.00),(2,2.00);
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> SELECT * FROM myevent;
+------+--------------+
| id   | evt_name     |
+------+--------------+
|    2 | AFTER INSERT |
|    2 | AFTER INSERT |
+------+--------------+
2 rows in set (0.00 sec)


11.4, Delete trigger

  Use the DROP TRIGGER statement to delete triggers that already exist in MySQL in the syntax:

DROP TRIGGER [schema_name.]trigger_name

Delete a trigger

mysql> DROP TRIGGER test.ins_sum;
Query OK, 0 rows affected (0.02 sec)


Keywords: MySQL Database Stored Procedure

Added by simjay on Fri, 07 Jun 2019 20:45:46 +0300