Home > Database > Mysql Tutorial > mysql建立触发器_MySQL

mysql建立触发器_MySQL

WBOY
Release: 2016-06-01 13:43:50
Original
908 people have browsed it

bitsCN.com 创建触发器。创建触发器语法如下:
 
CREATE TRIGGER trigger_name trigger_time trigger_event
ON tbl_name FOR EACH ROW trigger_stmt
 
其中trigger_name标识触发器名称,用户自行指定;
 
trigger_time标识触发时机,用before和after替换;
 
trigger_event标识触发事件,用insert,update和delete替换;
 
tbl_name标识建立触发器的表名,即在哪张表上建立触发器;
 
trigger_stmt是触发器程序体;触发器程序可以使用begin和end作为开始和结束,中间包含多条语句;
 
 
现有sys_log_login表,sys_log_login_record表,需要在sys_log_login表插入一条记录的时候去查询sys_log_login_record表中是否有和sys_log_login表中fd_operator_id一致的记录,若无,在sys_log_login_record中插入一条新记录;
 
 
 
 
create Trigger loginRecord
after  insert
on  sys_log_login for each ROW
BEGIN
if not exists(select fd_operator_id from sys_log_login_record where fd_operator_id = NEW.fd_operator_id) then
 insert into sys_log_login_record values(NEW.fd_id,NEW.fd_operator_id,NEW.fd_create_time);
END IF;
end;
delimiter;
  bitsCN.com

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template