I’m trying to create an advanced constraint on a MySQL table. The intent is to ensure that if a certain integer field is null or 0, then a collection of boolean fields are not true. The field “REVIEWEDBY” is the integer field, and all other fields mentioned are the boolean fields. Here’s what I’ve tried:
ALTER TABLE workorders ADD CONSTRAINT CK_Rev_Selection
CHECK (
(REVIEWEDBY <> 0 AND REVIEWEDBY NOT NULL)
OR ( REVDATERECEIVED = 0
AND REVEVCUSTOMERPO = 0
AND REVCONTRACT = 0
AND REVQUANTITY = 0
AND REVSPECIALINST = 0
AND REVCUSTOMER_OVERRIDE = 0
AND REVSHIPPINGADDRESS_OVERRIDE = 0
AND REVSHIPINGADDRESS2_OVERRIDE = 0
AND REVITEMSALL = 0
)
)
(code formatting edited by ajlittoz for better readability without scrollbar)
It is not preventing the situation it is intended to prevent.
I had originally attempted to force this by means of macros that would toggle the enabled status of the boolean controls on the form, but that very quickly became cumbersome and flaky.
Edit: I’ve tried a very simple analog of this at SQLFiddle. Here it is.