Triggers in Mysql

1. Database trigger basics

Three events, namely insert insert, delete delete and modify update

Two timings, before and after execution

 

2. Statement to create trigger

create trigger name event on table name

    for  each  row

    executable sql statement

Note: ①Trigger cannot have the same name ②Only one type of trigger can be set for a type of event on a table

 

3 Manage triggers

delete drop trigger trigger name

View show create trigger trigger name

 

4. To obtain program data within the trigger, use old and new

old: The data of the table where the listening event is located before the data occurs

new: After the listening data occurs, the data that has been processed

The data is the record that triggered the event

 

5. If a trigger consists of multiple sql

① Group SQL statements into blocks (under the begin and end signs)

②The statement block requires a separate terminator (default semicolon)

 

6. Command line: Since the program in the trigger uses a semicolon as a statement terminator

When the command line client encounters a semicolon, it is interpreted as an internal substatement terminator rather than a trigger terminator

At this point, we can fix this problem by modifying the statement terminator

DROP TRIGGER updateBook;
DELIMITER $$
CREATE TRIGGER updateBook AFTER UPDATE ON person
FOR EACH ROW
BEGIN
UPDATE book SET price = price + 100 WHERE id='abc1';
UPDATE book SET price = price + 200 WHERE id='abc2';
END
$$
DELIMITER ;

 

Come on, come on, go home and cook

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326992539&siteId=291194637