You can do this in your application code, but the safest way to do this is in the database using a trigger.
To abort the insertion, create an error in the trigger. We are doing something like this:
CREATE TRIGGER `t_insert` BEFORE UPDATE ON `t` FOR EACH ROW BEGIN IF new.A > new.B THEN CALL NONEXISTENT_PROC() END IF; END
source share