> ## Documentation Index
> Fetch the complete documentation index at: https://cockroachlabs.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# CHECK Constraint

export const InternalLink = ({version, path = "", children, ...props}) => {
  let detectedVersion = version || "stable";
  if (typeof window !== 'undefined' && !version) {
    const match = window.location.pathname.match(/\/docs\/([^/]+)/);
    if (match) {
      detectedVersion = match[1];
    }
  }
  const normalizedPath = path.startsWith("/") ? path.slice(1) : path;
  return <a href={`/docs/${detectedVersion}/${normalizedPath}`} {...props}>
      {children}
    </a>;
};

The `CHECK` <InternalLink path="constraints">constraint</InternalLink> specifies that values for the column in <InternalLink path="insert">`INSERT`</InternalLink> or <InternalLink path="update">`UPDATE`</InternalLink> statements must return `TRUE` or `NULL` for a Boolean expression. If any values return `FALSE`, the entire statement is rejected.

## Details

* You can specify `CHECK` constraints 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 add `CHECK` constraints to columns that were created earlier in the same transaction. For an example, see <InternalLink path="alter-table#add-constraints-to-columns-created-during-a-transaction">Add the `CHECK` constraint</InternalLink>.
* You can have multiple `CHECK` constraints on a single column but for performance optimization you should combine them using logical operators. For example, you should specify:

  ```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
  warranty_period INT CHECK (warranty_period >= 0) CHECK (warranty_period <= 24)
  ```

  as:

  ```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
  warranty_period INT CHECK (warranty_period BETWEEN 0 AND 24)
  ```
* When you drop a column with a `CHECK` constraint, the `CHECK` constraint is also dropped.

## Syntax

You can define `CHECK` constraints at the [column level](#column-level), where the constraint applies only to a single column, and at the [table level](#table-level).

You can also add `CHECK` constraints to a table using <InternalLink path="alter-table#add-the-check-constraint">`ADD CONSTRAINT`</InternalLink>.

### Column level

<img src="https://mintcdn.com/cockroachlabs/N8jqu5GnKPMC09TX/images/sql-diagrams/v25.4/check_column_level.svg?fit=max&auto=format&n=N8jqu5GnKPMC09TX&q=85&s=8c197f6ada61f19f0f6e21e7b5548efc" alt="check_column_level syntax diagram" style={{maxWidth: "100%", overflowX: "auto"}} width="747" height="249" data-path="images/sql-diagrams/v25.4/check_column_level.svg" />

| Parameter             | Description                                                                                                          |
| --------------------- | -------------------------------------------------------------------------------------------------------------------- |
| `table\_name`         | The name of the table you're creating.                                                                               |
| `column\_name`        | The name of the constrained column.                                                                                  |
| `column\_type`        | The constrained column's <InternalLink path="data-types">data type</InternalLink>.                                   |
| `check\_expr`         | An expression that returns a Boolean value; if the expression evaluates to `FALSE`, the value cannot be inserted.    |
| `column\_constraints` | Any other column-level <InternalLink path="constraints">constraints</InternalLink> you want to apply to this column. |
| `column\_def`         | Definitions for any other columns in the table.                                                                      |
| `table\_constraints`  | Any table-level <InternalLink path="constraints">constraints</InternalLink> you want to apply.                       |

#### Example

The following example specifies the column-level `CHECK` constraint that a `quantity_on_hand` value must be greater than `0`.

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> CREATE TABLE inventories (
    product_id        INT NOT NULL,
    warehouse_id      INT NOT NULL,
    quantity_on_hand  INT NOT NULL CHECK (quantity_on_hand > 0),
    PRIMARY KEY (product_id, warehouse_id)
  );
```

### Table level

<img src="https://mintcdn.com/cockroachlabs/N8jqu5GnKPMC09TX/images/sql-diagrams/v25.4/check_table_level.svg?fit=max&auto=format&n=N8jqu5GnKPMC09TX&q=85&s=f5802cc6c97f7ffdb106db716c751ffc" alt="check_table_level syntax diagram" style={{maxWidth: "100%", overflowX: "auto"}} width="631" height="277" data-path="images/sql-diagrams/v25.4/check_table_level.svg" />

| Parameter            | Description                                                                                                                                                                       |
| -------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `table\_name`        | The name of the table you're creating.                                                                                                                                            |
| `column\_def`        | Definitions for any other columns in the table.                                                                                                                                   |
| `constraint\_name`   | The name to use for the constraint, which must be unique to its table and follow these <InternalLink path="keywords-and-identifiers#identifiers">identifier rules</InternalLink>. |
| `check\_expr`        | An expression that returns a Boolean value. If the expression evaluates to `FALSE`, the value cannot be inserted.                                                                 |
| `table\_constraints` | Any other table-level <InternalLink path="constraints">constraints</InternalLink> to apply.                                                                                       |

#### Example

The following example specifies the table-level `CHECK` constraint named `ok_to_supply` that a `quantity_on_hand` value must be greater than `0` and a `warehouse_id` must be between `100` and `200`.

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> CREATE TABLE inventories (
    product_id        INT NOT NULL,
    warehouse_id      INT NOT NULL,
    quantity_on_hand  INT NOT NULL,
    PRIMARY KEY (product_id, warehouse_id),
    CONSTRAINT ok_to_supply CHECK (quantity_on_hand > 0 AND warehouse_id BETWEEN 100 AND 200)
  );
```

## Usage example

The following example demonstrates that when you specify the `CHECK` constraint that a `quantity_on_hand` value must be greater than `0`, and you attempt to insert the value `0`, CockroachDB returns an error.

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> CREATE TABLE inventories (
    product_id        INT NOT NULL,
    warehouse_id      INT NOT NULL,
    quantity_on_hand  INT NOT NULL CHECK (quantity_on_hand > 0),
    PRIMARY KEY (product_id, warehouse_id)
  );

> INSERT INTO inventories (product_id, warehouse_id, quantity_on_hand) VALUES (1, 2, 0);
```

```text theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
pq: failed to satisfy CHECK constraint (quantity_on_hand > 0)
```

## See also

* <InternalLink path="constraints">Constraints</InternalLink>
* <InternalLink path="alter-table#drop-constraint">`DROP CONSTRAINT`</InternalLink>
* <InternalLink path="default-value">`DEFAULT` constraint</InternalLink>
* <InternalLink path="foreign-key">`REFERENCES` constraint (Foreign Key)</InternalLink>
* <InternalLink path="not-null">`NOT NULL` constraint</InternalLink>
* <InternalLink path="primary-key">`PRIMARY KEY` constraint</InternalLink>
* <InternalLink path="unique">`UNIQUE` constraint</InternalLink>
* <InternalLink path="show-constraints">`SHOW CONSTRAINTS`</InternalLink>
