Runs constraint and integrity checks to ensure existing rows are consistent and tables are free of corruption.
Validation catches bad rows before they break reports, enforces business rules after a bulk load, and detects physical corruption early. A quick ALTER TABLE ... VALIDATE CONSTRAINT
or CHECK TABLE
run is cheaper than fixing production bugs later.
ALTER TABLE ... VALIDATE CONSTRAINT
?Use this statement to re-check an existing FOREIGN KEY, UNIQUE, or CHECK constraint on live data. MariaDB scans all rows and fails if any violate the rule.
-- Enable & validate after loading orphaned Orders rows is fixed
ALTER TABLE Orders
VALIDATE CONSTRAINT orders_customer_id_fk;
The command aborts on the first order whose customer_id
is missing from Customers
.
CHECK TABLE
scans the underlying storage engine for corruption. Add modifiers to balance speed vs. depth.
CHECK TABLE Orders QUICK;
QUICK
reads only the table headers, finishing in seconds even on busy ecommerce workloads.
mysqlcheck
loops through every table, performing the same checks you would run manually. Pipe it into cron for scheduled assurance.
# Validate and auto-repair the ecommerce database nightly
mysqlcheck --databases ecommerce --check --auto-repair
Run ALTER TABLE ... VALIDATE CONSTRAINT
immediately after large migrations. Schedule mysqlcheck
during low-traffic windows. Always use transactions when fixing invalid rows so you can roll back on error.
Skipping validation on new constraints and trusting foreign keys are enforced are common errors. See the next section for details.
ALTER TABLE ... VALIDATE CONSTRAINT
acquires a shared read lock; DML can continue but DDL waits. CHECK TABLE ... EXTENDED
may block writes, so plan maintenance windows.
Select the offending rows with a LEFT JOIN against the referenced table, correct or delete them, then rerun VALIDATE CONSTRAINT
.
No. It scans data but leaves indexes untouched unless corruption is detected.
You can kill the session, but partial validation rolls back. Plan time for the full scan.
MariaDB 10.2+ introduced CHECK constraints and validation syntax. Upgrade older installations first.