Skip to content

Instantly share code, notes, and snippets.

@danielestevez
Created October 15, 2014 09:05
Show Gist options
  • Save danielestevez/20fd96ec01a18f2fe675 to your computer and use it in GitHub Desktop.
Save danielestevez/20fd96ec01a18f2fe675 to your computer and use it in GitHub Desktop.
Complex SQL Constraints as a Trigger
CREATE TRIGGER complex_constraint BEFORE INSERT ON my_table
FOR EACH ROW
BEGIN
IF /*complex constraint*/(NEW.field1 IS NOT NULL AND NEW.field2 IS NOT NULL) OR (NEW.field1 IS NULL AND NEW.field2 IS NULL) THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'complex constraint exception message';
END IF;
END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment