UNIQUE, CHECK and DEFAULT Constraints
This lesson explains how UNIQUE, CHECK and DEFAULT constraints enforce business rules, how NULL values behave with them and how to add them to existing tables.
Learning outcomes
After completing this lesson, you will be able to:
- Explain how UNIQUE constraints and filtered unique indexes treat NULL values.
- Describe how CHECK constraints evaluate expressions, including REGEXP_LIKE patterns.
- Identify when DEFAULT values apply and the effect of WITH NOCHECK.
UNIQUE constraints
A UNIQUE constraint prevents duplicate values in a column or combination of columns, such as an email address or a (TenantID, SKU) pair, and is enforced by a unique index. Unlike a primary key, it allows NULL, but only one NULL per column set, because SQL Server treats NULLs as equal for uniqueness. When many rows may have no value but non-NULL values must be unique, a filtered unique index with WHERE Email IS NOT NULL is used instead.
Uniqueness with NULLs
CHECK constraints
A CHECK constraint is a Boolean expression evaluated for each inserted or updated row; rows for which it is FALSE are rejected, while UNKNOWN results caused by NULL pass, so a required value also needs NOT NULL. Constraints can compare columns of the same row, such as CHECK (EndDate > StartDate), and can use functions, including REGEXP_LIKE for format validation. Rules that span rows or tables need a unique constraint, a foreign key or a trigger instead.
CREATE TABLE Sales.Customer
(
CustomerID int IDENTITY PRIMARY KEY,
Email varchar(320) NULL,
Country char(2) NOT NULL CONSTRAINT DF_Customer_Country DEFAULT ('US'),
CreditLimit decimal(10,2) NOT NULL CONSTRAINT CK_Customer_Credit CHECK (CreditLimit BETWEEN 0 AND 50000),
CreatedUtc datetime2(3) NOT NULL CONSTRAINT DF_Customer_Created DEFAULT (SYSUTCDATETIME()),
CONSTRAINT CK_Customer_Email CHECK (REGEXP_LIKE(Email, '^[^@\s]+@[^@\s]+\.[^@\s]+$'))
);
CREATE UNIQUE INDEX UX_Customer_Email ON Sales.Customer (Email) WHERE Email IS NOT NULL;Which constraint enforces it?
DEFAULT constraints and adding constraints later
A DEFAULT constraint supplies a value when an INSERT omits the column, or uses the DEFAULT keyword; it can be a constant, a function such as SYSUTCDATETIME() or NEWSEQUENTIALID(), or NEXT VALUE FOR a sequence. Adding a NOT NULL column with a default to an existing table fills existing rows with the default value.
Adding a CHECK or FOREIGN KEY constraint to a table with data validates existing rows by default and fails if any violate it. Bad data should be fixed first; adding the constraint WITH NOCHECK enforces it only for new changes and leaves it untrusted. Naming constraints explicitly (for example CK_Customer_Credit) makes error messages and later changes clearer than system-generated names.
Defaults and existing data
Optional unique email
Customers may register without an email address, but two customers must never share the same address. A UNIQUE constraint on Email fails when the second customer without an email is inserted. How should the developer enforce the rule?
Drop the UNIQUE constraint and create a filtered unique index on Email WHERE Email IS NOT NULL. It enforces uniqueness among real addresses and ignores the rows without one. A UNIQUE constraint treats NULLs as equal, so it allows only one NULL, and a CHECK constraint cannot compare a row with other rows.
A CHECK (Quantity > 0) constraint exists, and a row is inserted with Quantity NULL. Is it rejected?
Show answer
No. The expression evaluates to UNKNOWN, which passes; NOT NULL is needed to require a value.
Exam Tips
Remember for the exam
- UNIQUE allows one NULL; many NULLs with unique non-NULL values: a filtered unique index.
- CHECK passes when the expression is UNKNOWN; add NOT NULL for required values.
- Format validation in a constraint: CHECK with REGEXP_LIKE.
- DEFAULT applies only when the INSERT omits the column or uses DEFAULT.