Ad space (banner)
🗄️SQL Lessons
Lesson 36 / 50

Restricting Input with CHECK Constraints

This lesson covers restricting input values with a CHECK constraint, so the database itself enforces a rule. It's for anyone searching "SQL CHECK constraint tutorial."

Adding CHECK(condition) to a table definition makes the database reject any value that fails the condition — like "score must be between 0 and 100," enforced by the database itself rather than trusted application code alone.

The example restricts scores to the 0-100 range with score INTEGER CHECK(score >= 0 AND score <= 100). Watch what happens when you try to insert an out-of-range score of 150 — the database rejects it outright.

A common early mistake is assuming application-side validation alone is sufficient — if only the app checks input, a different path (a script, a direct database connection) could still insert bad data. Giving the database its own rules provides a much stronger guarantee of correctness. In real database design, constraints like this act as a last line of defense against bad data slipping in due to an application bug.

SQL
OUTPUT

💡 The SQL engine may take a few seconds to load the first time you run code.

Ad space (banner)
Ad space (in-article)