--You must have considerable privileges to create a trigger (created TRIGGER), which is sufficient if you are already a root user. This is different from the SQL standard.
CREATE Trigger Syntax
CREATE TRIGGER trigger_name trigger_time trigger_event
on tbl_name for each ROW trigger_stmt
The trigger is related to a table named tbl_name .
Trigger_time is the action time of the triggering program. It can be before or after to indicate that the trigger was triggered before or after the statement that activated it.
trigger_event Indicates the type of statement that activates the trigger. trigger_event can be one of the following values:
· Insert: Activates the trigger when inserting a new row into a table
· UPDATE: Activating a trigger when a row is changed
· Delete: Activates the trigger when a row is deleted from the table
trigger_stmt is the statement that executes when the triggering program is activated. If you plan to execute multiple statements, you can use the BEGIN
... End Compound statement structure.
MySQL trigger is an important concept of MySQL database, the following is a description of three MySQL triggers for your reference, I hope you learn MySQL triggers can be helpful.
Example One delimiter// Create TriggerInsertuser beforeInsert on User forEach rowBegin Insert intoUser_group (Uid,gid)Values(New.uid,'group4444444444'); End;//delimiter; Example two delimiter// Create TriggerInsertuser beforeInsert on User forEach rowBegin IFNew. Type=2 Then Insert intoUser_group (Uid,gid)Values(New.uid,'group4444444444'); Else Insert intoUser_group (Uid,gid)Values(New.uid,'group55555555555') END IF; End;//delimiter; Example three: Delimiter// Create TriggerInsertuser beforeInsert on User forEach rowBegin IFNew.type=1 Then Insert intoUser_group (Uid,gid)Values(New.uid,'578D3369633B47BD9C1FE8BF905CBFB1'); END IF; IFNew.type=2 Then Insert intoUser_group (Uid,gid)Values(New.uid,'387bcd57fc5a4c3c9de83ee210fef661'); END IF; End;//delimiter;
MySQL trigger structure and three cases demo