DELIMITER $$ CREATE OR REPLACE DEFINER=`root`@`localhost` TRIGGER `vn2008`.`agency_hourBeforeInsert` BEFORE INSERT ON `agency_hour` FOR EACH ROW BEGIN DECLARE vAlreadyExists BOOLEAN DEFAULT FALSE; IF NEW.week_day IS NULL AND NEW.province_id IS NULL AND NEW.subtract_day = 0 AND NEW.max_hour = 0 THEN CALL util.throw ('FILL_MORE_FIELDS'); END IF; SELECT COUNT(*) INTO vAlreadyExists FROM vn2008.agency_hour WHERE NEW.week_day <=> week_day AND NEW.province_id <=> province_id AND NEW.warehouse_id <=> warehouse_id AND NEW.max_hour <=> max_hour AND NEW.agency_id <=> agency_id; IF vAlreadyExists > 1 THEN CALL util.throw('ALREADY_EXISTS'); END IF; END$$ DELIMITER ;