SQL error · INSERT, UPDATE, DELETE and schema

CHECK rejects the value

Problem: The value makes one of the table's CHECK conditions false.

Solution: Fix the data, or review the business rule before you change the constraint.

Why it happens: CHECK rejects the value

Schema and failing operation
CREATE TABLE coupons (
  code TEXT PRIMARY KEY,
  discount INTEGER NOT NULL
    CHECK (discount BETWEEN 1 AND 100)
);

INSERT INTO coupons (code, discount)
VALUES ('SUMMER', 130);

The value 130 is outside the allowed range. If the domain defines the discount as a percentage from 1 to 100, the constraint is catching invalid data.

Check the result with small data

We can evaluate the same CHECK rule without touching any table.

Self-contained query · SQLite
SELECT 130 BETWEEN 1 AND 100 AS valid_value,
       30 BETWEEN 1 AND 100 AS corrected_value;
Query result
valid_valuecorrected_value
01

In SQLite, the expression returns 0 for 130 and 1 for 30. The CHECK constraint should match the valid domain, not bend to whatever data arrives.

Check your understanding: Would 100 be accepted with BETWEEN 1 AND 100?

Show answer

Yes. BETWEEN includes both ends, so 100 meets the rule.

“CHECK rejects the value” error messages by engine

The exact wording depends on the database. Compare these common messages with the one in your console.

EngineTypical message
PostgreSQLERROR: new row for relation "coupons" violates check constraint "discount_coupons_check"
MySQLERROR 3819 (HY000): Check constraint 'coupons_chk_1' is violated.
SQL ServerMsg 547, Level 16: The INSERT statement conflicted with the CHECK constraint "CK_coupons_discount".
OracleORA-02290: check constraint (ESQUEMA.CK_CUPONES_DESCUENTO) violated
SQLiteRuntime error: CHECK constraint failed: discount BETWEEN 1 AND 100

How to fix it in SQL

Valid value
INSERT INTO coupons (code, discount)
VALUES ('SUMMER', 30);

If 130 should be valid under a different business rule, change the model deliberately and document it. Do not change CHECK just to accept one particular row without understanding what it means.

How to check it

Evaluate the condition with the value received: 130 BETWEEN 1 AND 100 returns false.

How to avoid it next time

CHECK does not make a column mandatory: if a missing value is not valid, add NOT NULL.

Related cases worth checking

In PostgreSQL, a CHECK expression that evaluates to NULL does not count as a violation. SQLite also accepts a row when the constraint expression comes out null. If a column must also be required, declare NOT NULL explicitly.

Investigate the rule before changing the value

The constraint's name helps you find the rule that rejected the row. Review the whole expression and the data sent, not just the field that looks suspicious. A limit may be correct while the application sends cents to a column defined in pounds; raising the limit would hide the units problem rather than solve it.

Test values just below, at and just above the limit. If the rule is quantity > 0, zero is not valid; if it is quantity >= 0, it is. Also test a missing value: in SQLite, a null result from CHECK does not reject the row on its own. When the value is required, presence must be enforced with NOT NULL.

Not every business rule fits in a single-row check. Checking stock shared with other operations may need a different transaction strategy. If the rule changes legitimately, prepare a migration and review the existing data before switching it on. If the incoming data is what fails, return an understandable error to the source and keep the constraint as a safeguard for every write path.

Technical questions: SQLite: CREATE TABLE and constraints (opens in a new tab).