MySQL trigger and mysql trigger
I have read a lot of blog materials on the Internet and I feel that everyone is copying each other. I will summarize it based on my understanding and experience;
First, create a trigger:
A friend who uses Navicat can create a trigger on it:
(I simply copy the preceding SQL statement)
Create trigger 'C _ time' before insert on 'student'
For each row;
C_time is the name of a trigger.
Before and after are the trigger times.
Trigger event triggered by insert updata delete
On Connection table name
For each row indicates each field.
The next step is the focus. How can we use it when we create it;
DELIMITER $
Create trigger t_time after insert on student for each row
Begin
Declare c int; set c = (select stu t from class where id = new. id );
Update class set stuCount = c + 1 where id = new. id;
End
$ DELIMITER;
The trigger can be regarded as a function. Use DELIMITER $ DELIMITER to pack the expression in the begin end, and declare to write the variable type, you can use this begin end expression to make full use of your imagination!
I believe many people have seen the role of the trigger.
Purpose:
1. Modify the data permission restriction. I can write the restriction id in begin end to modify my data.
2. Two or more associated tables when data in one table is cleared, the associated fields of other associated tables can be updated to null.
3. You can track data changes and so on.
Of course, this is more important than that. There are still many other functions that will not be written here.