Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the difference between UNIQUE INDEX and UNIQUE KEY?

Tags:

mysql

I have seen couple of sample tables one is with UNIQUE INDEX and other is with UNIQUE KEY. what is the difference between two?? or both are same?

like image 204
mathew Avatar asked Jul 14 '10 10:07

mathew


People also ask

What is the difference between key and unique key?

A primary key is a column, or a combination of columns, that can uniquely identify a row. It is a special case of unique key. A table can have at most one primary key, but more than one unique key. When you specify a unique key on a column, no two distinct rows in a table can have the same value.

Is unique key same as unique constraint?

Both the UNIQUE and PRIMARY KEY constraints provide a guarantee for uniqueness for a column or set of columns. A PRIMARY KEY constraint automatically has a UNIQUE constraint. However, you can have many UNIQUE constraints per table, but only one PRIMARY KEY constraint per table.

Do we need index on unique key?

Unique indexes are indexes that help maintain data integrity by ensuring that no rows of data in a table have identical key values. When you create a unique index for an existing table with data, values in the columns or expressions that comprise the index key are checked for uniqueness.

Does unique key automatically create index?

Yes, absolutely. A unique constraint creates a unique index.


1 Answers

CREATE TABLE
KEY is normally a synonym for INDEX.

You can use them interchangeably as described in syntax page
[CONSTRAINT [symbol]] UNIQUE [INDEX|KEY]

like image 111
Naktibalda Avatar answered Sep 22 '22 00:09

Naktibalda