Simple MySQL trigger instance

Source: Internet
Author: User
TRIGGER Syntax: CREATETRIGGER & lt; Trigger name & gt; -- the trigger must have a name up to 64 characters, which may be followed by a separator. it is similar to the naming of other objects in MySQL. {BEFORE | AFTER} -- Trigger has execution time setting: it can be set to BEFORE or AFTER an event .... "> <LINKhref =" http: // www. php100

 

Trigger syntax:

CREATE TRIGGER <触发器名称> -- The trigger must have a name and a maximum of 64 characters. it may be followed by a separator. it is similar to the naming method of other objects in MySQL.

{BEFORE | AFTER} -- Trigger has the execution time setting: it can be set to BEFORE or AFTER an event occurs.

{INSERT | UPDATE | DELETE} -- trigger events can also be set: they can be triggered during insert, update, or delete execution.

ON <表名称> -- A trigger belongs to a table. When an insert, update, or delete operation is performed on the table, the trigger is activated. we cannot schedule two triggers for the same event of the same table.

For each row -- trigger execution interval: the for each row clause notifies the trigger to execute an action every ROW, instead of executing an action FOR the entire table.

<触发器sql语句> -- The trigger contains the SQL statement to be triggered: the statement here can be any legal statement, including compound statements, but the statements here are subject to the same restrictions as the functions.

Tip: You must have considerable permissions to CREATE a TRIGGER. if you are a Root user, this is enough. This is different from the SQL standard.

Instance:

Example1:

Create table tab1:

1234 Drop table if exists tab1; create table tab1 (tab1_id varchar (11 ));

Create table tab2:

1234 Drop table if exists tab2; create table tab2 (tab2_id varchar (11 ));

Create trigger: t_afterinsert_on_tab1

Purpose: automatically add a record to the tab2 table after adding a record to the tab1 table

1234567 Drop trigger if exists t_afterinsert_on_tab1; create trigger t_afterinsert_on_tab1AFTER insert on tab1FOR each rowbegin insert into tab2 (tab2_id) values (new. tab1_id); END;

Test:

1 Insert into tab1 (tab1_id) values ('20140901 ');

Let's see the results. it is estimated that both tables have the same data!

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.