SQL Constraints
SQL constraints are used to specify rules for the data in a table.
If there is any violation of the constraints, the action is aborted by the constraint.
Constraints can be specified when the table is created (with the CREATE TABLE statement) or after the table is created (with the ALTER TABLE statement).
SQL CREATE TABLE + CONSTRAINT Syntax
In SQL, we have the following constraints:
NOT NULL - Ensures that a column cannot have a NULL value.
UNIQUE - Ensures that all values in a column are different.
PRIMARY KEY - A combination of NOT NULL and UNIQUE. Uniquely identifies each row in a table.
FOREIGN KEY - Prevents 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.
In the following sections, we will go into detail about each constraint.