What is CHECK constraint in DB2?

Check constraint is a database rule that checks data integrity. Thus, only values from the domain for the attribute or column are allowed.

In DB2, a CHECK constraint is a type of constraint that is used to enforce a condition on the values that can be inserted or updated in a column of a table. It ensures that the values in the specified column meet the specified condition or expression.

The CHECK constraint is defined at the column level and is used to restrict the range of values that can be stored in that column. When a new row is inserted or an existing row is updated, the CHECK constraint is evaluated to determine whether the values in the specified column meet the specified condition. If the condition is not satisfied, the operation (insert or update) will be rejected.

Here’s a basic syntax for creating a CHECK constraint in DB2:

CREATE TABLE YourTable (
Column1 INT,
Column2 VARCHAR(50),
-- Other columns
CONSTRAINT CheckConstraintName CHECK (Column1 > 0 AND Column2 IS NOT NULL)
);

In this example, a CHECK constraint named CheckConstraintName is defined on YourTable to ensure that Column1 has a value greater than 0 and Column2 is not NULL.

This constraint helps in maintaining data integrity by preventing the insertion or update of rows that do not satisfy the specified condition.