CHECK Constraint

SQL · Working with Tables

Theory

A CHECK constraint is a rule, written as a condition, that every row must satisfy. salary DECIMAL(10,2) CHECK (salary > 0) rejects zero or negative salaries; CHECK (status IN ('active', 'left')) limits a column to known values; a table-level CHECK (end_date >= start_date) compares two columns of the same row.

The condition is tested on every INSERT and UPDATE, and the statement fails if the result is FALSE. A subtle rule: a CHECK passes when the result is UNKNOWN, so a NULL salary is accepted by CHECK (salary > 0). Add NOT NULL if the value is required. CHECK can only look at the current row; rules that involve other rows or tables need foreign keys, triggers or application logic. Name constraints (CONSTRAINT chk_salary CHECK (...)) to get clearer error messages. Older MySQL versions parsed CHECK but ignored it.

Why we use it

Some values are simply impossible: negative quantities, percentages above 100, end dates before start dates. CHECK blocks them inside the database so no application or manual fix can slip bad data in.

When to use it

Use CHECK for simple, row-level business rules on values. For lists of allowed values that change often, a lookup table with a foreign key is easier to maintain. For cross-row rules, use triggers or application code.

How it works

  1. INSERT or UPDATE a row
  2. Evaluate the CHECK condition for that row
  3. Is the result FALSE? Yes: Reject the statement. No: TRUE or UNKNOWN (NULL): accept the row.
  4. Repeat for every row the statement touches
  5. Only rule-abiding rows are stored

Syntax

column datatype CHECK (condition)

Example

CREATE TABLE employees (
  salary DECIMAL(10,2) CHECK (salary > 0)
);

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic