MySQL Crash Course #17# Chapter 25. Triggers

It is recommended to read this  mysql use trigger (Trigger) to make the code simpler

以及 23.3.1 Trigger Syntax and Examples

 Feels a bit like AOP in Spring

Why do we need triggers? -- Because we want something to happen automatically when something happens. .

For example, when inserting data into a table, insert data into another table at the same time.

"Insert data to a table" is the event (the trigger), and "insert data to another table" is the thing we want to happen automatically (the thing that gets triggered).

But why not manually "insert data into a table first, and then insert data into another table"?

Personally, there are two reasons why we do this:

  1. Implementing a certain business logic is like reducing inventory when placing an order, right? But whether this work is done by the upper-layer application or the database is a problem.
  2. Separation of concerns. Triggering events can be trivial, business-independent security checks, etc. For this concept, refer to Spring AOP

The following COPY a few demos for backup.

The first is to look at existing triggers:

SELECT * FROM information_schema.`TRIGGERS`;

Delete an existing trigger:

DROP TRIGGER newproduct;

Create a simple trigger:

CREATE TRIGGER newproduct AFTER INSERT ON products
FOR EACH ROW SELECT 'Product added';

-- - Execute SELECT 'Product added' whenever inserting data into products, // print the original

- - -- FOR EACH ROW Do this for every record inserted

--- -- PS. Triggers can only be created on real tables, not on virtual tables (views)

-- - - - Supports up to 6 triggers per table AFTER BEFORE 2 * 3 UPDATE DELETE INSERT = 6 

- ---I tried it and it didn't work. . . Therefore, the above rules may be changed in the new version. .

mysql> CREATE TRIGGER newproduct AFTER INSERT ON products
    -> FOR EACH ROW SELECT 'Product added';
ERROR 1415 (0A000): Not allowed to return a result set from a trigger

 A few notes about triggers:

  1. Triggers cannot be overridden. To modify them, you can only delete the old one and create a new one.
  2. In the execution flow of BEFORE trigger → SQL statement → AFTER trigger, any error will not be executed any further.

About OLD and NEW keywords

Within the trigger body, the OLD and NEW keywords enable you to access columns in the rows affected by a trigger. OLDand NEW are MySQL extensions to triggers; they are not case-sensitive.

In an INSERT trigger, only  can be used; there is no old row. In a  trigger, only  can be used; there is no new row. In an  trigger, you can use  to refer to the columns of a row before it is updated and  to refer to the columns of the row after it is updated.NEW.col_nameDELETEOLD.col_nameUPDATEOLD.col_nameNEW.col_name

A column named with OLD is read only. You can refer to it (if you have the SELECT privilege), but not modify it. You can refer to a column named with NEW if you have the SELECT privilege for it. In a BEFORE trigger, you can also change its value with  if you have the  privilege for it. This means you can use a trigger to modify the values to be inserted into a new row or used to update a row. (Such a  statement has no effect in an trigger because the row change will have already occurred.)SET NEW.col_name = valueUPDATESETAFTER

In a BEFORE trigger, the NEW value for an AUTO_INCREMENT column is 0, not the sequence number that is generated automatically when the new row actually is inserted.

trigger more statements

By using the BEGIN ... END construct, you can define a trigger that executes multiple statements.

mysql> delimiter //
mysql> CREATE TRIGGER upd_check BEFORE UPDATE ON account
       FOR EACH ROW
       BEGIN
           IF NEW.amount < 0 THEN
               SET NEW.amount = 0;
           ELSEIF NEW.amount > 100 THEN
               SET NEW.amount = 100;
           END IF;
       END;//
mysql> delimiter ;

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324744550&siteId=291194637