SQL

SQL Constraints

Learn how to enforce rules on data in columns.

By TechCoder TeamLast updated: 2026-06-02
In a Nutshell

Learn how to enforce rules on data in columns. This hands-on tutorial focuses on practical implementation of sql constraints concepts.

What are Constraints?

Constraints are rules applied to columns in a table. They ensure the accuracy and reliability of the data.

Common Constraints

ConstraintDescription
NOT NULLEnsures a column cannot have a NULL value.
UNIQUEEnsures all values in a column are different.
PRIMARY KEYA combination of NOT NULL and UNIQUE. Uniquely identifies each row.
FOREIGN KEYPrevent actions that would destroy links between tables.
CHECKEnsures that the values in a column satisfy a specific condition.
DEFAULTSets a default value for a column if no value is specified.

Example: Creating a Table with Constraints

Try creating a new table Employees with various constraints.

SQL PLAYGROUND
⏳ Loading editor…

Violating Constraints

Try inserting a row that violates the CHECK constraint (Age < 18) or NOT NULL.

SQL PLAYGROUND
⏳ Loading editor…