devxlogo

Understanding the Unique Keyword in MySQL

Understanding the Unique Keyword in MySQL

At times, we need a certain combination of column values to be unique in a table. With MySQL, this can be achieved using the UNIQUE keyword.

There can be a combination of one or more keys and the same will be validated before inserting the data to the table.

Example: Table named DETAILS is created as below.

CREATE TABLE 'Details' (   'ai' INT(11) NOT NULL ,   'name' VARCHAR(50) NULL DEFAULT NULL ,   'address' VARCHAR(100) NULL DEFAULT NULL COLLATE 'utf8_bin',   UNIQUE (ai,name))COLLATE='utf8_bin'ENGINE=InnoDB; 

This ensure that the values in column ai and name combined are unique in nature. This can be made for any combination of columns.

devx-admin

Share the Post: