文章教程

MySQL如何创建触发器

5/9/2018 10:32:34 PM 人评论 次浏览

本文实例为大家分享了MySQL创建触发器的具体代码,供大家参考,具体内容如下

先来个实例:

#建表
DROP TABLE IF EXISTS t_attendance;
CREATE TABLE t_attendance (
 job_no VARCHAR(30) DEFAULT '',
 operateTime VARCHAR(20) DEFAULT '',
 INDEX index_operateTime(operateTime),
 INDEX index_jobNo(job_no)
) ENGINE=INNODB DEFAULT CHARSET=utf8


#建触发器
DELIMITER $
CREATE TRIGGER t_afterinsert_on_doorlog AFTER INSERT 
ON t_doorlog FOR EACH ROW 
BEGIN
 DECLARE jobno VARCHAR(30);
 DECLARE time1 VARCHAR(20);
 SET jobno = (SELECT job_num FROM tb_park_user_base WHERE card_num = new.cardNo);
 SET time1 = DATE_FORMAT(new.operateTime,'%Y%m%d%H%i%s');
 INSERT INTO t_attendance (job_no, operateTime) 
 VALUES
  (jobno,time1);
END$