mysql触发器:
1.语法:
create trigger trigger_name trigger_time trigger_event on table_name for each row trigger_stmt
trigger_time:是触发动作时间,可以是before或者after
trigger_event:是触发语句类型,可以是insert ,update,delete
trigger_stmt:是触发之后需要做什么,如果是多个语句需要用begin end
2.实例
示例一
delimiter //
create trigger InsertUser
before insert on user
for each row
Begin
insert into user_group(uid,gid) values(new.uid,'group4444444444');
end;//
delimiter ;
示例二
delimiter //
create trigger InsertUser
before insert on user
for each row
Begin
IF new.Type=2 then
insert into user_group(uid,gid) values(new.uid,'group4444444444');
else
insert into user_group(uid,gid) values(new.uid,'group55555555555')
END IF;
end;//
delimiter ;
示例三:
delimiter //
create trigger InsertUser
before insert on user
for each row
Begin
IF new.type=1 then
insert into user_group(uid,gid) values(new.uid,'578d3369633b47bd9c1fe8bf905cbfb1');
END IF;
IF new.type=2 then
insert into user_group(uid,gid) values(new.uid,'387bcd57fc5a4c3c9de83ee210fef661');
END IF;
end;//
delimiter ;