jemz — 2013-10-21T08:58:06-04:00 — #1
Hi, can you help me please to enlighten my mind what is the difference of this two "UNIQUE" and "INDEX" in MySQL,can you please give some little example so that i can understand thoroughly.and also when to use this "UNIQUE" and "INDEX" in my tables.
Thank you in advance
davemaxwell — 2013-10-21T10:09:37-04:00 — #2
I'm sure Rudy will shoot me down if my answer is incomplete, but essentially...
index is simply a means to look up specific rows on a table. Using an index saves the database from having to search each and every row individually. Think of it like an index in the back of a text book. You can use the index at the back of the book to look for a specific topic and find it easily without having to page each and every page to find it.
unique is used when you want to have a value exist exactly once in a column. It allows you to identify individual rows on a table.
If you're of a type that documentation makes sense to you, you can refer to the mySQL page specifically....http://dev.mysql.com/doc/refman/5.0/en/create-index.html
john_betong — 2013-10-21T12:10:14-04:00 — #3
Take a look at the above link and do an advanced search for Jem.
A popup box should appear showing numerous members names beginning with Jem. You will notice that there is an alphabetical INDEX on the members names but only one UNIQUE (oxymoron occurrence of a member's name.
r937 — 2013-10-21T12:22:25-04:00 — #4
na, man, nice job
confusion might arise from the fact that a unique constraint is actually implemented as a unique index -- so if you declare a column as UNIQUE, you do ~not~ need an additional index on it
davemaxwell — 2013-10-21T12:28:52-04:00 — #5
True......if something is defined is unique, it's an index by default. But an index does not have to be unique.
jemz — 2013-10-29T10:46:01-04:00 — #6
HI ,I have question but if we have primary key,..I know primary key is unique, so i don't need anymore to add "unique index" on my table?...sorry i am confuse on this...please correct me if i am wrong...:)
davemaxwell — 2013-10-29T11:20:19-04:00 — #7
Correct, unless there is a non-identifying field that you would for some off reason want one and only one value of.
An example would be a table of users. The primary key would identify each individual record. Now, if for some odd reason, your customer decides they only want one person from each town, you would put a unique index on the town field. From a semantic perspective, you don't want to make this the primary key because the table is dealing with users, so you declare an extra unique index.
r937 — 2013-10-29T12:25:34-04:00 — #8
definitely not on the same column(s)
but you can also declare a unique index on a different column
for example, a table of users which has an auto_increment primary key, but you also want to ensure that no two users have the same name, so you declare a unique index on the name
yes, you could use the name as the primary key, but because users are referenced in many many tables (e.g. last_updated_by columns), you would prefer to use the 4-byte integer as the foreign key
jemz — 2013-11-03T08:48:57-05:00 — #9
HI @DaveMaxwell and @r937, Thank you for helping me...
scallioxtx — 2013-11-03T10:49:14-05:00 — #10
For completeness sake I'd like to add that it possible to add an index or a unique index to multiple columns at once. So if you want to allow multiple users with the same username but only if they have different email addresses for example you can add a unique index to (username, email). That way the combination of the username and email have to be unique, not the columns themselves.