日期:2014-05-16  浏览次数:20436 次

触发器的使用

先创建两个测试表

create table if not exists t1
(
?? ID???????????????????????????? integer(4) AUTO_INCREMENT,????????????????????
?? v varchar(20),
?? primary key (ID)
);
create table if not exists t2
(
?? ID???????????????????????????? integer(4)? AUTO_INCREMENT,???????????????????
?? v varchar(20),
?? primary key (ID)
);

?

--db2 触发器 并待条件执行 (命令执行,需保证在一行执行)
CREATE TRIGGER t_on_t1 AFTER? INSERT? ON t1 REFERENCING? NEW AS new? FOR EACH ROW MODE DB2SQL WHEN (new.v='bb')? BEGIN ATOMIC insert into t2 values (new.id,new.v); END

?

--mysql 触发器(格式一定要注意)

?

DELIMITER $$
DROP TRIGGER IF EXISTS `t_on_t1`$$
CREATE
??? TRIGGER `t_on_t1` AFTER? INSERT ON? `t1`
??? FOR EACH ROW BEGIN
INSERT INTO T2 VALUES (NEW.ID,NEW.V);
??? END$$
DELIMITER ;

?