Constraints
Constraints
In MySQL, constraints are rules applied to columns in a table to enforce data integrity,
define relationships, and ensure the accuracy and reliability of the data.
UNIQUE
A UNIQUE constraint ensures that all values in a column are UNIQUE across the table,
but it accepts NULL.
PRIMARY KEY
A primary key uniquely identifies each record in a table. Only one primary key is allowed
per table, and it cannot contain NULL values.
FOREIGN KEY
A FOREIGN KEY is used to establish a relationship between two tables.
It links a column in one table (the child table) to the PRIMARY KEY of another table (the
parent table), ensuring referential integrity.
● Parent Table: The table containing the referenced key (usually the primary key).
● Child Table: The table containing the foreign key that references the parent
table.
DEFAULT
Dropping DEFAULT:
CHECK
The check constraint allows you to specify a condition that each value in a column must
satisfy.
AUTO_INCREMENT
This constraint is typically used with a primary key to automatically generate a unique
integer value for new rows.
Setting AUTO_INCREMENT: