MySQL_ trigger Trigger _ notes

A trigger is a special stored procedure , its implementation is not called by the program, not manually start, but is triggered by an event, such as when a table operation (insert, delete, update) will be activated to perform it. Triggers are often used to enhance data integrity constraints and business rules and so on.

Put another method of personal understanding is that when a table do insert, update, or delete action, it will set a good trigger to fire through the linkage so another table, the appropriate action automatically.

 

View Trigger

SHOW TRIGGERS \ G                                                                query information of all the triggers can not query the specified trigger

SELECT * FROM information_schema.triggers \ G                query information tiggers in will display detailed information about all triggers

SELECT * FROM information_schema.triggers WHERE TRIGGER_NAME='t1_triggers' \G 

Only read detailed information t1_triggers trigger.

 

Delete Trigger

. DROP TRIGGER trigger name | name database trigger name;

 

The syntax to create a trigger

DELIMITER $

create trigger Trigger_name <before | after | instead of> <insert | update | delete>

on table_name

for each row

BEGIN

- trigger code

END $

DELIMITER ;

 

DELIMITER is the mysql modify the command terminator, by default, MySQL command a semicolon (;) ends, it will start terminator change ($), END create a trigger before the end of this operation, and finally the terminator to (;). It may be abbreviated as (\ d).

Trigger_name is the name of the trigger, the trigger name must be unique in the current database.

<before | after | instead of> trigger time, before and after the trigger is used to represent before or after the statement that activated it is triggered, usually after, if you want to verify that the data meet the new limit, use before. instead of triggers used in the update of the view.

table_name is the name of the associated table

[FOR EACH ROW] is optional, if you specify the FOR EACH ROW, it indicates that the trigger is a row-level triggers, DML statement processing will be executed for each record trigger;

 

Trigger code NEW and OLD use:

 

 

For the INSERT statement, only NEW is legal;

For DELETE statements, only OLD is legal;

For UPDATE statements, NEW, OLD can be used simultaneously;

Delete, and update the conditions of the latter must be a primary key.

 

 

Do a simple test:

Create two tables

create table t1(

id int primary key AUTO_INCREMENT not null,

name varchar(20),

sex enum('m','f'),

salary double(10,2)

);

 

create table t2(

total int,

t_salary double(15,2)

);

 

Now these two tables are not related, to create a trigger

\d $

create trigger t1_insert

after insert on t1

for each row

BEGIN

update t2 set total=total+1,t_salary=t_salary+new.salary;

END$

\d ;

 

insert into t2(total,t_salary) values(0,0);

To t2 initial value table for a count, then insert a row into table t1, t2 can be seen that the table automatically trigger operation

insert into t1(name,sex,salary) values('aoteman','m',10000);

 

insert into t1(name,sex,salary) values('shejing','f',20000),('huluwa','m',30000);

Try again

 

Like a long time, update, and delete do not know how to write the triggers, try to calculate the statistical functions in Table 2, but can no longer update after creating a trigger Table 1, suggesting set of functions is invalid

 

Continue to learn to go ..

 

Published 60 original articles · won praise 9 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_44697035/article/details/104365351