Unique field in MySQL

Create a Unique field in MySQL

To create a unique column in mysql we have 2 ways.

1 On Create Table

Method 1

CREATE TABLE table_name
(
    name VARCHAR(255) UNIQUE
);

Method 2

CREATE TABLE table_name
(
    name VARCHAR(255),
    UNIQUE KEY(name)
);

2 On Alter Table

ALTER TABLE table_name ADD UNIQUE KEY(name);