Constraints

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

CONSTRAINTS

IN
MySQL

@NANDINI IKHANKAR
Constraints in MySQL are rules
applied to a column or a set of
columns in a table that enforce data
integrity. They help maintain data
accuracy, consistency, and integrity
within the database.
NOT NULL
By default, a column can hold NULL
values. This constraint ensures that a
column cannot have a NULL value. It
forces the column to always contain a
value.
UNIQUE
The UNIQUE constraint ensures that all
values in a column are unique (no
duplicates). It can be applied to one or
multiple columns.
PRIMARY KEY
The PRIMARY KEY constraint uniquely
identifies each record in a table. It
enforces both the NOT NULL and UNIQUE
constraints on the specified column(s).
FOREIGN KEY
The FOREIGN KEY constraint establishes a
relationship between two tables. It
ensures that the values in a column match
the values in a referenced column in
another table.
CHECK
The CHECK constraint is used to limit the
range of values that can be placed in a
column. It defines a condition that each
row must satisfy.
DEFAULT
The DEFAULT constraint is used to set a
default value for a column if no value is
specified during an insertion.
AUTO_INCREMENT
The AUTO_INCREMENT constraint is used
to generate a unique number
automatically when a new record is
inserted into a table.
summary
MySQL provides a robust set of constraints
to ensure data consistency and integrity
within the database, contributing to better
data quality and reliability.
Above examples illustrate the
implementation of different constraints in
MySQL tables, enforcing data integrity,
uniqueness, relationships between tables,
and default values. Each constraint plays a
vital role in maintaining a well-structured
and reliable database schema.

You might also like