Use of triggers in MySQL

Source: Internet
Author: User

Concept

The trigger is a special kind of transaction, which listens for adding and deleting operations, and triggers additions and deletions. It is mainly used to deal with some complex business logic to ensure the linkage of data. It contains four elements: 1. Monitoring location (table) 2. Monitoring Events (Insert/update/delete) 3. Trigger Time (After/before) 4. Trigger Event (Insert/update/delete)

Grammar
CREATE TRIGGER ‘触发器名称‘ ‘触发时间(after|before)‘ ‘监听的事情(insert|update|delete) ‘ON ‘触发地点‘ FOR EACH ROW BEGIN  sql1;  sql2;  sql3;END$

The For each row is used to identify the type of trigger because MySQL supports row-level triggers and does not support statement triggers, so you can only use the for each row. Because the trigger uses '; ' Distinguish multiple event statements, so you need to redefine the delimiter.

Instance

Modify delimiter to $

Create a Trigger

View triggers

Before you perform the action
T4 table data is as follows

T3 table data is as follows

After performing the appropriate action

Use of triggers in MySQL

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.