MySQL trigger structure and three cases demo

Source: Internet
Author: User

--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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.