实例~~
example1:
创建表tab1
1
2
3
4
|
DROP TABLE IF EXISTS tab1; CREATE TABLE tab1( tab1_id varchar (11) ); |
创建表tab2
1
2
3
4
|
DROP TABLE IF EXISTS tab2; CREATE TABLE tab2( tab2_id varchar (11) ); |
创建触发器:t_afterinsert_on_tab1
作用:增加tab1表记录后自动将记录增加到tab2表中
1
2
3
4
5
6
7
|
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) values (new.tab1_id); END ; |
测试一下
1
|
INSERT INTO tab1(tab1_id) values ( '0001' ); |
看看结果
1
2
|
SELECT * FROM tab1; SELECT * FROM tab2; |
example2:
创建触发器:t_afterdelete_on_tab1
作用:删除tab1表记录后自动将tab2表中对应的记录删去
1
2
3
4
5
6
7
|
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 ; |
测试一下
1
|
DELETE FROM tab1 WHERE tab1_id= '0001' ; |