What Happens When You Insert a Duplicate into a UNIQUE Key?
A UNIQUE key ensures that all values in the key column (or column group) remain distinct. If you insert a duplicate value, MySQL rejects the operation and raises an error because the uniqueness rule is violated.
MySQL checks the UNIQUE index before inserting or updating a row.
If the value already exists, MySQL blocks the operation.
This prevents duplicate values and ensures data integrity at the key level.
The second insert fails with a duplicate key error.
UNIQUE key columns can store multiple NULL values because NULL is not considered equal to any value.
In a composite UNIQUE key, the combination must be unique even if individual columns repeat.