I want to use the CHECK constraint in MySQL, but it is not supported. (Unlike other RDBMS, it will understand but not enforce the CHECKs.)
I have seen some workarounds with triggers. But they tend to set a default value to the field in question instead of returning an error.
Is it possible to construct a trigger that returns an error if a condition is not met?
Ultimately I want a trigger that copies a CHECK constraint.
解决方案
Try the following syntax
CREATE TRIGGER mytabletriggerexample
BEFORE INSERT
FOR EACH ROW BEGIN
IF(NEW.important_value) < (fancy * dancy * calculation) THEN
DECLARE dummy INT;
SELECT Your meaningful error message goes here INTO dummy
FROM mytable
WHERE mytable.id=new.id
END IF; END;