Skip to main content
Log inGet a demo
← SQL Dictionary
Constraints

SQL CHECK

What is SQL CHECK?

A CHECK constraint in SQL is a database constraint that specifies a condition that data must meet for an operation to be allowed. It ensures that data inserted or updated in a table adheres to a specified condition or expression, maintaining data integrity and consistency.

When you would use it

You would use a CHECK constraint in SQL when you need to:

  1. Enforce Data Integrity: Ensure that data adheres to specific rules or conditions, preventing invalid or inappropriate data from being inserted into a table.

  2. Implement Business Rules: Enforce business rules that require certain data conditions to be met, such as validating age, salary ranges, or product quantities.

  3. Customize Data Validation: Define custom data validation rules to meet the specific requirements of your application.

  4. Ensure Data Quality: Validate data accuracy by ensuring that it conforms to predefined rules or constraints.

Syntax

The syntax for defining a CHECK constraint in SQL is as follows:

CREATE TABLE table_name (
    column1 data_type,
    column2 data_type,
    -- ...
    CONSTRAINT check_name CHECK (condition)
);
  • table_name: The name of the table for which you're creating the CHECK constraint.
  • column1, column2, ...: The columns in the table.
  • check_name: An optional name for the CHECK constraint.
  • condition: The condition or expression that data must meet to be valid.

Parameter values

  • table_name: The name of the table for which you're defining the CHECK constraint.
  • column1, column2, ...: The columns within the table that you want to apply the CHECK constraint to.
  • check_name: An optional name for the CHECK constraint, which can be used to reference the constraint.
  • condition: The condition or expression that specifies the rule to be enforced. This condition can involve one or more columns.

Example query

Here's an example SQL query that creates a "employees" table with a CHECK constraint that enforces a minimum age of 18:

CREATE TABLE employees (
    employee_id INT PRIMARY KEY,
    first_name VARCHAR(50),
    last_name VARCHAR(50),
    date_of_birth DATE,
    -- Other columns
    CONSTRAINT check_age CHECK (DATE_DIFF(CURDATE(), date_of_birth) >= 6570)
);

Example table response

The "employees" table is created with a CHECK constraint enforcing a minimum age of 18. The table might contain data like this:

employee_idfirst_namelast_namedate_of_birth
1JohnDoe2000-05-15
2JaneSmith2005-09-22
3BobJohnson1999-12-10

Use cases

  1. Enforce Data Integrity: To ensure that data adheres to specific rules or conditions, preventing invalid or inappropriate data from being inserted into a table.

  2. Implement Business Rules: To enforce business rules that require certain data conditions to be met, such as validating age, salary ranges, or product quantities.

  3. Customize Data Validation: Define custom data validation rules to meet the specific requirements of your application.

  4. Ensure Data Quality: Validate data accuracy by ensuring that it conforms to predefined rules or constraints.

SQL Languages Availability

The concept of a CHECK constraint is available in most SQL-based relational database management systems (RDBMS), including but not limited to:

  • MySQL
  • PostgreSQL
  • Microsoft SQL Server
  • Oracle Database
  • IBM Db2
  • SQLite

While the core concept of CHECK constraints is universal, the specific implementation and syntax may vary between different database systems. Always consult your specific RDBMS documentation for precise details on using CHECK constraints.


Keep reading

SQL FOREIGN KEY

Read article

SQL DEFAULT

Read article

SQL Create Index

Read article

Ready to put your SQL knowledge to work?

Practice writing SQL to call data from the warehouse and sync it into Google Sheets in this 5 minute interactive demo.

Hightouch Audiences user interface.