三个after 触发器
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)
);
-- 添加after insert触发器
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;
-- 添加after update触发器
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;
-- 添加after delete触发器
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;