Create a trigger
Grammar:
Create TRIGGER Trigger name before|after trigger event on table name for each row execution statement
Note: There can only be one trigger event at the same trigger time, such as the before of insert time can only have one departure event
Create a trigger_time table first:
Mysql> CREATE TABLE Trigger_time (Insert_time datetime);
Create a trigger that has only one execution statement
To create a trigger:
Mysql> CREATE trigger DEPT_TRIG1 before insert on Department for each row insert to Trigger_time values (now ());
In this way, when a record is inserted into the department table, the current time is inserted into the Trigger_time table.
to create a trigger with more than one execution statement
Grammar:
Create TRIGGER Trigger name before|after departure event on indicates for each row
Begin
Execute statement List
End
Note: MySQL defaults to ";" As the end. In the process of creating a trigger, you need to use ";" to solve this problem, you can use "delimiter &&" to change the Terminator to && After the trigger is created, use "delimiter;" Changes the Terminator to ";".
Mysql> delimiter &&
Mysql> CREATE trigger DEPT_TRIG2 after delete on department for each row
Begin
INSERT into Trigger_time values (now ());
INSERT into Trigger_time values (now ());
-End
&&
view triggers
Grammar:
Show triggers;
Delete Trigger
Grammar:
Drop trigger trigger name;
Fifth Chapter Trigger