Why it happens: CHECK rejects the value
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.
SELECT 130 BETWEEN 1 AND 100 AS valid_value,
30 BETWEEN 1 AND 100 AS corrected_value;| valid_value | corrected_value |
|---|---|
| 0 | 1 |
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.
How to fix it in SQL
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).