That is a rather basic question for this forum. You might want to look at some basic database tutorials for more insight into what it is all about.
As I see it (and I am self-learned/book learned so my explanation please take with a grain of salt), the unique index ensures that rows will not be added to the database table, either via an INSERT or via an UPDATE query, that will result in the key no longer being unique. You will get an error instead.
Your unique key can consist of only one table column or many.
Why is it needed? Your database design may require that certain columns, or combination of columns only appear once in the table. For example, if your table is holding users of a site, and the users are to select a user name, and this user name must be unique to the site, you can enforce this in your database by making the username a unique index. You can also not do this and write the code that talks to the database, e.g. PHP, so that the uniqueness is maintained, but there are many advantages to letting the database do it instead or as well, e.g. 1) you may make a programming mistake but the database won't and 2) by examining the returned error codes you can let the database tell your external programs if a user is attempting to enter duplicate data to your database.
Comment