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
| Constraint | Description |
|---|---|
NOT NULL | Ensures a column cannot have a NULL value. |
UNIQUE | Ensures all values in a column are different. |
PRIMARY KEY | A combination of NOT NULL and UNIQUE. Uniquely identifies each row. |
FOREIGN KEY | Prevent actions that would destroy links between tables. |
CHECK | Ensures that the values in a column satisfy a specific condition. |
DEFAULT | Sets 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.
Violating Constraints
Try inserting a row that violates the CHECK constraint (Age < 18) or NOT NULL.