Tim Tim - 1 month ago 8
MySQL Question

Understanding MYSQL composite index

I have been reading some code and come across

UNIQUE KEY User_name (User_name,Email)


why user_name is repeated in this composite index? why not just
UNIQUE KEY (User_name,Email)
?

Answer

This is known as a together unique index. When both fields are considered together their values cannot be repeated but individual fields may repeat.

For example there may be several entries in the username column called Tim, and there maybe several entires in the email field named tim@example.com however there can be only one username=tim, email=tim@example.com

If you want usernames to be unique (which is usually the case) you need to make that column unique on it's own (you can also make it the primary key).

Comments