1. Why should I use a trigger?
Triggers are somewhat similar to functions and need to be declared and executed. However, the execution of the trigger is not called by the program, nor is it started manually, but is triggered by an event, which is activated to achieve execution.
The activation trigger is triggered when the Delete,insert,update statement is triggered, and the other SQL statements do not activate the trigger.
You can use triggers to process database objects when you are working with some business logic, when the table's data changes and you can automate some processing.
The actions of triggers include creating triggers, viewing triggers, and deleting triggers.
2. Create a Trigger
2.1 Creating a trigger that has an execution statement
CREATE TRIGGER | on for Each ROW trigger_stmt;
Examples are as follows:
CREATE TRIGGER INSERT on for INSERT into VALUES (NULL,'t_dept', Now ());
When you insert any record into the department table t_dept, the current time record is inserted into the table t_diary before the insert operation.
2.2 Creating a trigger with multiple execution statements
CREATE TRIGGER | on for Each BEGINEND;
Examples are as follows:
DELIMITER $$CREATE TRIGGERTri_diary beforeINSERT onT_dept forEach ROWBEGIN INSERT intoT_diaryVALUES(NULL,'t_dept', now ());INSERT intoT_diaryVALUES(NULL,'t_dept', now ());END$ $DELIMITER;
In the example above, the "DELIMITER $$" statement is first set to "$$", and then the execution statement list is written between the keyword begin and end, and finally through the "DELIMITER;" The statement reverts the ending symbol to the default Terminator ";".
3. View triggers
3.1 Viewing triggers through the show triggers statement
Show triggers displays a list of all triggers.
3.2 View the trigger by viewing the system table triggers implementation.
4. Delete a trigger
DROP TRIGGER trigger_name;
MySQL's action on the trigger