SQL Check

The CHECK constraint is used to limit the value range that can be placed in a column. If the condition evaluates to FALSE, the data is rejected. This ensures data validity by enforcing rules at the column or table level.

Tutorials dojo strip

SQL Check Syntax

SQL

You can also define a CHECK constraint on multiple columns:

SQL

SQL Check Example

SQL Check on Create Table

This query applies a CHECK constraint while creating the table, ensuring that the date of birth (dob) is not in the future:

SQL

SQL Check on Alter Table

SQLite does not support adding CHECK constraints using ALTER TABLE. The only workaround is to recreate the table with the new CHECK constraint and copy the data from the original table.

Steps:

  1. Create a new table with the desired CHECK constraint:
SQL
  1. Copy data from the existing table:
SQL
  1. Drop the old table (optional — do with caution):
SQL
  1. Rename the new table:
SQL

SQL Check Drop a Check Constraint

SQLite does not support dropping a CHECK constraint directly.

If needed, you must:

  • Create a new table without the constraint
  • Move the data over
  • Drop the old table
  • Rename the new one

SQL Check Labs

Tutorials dojo strip