> 数据库 > MySQL >

mysql触发器实例 插入数据时触发

分享一个mysql触发器的例子,代码如下:

create table test(
  percent decimal

);

delimiter $$

CREATE TRIGGER test_before_insert
BEFORE INSERT ON test FOR EACH ROW
BEGIN
    IF NEW.percent < 0.0 OR NEW.percent > 1.0 THEN
        SET NEW.percent = NULL;
    END IF;
END$$

delimiter ;

drop trigger test_before_insert;
drop table test;
(责任编辑:IT)