Create a MySQL trigger

Source: Internet
Author: User

Create trigger trigger_name trigger_time trigger_event on tbl_name <br/> for each row begin <br/> [statement] <br/> end <br/> 

Trigger_name: trigger name

Trigger_time: departure time [after: after execution, before: before execution]

Trigger_event: Start event [insert, update, delete]

Tbl_name: for a table

Statement: Expression

 

For example:

When a new record is added to a table, enter its orderid number in the other table.

Create trigger trigger_name after insert on table_name <br/> begin <br/> insert into other_table_name set keyname = new. orderid, keyValue = new. orderid; <br/> end 

New: refers to the order record after insertion. You can reference the inserted data by using the new. method.

 

 

For example:

When updating a field in a table, update or update another table based on whether the conditions are met.

 

Create trigger trigger_name after update on table_name <br/> begin <br/> declare counts int (1); <br/> if old. ordstate = 0 & New. ordstate = 1 then <br/> select count (*) into counts from other_table_name where keyname = old. orderid; <br/> If counts = 0 then <br/> insert into listen_ticket set keyname = new. orderid, keyValue = new. orderid; <br/> end if; <br/> end 

Declare: defines Variables

Old: unupdated data information

Into variable: Assign the query return value to a variable.

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.