Mysql trigger, mysql trigger instance

Source: Internet
Author: User

Mysql trigger, mysql trigger instance

Trigger 1. Definition: a specific operation is triggered by insert, update, delete, and other events. The trigger can trigger multiple execution statements. Note: triggers cannot contain keywords such as start transaction, commit, or rollback, nor call statements. 1. Format: create trigger name berfor | after trigger event on table name for each row execution statement 2. format: mysql> DELIMITER & mysql> create trigger name berfor | after trigger event on table name for each row begin execution statement; end & mysql> DELIMITER; note: for each row indicates that any operation on a record meets the trigger event, this trigger will be triggered. by default, MYSQL uses ";" as the end statement. You need to use a semicolon to create a trigger. To solve this problem, you can use the delimiter statement, for example, "delimiter &". You can change the end symbol to "&". after the trigger is created, run the "delimiter;" command to change the end symbol to a semicolon. Only one trigger can be created for a table with the same trigger time. Ii. query trigger 1. Statement: show triggers \ G. This statement queries all triggers in the specified database. 2. query the triggers table in the information_schema database: select * from information_schema.triggers where trigger_name = 'trigger name' \ G 3. Delete trigger drop trigger name; 4. trigger application 1. For insert, the new row uses new to represent the value of each column in the row. create trigger tg2after insert on ofor each rowbeginupdate g set num = num-new.much where id = new. gid; end $2. For delete, the row to be deleted is represented by old. 3. For update, the old value of the row to be updated is represented by old, use new to create trigger tg2after insert on ofor each rowbeginupdate g set num = num + old. much-new.much where id = old. gid; end $

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.