SQL Server › Constraints (Primary Key, Foreign Key, Check & Unique)

What is a check constraint, and how do you use one to enforce a specific business rule at the database level?

Intermediate

Answer

A check constraint defines a condition that every value in a column, or combination of columns, must satisfy before a row can be inserted or updated, letting you enforce business rules like requiring a price to always be positive directly in the database, rather than relying solely on application code to catch invalid data.
CREATE TABLE Products (
  ProductId INT PRIMARY KEY,
  Price DECIMAL(10,2) CHECK (Price > 0)
);
Real-world example A product catalog enforces that every product's price must always be greater than zero directly at the database level, preventing accidental negative pricing even if a bug exists somewhere in the application code.

Common follow-ups: Can a check constraint reference more than one column at the same time?;What happens if existing data violates a new check constraint being added?