What is CHECK Constraint in SQL Server?

Asked 06-Dec-2019
Updated 06-Apr-2023
Viewed 767 times

1 Answer


0

A CHECK constraint is a type of constraint in SQL Server that ensures that data in a column or set of columns meets a specific condition or set of conditions. When you define a CHECK constraint on a table, SQL Server will evaluate the condition specified in the constraint each time a new row is added to or updated in the table. If the condition is not met, the INSERT or UPDATE operation will fail and an error will be generated.

For example, you could define a CHECK constraint on a column that specifies that the value must be greater than 0 and less than 100. If someone tries to insert or update a row with a value outside of this range, the operation will fail and an error will be generated.

What is CHECK Constraint in SQL Server

CHECK constraints are commonly used to enforce business rules or data integrity rules on a table. For example, you could use a CHECK constraint to ensure that a column contains only valid dates, or to ensure that a column containing employee salaries does not exceed a certain amount.

It is important to note that CHECK constraints can have a performance impact on large tables or tables that are frequently updated. When defining a CHECK constraint, you should ensure that the condition specified is simple and efficient. Additionally, you should avoid using complex functions or subqueries in the condition, as these can significantly impact performance.