MySQL Trigger trigger usage one (simple)

Source: Internet
Author: User

Example ~ ~
example1:

Create a table Tab1

1234 DROPTABLE IF EXISTS tab1;CREATE TABLEtab1(    tab1_id varchar(11));

Create a table TaB2

1234 DROPTABLE IF EXISTS tab2;CREATE TABLEtab2(    tab2_id varchar(11));


Create trigger:t_afterinsert_on_tab1

Function: Automatically adds records to the TAB2 table after adding TAB1 table records

1234567 DROPTRIGGER IF EXISTS t_afterinsert_on_tab1;CREATE TRIGGER t_afterinsert_on_tab1 AFTER INSERT ON tab1FOR EACH ROWBEGIN     insert intotab2(tab2_id) values(new.tab1_id);END;

Test it.

1 INSERTINTOtab1(tab1_id) values(‘0001‘);

Look at the results

12 SELECT* FROM tab1;SELECT * FROMtab2;
example2:

Create trigger:t_afterdelete_on_tab1

Function: Delete the TAB1 table records automatically deleted the corresponding records in the TAB2 table

1234567 DROPTRIGGER IF EXISTS t_afterdelete_on_tab1;CREATE TRIGGER t_afterdelete_on_tab1AFTER DELETE ON tab1FOR EACH ROWBEGIN      delete from tab2 wheretab2_id=old.tab1_id;END;

Test it.

1 DELETEFROM tab1 WHEREtab1_id=‘0001‘;

MySQL Trigger trigger usage one (simple)

Related Article

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.