MySQL doesn't enforce check constraints.
This is a well documented deviation from the SQL standard. (Though it unexpected by the uninitiated.)
If you need the MySQL database to enforce a "check constraint", the enforcement has to be coded into a BEFORE INSERT
and a BEFORE UPDATE
trigger.
This note:
The CHECK
clause is parsed but ignored by all storage engines.
is buried in the MySQL Reference Manual, under the CREATE TABLE
syntax.
Reference: https://dev.mysql.com/doc/refman/5.5/en/create-table.html
WARNING ABOUT ENUM
An ENUM
does not restrict "invalid" values from being inserted; an invalid value is translated into a zero length string, a warning is issued, but it's not an error.
Demonstration:
CREATE TABLE foo (gen ENUM('M','F'))
INSERT INTO foo (gen) VALUES ('x')
-- Warning Code : 1265
-- Data truncated for column 'gen' at row 1
SELECT gen, CHAR_LENGTH(gen) FROM foo;
-- gen CHAR_LENGTH(gen)
-- --- ----------------
-- 0