MySQL Trigger Simple instance

Source: Internet
Author: User

Three after triggers

DROP TABLE IF EXISTS tab1;
CREATE TABLE Tab1 (
tab1_id varchar (11),
Tab1_name varchar (11),
Tab1_pass varchar (11)
);
DROP TABLE IF EXISTS tab2;
CREATE TABLE TAB2 (
tab2_id varchar (11),
Tab2_name varchar (11),
Tab2_pass varchar (11)
);
--Add after insert trigger
DROP TRIGGER IF EXISTS t_afterinsert_on_tab1;
CREATE TRIGGER T_AFTERINSERT_ON_TAB1
After INSERT on TAB1
For each ROW
BEGIN
Insert into TAB2 (Tab2_id,tab2_name,tab2_pass) values (New.tab1_id,new.tab1_name,new.tab1_pass);
END;
INSERT into TAB1 (tab1_id,tab1_name,tab1_pass) VALUES (' 0001 ', ' name1 ', ' pass1 ');
SELECT * from TAB1;
SELECT * from TAB2;

--Add after UPDATE trigger
DROP TRIGGER IF EXISTS t_afterupdate_on_tab1;
CREATE TRIGGER T_AFTERUPDATE_ON_TAB1
After update on TAB1
For each ROW
BEGIN
Update tab2 Set tab2_id = new.tab1_id, Tab2_name = new.tab1_name, Tab2_pass = New.tab1_pass;
END;
Update TAB1 Set tab1_id = ' 0002 ', tab1_name = ' name2 ', Tab1_pass = ' pass2 ';
SELECT * from TAB1;
SELECT * from TAB2;

--Add after Delete trigger
DROP TRIGGER IF EXISTS t_afterdelete_on_tab1;
CREATE TRIGGER T_AFTERDELETE_ON_TAB1
After delete on TAB1
For each ROW
BEGIN
Delete from tab2 where tab2_id = old.tab1_id;
END;
Delete from tab1 where tab1_id = ' 0002 ';
SELECT * from TAB1;
SELECT * from TAB2;

MySQL Trigger Simple instance

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.