Attribute-Attribute Dependency So as part of our college project, I'm working on CENSUS Database. I stumbled upon a problem wherein if an individual is below the age of 18, his/her marital status should only allow 'NA' and anyone above 18 should be allowed to choose from any of the following values ('Married', 'Widowed', 'Single', etc). The information on CONSTRAINTS doesn't seem to be helping. Not finding any way to implement this kind of constraint mechanism, please suggest.
MySQL does not allow to define those kind of constraints, the only ones that are allowed are those related to indexes, such as UNIQUE or FOREIGN KEYs, or invalid data types. CHECK is not supported.

There are several options, depending on what you want to achieve.


First, there is obviously the option of doing application-only checks.
The recommended way on server side is to revoke INSERT and UPDATE access to the table from the user editing the information and enforce the usage of a couple of stored procedures to check the data before actually inserting it, so that the user has to CALL insert_my_table(colums, of, my, table).
You can also do some "hacks" like using a BEFORE INSERT/UPDATE TRIGGER that will force the transaction to fail if it detects a "wrong" value, as it was suggested on the previous link. This is easier to implement but a bit more ugly.