MySQL Tutorial
The UNIQUE
constraint in MySQL ensures that all values in a column or a set of columns are distinct (no duplicates). It is commonly used to prevent duplicate data in database tables.
A PRIMARY KEY
constraint automatically has a UNIQUE
constraint.
However, you can have many UNIQUE
constraints per table, but only one PRIMARY KEY
constraint per table.
email
→ Ensures no two students have the same email.phone_number
→ Named unique_phone
to ensure phone numbers are unique.You can apply the UNIQUE constraint to multiple columns, ensuring the combination of values is unique.