CONSTRAINT check failed
ER_CONSTRAINT_FAILED (4025) is returned when a CHECK constraint on a table is violated during INSERT or UPDATE. MariaDB enforces CHECK constraints from version 10.2.1+.
CREATE TABLE products ( id INT PRIMARY KEY, price DECIMAL(10,2), CONSTRAINT chk_price CHECK (price >= 0) ); INSERT INTO products VALUES (1, -5.00);
expected output
ERROR 4025 (23000): CONSTRAINT `chk_price` failed for `mydb`.`products`
Fix 1
Validate the value before inserting
WHEN The application controls the value.
-- Application: validate price >= 0 before insert -- SQL: show all constraints on a table: SELECT CONSTRAINT_NAME, CHECK_CLAUSE FROM INFORMATION_SCHEMA.CHECK_CONSTRAINTS WHERE TABLE_NAME = 'products';
Why this works
INFORMATION_SCHEMA.CHECK_CONSTRAINTS documents all CHECK constraints so applications can mirror the validation logic.
Fix 2
Update or drop the constraint
WHEN The constraint definition is wrong.
-- MariaDB 10.2+: drop and recreate ALTER TABLE products DROP CONSTRAINT chk_price; ALTER TABLE products ADD CONSTRAINT chk_price CHECK (price >= -100);
Why this works
ALTER TABLE can drop and re-add CHECK constraints without rebuilding the entire table in modern MariaDB.
✕ Drop CHECK constraints to suppress validation errors
CHECK constraints are the last line of defence against invalid data in the database layer; application-level validation is not sufficient on its own.
CHECK constraints became enforced in MariaDB 10.2.1. Earlier versions accepted the syntax but did not enforce the constraint.
MariaDB — CHECK Constraints
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev