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.


About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist