CHECK Constraint

The CHECK constraint specifies that values for the column in INSERT or UPDATE statements must return TRUE or NULL for a Boolean expression. If any values return FALSE, the entire statement is rejected.

Details

  • New in v19.1: If you add a CHECK constraint to an existing table, CockroachDB will run a background job to validate existing table data in the process of adding the constraint. If a row is found that violates the constraint during the validation step, the ADD CONSTRAINT statement will fail. This differs from previous versions of CockroachDB, which allowed you to add a check constraint that was enforced for writes but could be violated by rows that existed prior to adding the constraint.
  • New in v19.1: Check constraints can be added to columns that were created earlier in the same transaction. For an example, see Add the CHECK constraint.
  • CHECK constraints may be specified at the column or table level and can reference other columns within the table. Internally, all column-level CHECK constraints are converted to table-level constraints so they can be handled consistently.
  • You can have multiple CHECK constraints on a single column but ideally, for performance optimization, these should be combined using the logical operators. For example:
  1. warranty_period INT CHECK (warranty_period >= 0) CHECK (warranty_period <= 24)

should be specified as:

  1. warranty_period INT CHECK (warranty_period BETWEEN 0 AND 24)
  • When a column with a CHECK constraint is dropped, the CHECK constraint is also dropped.

Syntax

CHECK constraints can be defined at the table level. However, if you only want the constraint to apply to a single column, it can be applied at the column level.

Note:
You can also add the CHECK constraint to existing tables through ADD CONSTRAINT.

Column level

CREATETABLEtable_name(column_namecolumn_typeCHECK(check_expr)column_constraints,column_deftable_constraints))
ParameterDescription
table_nameThe name of the table you're creating.
column_nameThe name of the constrained column.
column_typeThe constrained column's data type.
check_exprAn expression that returns a Boolean value; if the expression evaluates to FALSE, the value cannot be inserted.
column_constraintsAny other column-level constraints you want to apply to this column.
column_defDefinitions for any other columns in the table.
table_constraintsAny table-level constraints you want to apply.

Example

  1. > CREATE TABLE inventories (
  2. product_id INT NOT NULL,
  3. warehouse_id INT NOT NULL,
  4. quantity_on_hand INT NOT NULL CHECK (quantity_on_hand > 0),
  5. PRIMARY KEY (product_id, warehouse_id)
  6. );

Table level

CREATETABLEtable_name(column_def,CONSTRAINTnameCHECK(check_expr)table_constraints)
ParameterDescription
table_nameThe name of the table you're creating.
column_defDefinitions for any other columns in the table.
nameThe name you want to use for the constraint, which must be unique to its table and follow these identifier rules.
check_exprAn expression that returns a Boolean value; if the expression evaluates to FALSE, the value cannot be inserted.
table_constraintsAny other table-level constraints you want to apply.

Example

  1. > CREATE TABLE inventories (
  2. product_id INT NOT NULL,
  3. warehouse_id INT NOT NULL,
  4. quantity_on_hand INT NOT NULL,
  5. PRIMARY KEY (product_id, warehouse_id),
  6. CONSTRAINT ok_to_supply CHECK (quantity_on_hand > 0 AND warehouse_id BETWEEN 100 AND 200)
  7. );

Usage example

CHECK constraints may be specified at the column or table level and can reference other columns within the table. Internally, all column-level CHECK constraints are converted to table-level constraints so they can be handled in a consistent fashion.

  1. > CREATE TABLE inventories (
  2. product_id INT NOT NULL,
  3. warehouse_id INT NOT NULL,
  4. quantity_on_hand INT NOT NULL CHECK (quantity_on_hand > 0),
  5. PRIMARY KEY (product_id, warehouse_id)
  6. );
  7. > INSERT INTO inventories (product_id, warehouse_id, quantity_on_hand) VALUES (1, 2, 0);
  1. pq: failed to satisfy CHECK constraint (quantity_on_hand > 0)

See also

Was this page helpful?
YesNo