What is MySQL Constraint and Why is it used?
Constraints are rules that you apply to table columns to make sure the data is correct. They help stop wrong or unwanted data from getting into the table, so the data stays valid and safe.
Constraints are used to prevent duplicate, invalid, or unwanted data from being entered and stored in a table by mistake. They prevent this type of data from being stored in the table and help maintain data integrity, accuracy, and consistency.
The list of Constraint types is:
MySQL Constraints and Their Uses:
MySQL has different types of constraints which control the data added to a table. Each constraint has a different use.
Some constraints stop duplicate data, some stop empty values, and some check if the data follows a rule.
The table below shows the main types of MySQL constraints and what they do.
Constraint type Work Example PRIMARY KEY It identifies each row with a unique value. Student ID
FOREIGN KEY It connects two tables. Student ID
UNIQUE It does not allow duplicate values. Email
NOT NULL It does not allow an empty value. Student Name
CHECK It checks if the value follows a rule. Age ≥ 18
DEFAULT It adds a value when no value is given. Country = India












