MySQL Basics: Triggers

Source: Internet
Author: User

Mysql triggers

1. Create a Trigger

  1) Create a trigger that has only one execution statement:

CREATE  TRIGGER   Trigger name   | after   trigger event                   on    table name for each     ROW   Execution statement

The for every row indicates that the action on any one record satisfies the triggering event, triggers the trigger, and executes the final execution statement.

  2) Create a trigger with multiple execution statements

CREATE  TRIGGER   Trigger name   | after   trigger event                   on    table name for each     ROW                      BEGIN                         execution Statement list                   END

2. View triggers

SHOW  TRIGGERS; SELECT *  from  Information_schema.triggers;

3. Using triggers

  1) Insert Trigger 

Within the Insert trigger code, you can refer to a virtual table called NEW to access the inserted row;

In the Before insert trigger, the value in NEW can also be updated (allowing changes to the inserted value);

For the Auto_increment column, new contains 0 before the insert is executed, and the newly generated auto-generation value is included after the insert executes.

  2) Update trigger

In the update trigger code, you can refer to a virtual table named old to access the value of the previous (before the UPDATE statement), referencing a virtual table named new to access the new updated value;

In the Before update trigger, the value in NEW may also be updated (allowing changes to be used for the value in the UPDATE statement);

The values in old are all read-only and cannot be updated.

  3) Delete Trigger

Within the Delete trigger code, you can refer to a virtual table named old to access the deleted row;

The values in old are all read-only and cannot be updated.

4. Delete a trigger

DROP  TRIGGER   Trigger name;

MySQL Basics: triggers

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.