UNIQUE Constraint Syntax
The syntax for using UNIQUE constraints in MySQL can vary depending on whether you are adding constraints during creation or while updating the table. You can use the following syntaxes depending on the situation.
UNIQUE Constraint on CREATE TABLE
To add a UNIQUE Constraint on the CREATE TABLE statement, use the following syntax:
CREATE TABLE table_name (
column_name data_type UNIQUE,
...
);
Syntax to add UNIQUE constraint on multiple columns:
CREATE TABLE table_name (
column_name1 data_type,
column_name2 data_type,
...
UNIQUE (column_name1, column_name2)
);
UNIQUE Constraint on ALTER TABLE
To add UNIQUE constraint while updating the table, use the following syntax:
ALTER TABLE table_name
ADD CONSTRAINT constraint_name UNIQUE (column_name);
MySQL UNIQUE Constraint
MySQL UNIQUE constraint ensures that the values in a column or group of columns remain unique, preventing duplicate entries in a column and maintaining the integrity of the table.