LOCK TABLES `diapason` WRITE;
DROP TRIGGER IF EXISTS `dip`;
DELIMITER //
CREATE TRIGGER `dip` BEFORE INSERT ON `xxx`.`diapason`
FOR EACH ROW
BEGIN
DECLARE dst int;
if NEW.rmin <= 0 OR NEW.rmax <= 0 OR NEW.rmin > NEW.rmax then
signal sqlstate '45000'
SET message_text = "rmin need less than rmax";
end if;
SELECT rmin FROM `diapason` WHERE rmin <= NEW.rmax AND rmax >= NEW.rmin LIMIT 0, 1 INTO dst;
if dst is not null then
signal sqlstate '45000'
SET message_text = "cross";
end if;
SET NEW.value = CONCAT("dst: ",IFNULL(dst,'n/a'));
END//
DELIMITER ;
UNLOCK TABLES;