MySQL 触发器示例

MySQL 触发器insert 示例一


view sourceprint?01 delimiter //   
create trigger InsertUser   
before insert on user   
for each row   
Begin   
insert into user_group(uid,gid) values(new.uid,'group4444444444');   
end;//   
delimiter ;   


MySQL 触发器insert 示例二
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 ;   
MySQL 触发器insert 示例三:


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 ;   


DELIMITER ;//指定分隔符为";"
DELIMITER ;
DELIMITER |  //指定分隔符为"|",因为触发器语法中存在";"
create trigger trigger_wz_t8_insert_time
 before insert  on t8
  for each row begin
     set new.INSERT_TIME= now();
end 
|
DELIMITER ;
DELIMITER |
create trigger trigger_wz_t8_update_time
 before update  on t8
  for each row begin
     set new.update_time= now();
end 
|
DELIMITER ;




DELIMITER ;
drop PROCEDURE IF EXISTS `t8` ;
delimiter //
create  procedure `p_delete_t8` ()
begin
  DECLARE v_del_partition_name varchar(30);
  select CONCAT('t8',DATE_FORMAT(now(),'%d')) INTO v_del_partition_name;
  SET @str_exec = concat('alter table t8 truncate partition ', v_del_partition_name);
  prepare stmt FROM @str_exec;
  execute stmt;
end ;
 //
DELIMITER ;

你可能感兴趣的:(mysql,user,table,delete,insert,each)