Error
Error Code: 3814

MySQL Error 3814: Disallowed Function in CHECK Constraint

📦 MySQL
📋

Description

This error indicates that a `CHECK` constraint defined on a table attempts to use a function that MySQL does not permit within constraint expressions. It typically occurs when creating or altering a table with a `CHECK` constraint that includes non-deterministic, user-defined, or other restricted functions.
💬

Error Message

An expression of a check constraint '%s' contains disallowed function: %s.
🔍

Known Causes

3 known causes
⚠️
Usage of Non-Deterministic Functions
The `CHECK` constraint expression includes functions like `RAND()`, `UUID()`, `NOW()`, or `CURRENT_TIMESTAMP()` that can produce different results each time they are evaluated, which is not allowed.
⚠️
Inclusion of User-Defined or Stored Functions
The constraint attempts to call a custom user-defined function (UDF) or a stored function, which MySQL restricts within `CHECK` constraint definitions.
⚠️
Use of Restricted Built-in Functions
Certain built-in MySQL functions are explicitly disallowed in `CHECK` constraints due to their behavior or potential side effects, even if they appear deterministic.
🛠️

Solutions

3 solutions available

1. Remove or Rewrite the Disallowed Function easy

Modify the CHECK constraint to exclude or replace the problematic function.

1
Identify the specific CHECK constraint causing the error. The error message will usually name it (e.g., '%s' in the error message).
2
Examine the expression within that CHECK constraint. Pinpoint the disallowed function (e.g., a UDF, aggregate function, or stored procedure call).
3
Rewrite the CHECK constraint expression to use only allowed functions. Allowed functions typically include basic arithmetic operators, comparison operators, logical operators, and certain built-in scalar functions like `IF`, `COALESCE`, `NULLIF`, `GREATEST`, `LEAST`, `LENGTH`, `SUBSTRING`, etc. Avoid functions that depend on the state of other rows (aggregates) or external logic (UDFs, stored procedures).
ALTER TABLE your_table DROP CONSTRAINT your_check_constraint_name;
ALTER TABLE your_table ADD CONSTRAINT your_check_constraint_name CHECK (new_expression);
4
If the original function's logic is essential, consider implementing it in a trigger or application-level validation instead of the CHECK constraint.

2. Replace User-Defined Functions (UDFs) or Stored Procedures medium

If a UDF or stored procedure is used, rewrite its logic directly or in an alternative way.

1
Locate the CHECK constraint that references a UDF or stored procedure.
2
Understand the exact logic performed by the UDF or stored procedure.
3
Rewrite the logic of the UDF/stored procedure directly within the CHECK constraint expression if possible. This might involve breaking down complex logic into simpler, allowed scalar functions and operators.
ALTER TABLE your_table DROP CONSTRAINT your_check_constraint_name;
ALTER TABLE your_table ADD CONSTRAINT your_check_constraint_name CHECK (column1 > 0 AND column2 IS NOT NULL AND LENGTH(column3) < 50);
4
If direct rewrite is not feasible due to complexity, consider implementing the validation logic in an `BEFORE INSERT` or `BEFORE UPDATE` trigger. This allows for more complex procedural logic.
DELIMITER //
CREATE TRIGGER validate_data_before_insert
BEFORE INSERT ON your_table
FOR EACH ROW
BEGIN
    -- Your validation logic here, potentially calling allowed functions
    IF NOT (NEW.column1 > 0 AND NEW.column2 IS NOT NULL) THEN
        SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Invalid data provided.';
    END IF;
END; //
DELIMITER ;

DELIMITER //
CREATE TRIGGER validate_data_before_update
BEFORE UPDATE ON your_table
FOR EACH ROW
BEGIN
    -- Your validation logic here
    IF NOT (NEW.column1 > 0 AND NEW.column2 IS NOT NULL) THEN
        SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Invalid data provided.';
    END IF;
END; //
DELIMITER ;

3. Remove the CHECK Constraint Temporarily easy

Disable the constraint to allow data modification and then re-evaluate its necessity or implementation.

1
Identify the CHECK constraint causing the error.
2
Temporarily disable or drop the constraint to allow operations that were failing.
ALTER TABLE your_table DISABLE CONSTRAINT your_check_constraint_name; -- For MySQL 8.0.16+ or use DROP for older versions
-- OR for older MySQL versions:
ALTER TABLE your_table DROP CONSTRAINT your_check_constraint_name;
3
Perform the data modification or insertion that was blocked by the constraint.
4
Re-evaluate the necessity of the constraint. If it's still needed, rewrite it using only allowed functions or implement the logic in a trigger as described in Solution 2.
ALTER TABLE your_table ADD CONSTRAINT your_check_constraint_name CHECK (new_valid_expression);
🔗

Related Errors

5 related errors