mariadb-syntax-check-constraints
Installation
SKILL.md
MariaDB CHECK Constraints
Deterministic guide for declaring, naming, dropping, and reasoning about CHECK constraints in MariaDB. CHECK constraints are enforced (not parsed-and-ignored) since MariaDB 10.2.1, which precedes MySQL 8 by several years. This skill applies to 10.6-LTS, 10.11-LTS, 11.x, and 12.x.
Quick Reference
ALWAYS remember the following invariants:
- CHECK passes when the predicate evaluates to NULL. SQL three-valued logic (3VL) treats
NULLas "unknown", and a CHECK only fails when the predicate is explicitly FALSE. A nullable columncol INT CHECK (col > 0)will acceptNULL. Combine withNOT NULLto require a value AND a positive value. - CHECK constraints are enforced since MariaDB 10.2.1. Earlier versions parsed
CHECKsyntax but ignored it. MariaDB's enforcement predates MySQL 8.0 (released July 2018) by approximately 18 months. - Use
CHECK (JSON_VALID(col))to validate JSON columns. MariaDB'sJSONtype is aLONGTEXTalias, not native binary; withoutJSON_VALIDyou can insert literal garbage and break every downstreamJSON_EXTRACT. (See L-005 / D-010.) From 10.4.3+, theJSONalias auto-appliesJSON_VALIDas a CHECK, but explicitCHECK (JSON_VALID(col))is required onLONGTEXT/TEXTcolumns used as JSON and recommended in all migration scripts. - INSERT IGNORE turns CHECK violation into a warning. The row is not inserted, but no error is raised. Use plain
INSERTwhen you need hard rejection. - Prefer CHECK over triggers for pure invariants. CHECK is set-based, declarative, faster, and visible in
information_schema.CHECK_CONSTRAINTS. Use triggers only when you need side-effects (audit row, modify other tables, derived columns) or cross-table validation. - Cross-table or sub-query CHECK is not allowed. A CHECK predicate may only reference columns of the same row. Use a
BEFORE INSERT/BEFORE UPDATEtrigger with aSIGNALif you must validate against another table. - Disable globally with
SET SESSION check_constraint_checks = OFFfor bulk-load of legacy data, then re-enable. Disabled checks do not re-validate existing rows on re-enable. auto_incrementcolumns may not appear in a CHECK constraint (permariadb.com/kb/en/constraint/).- Only deterministic functions are permitted in CHECK expressions.
NOW(),RAND(),UUID(),CURRENT_TIMESTAMPARE non-deterministic and MUST NOT appear in CHECK.