The table you're working on already contains data; and the data isn't unique with regard to your new index.
Example:
col1 | col2 | col3
====================
foo | 1 | q
bar | 2 | w
bar | 3 | e
bar | 2 | r
In the above table, you couldn't create a unique index on (col1,col2)
, exactly because the data in it would be non-unique (multiple rows with (bar,2)
). The script can't know which of those "duplicate" rows is actually needed. There are three options available to it:
- create a UNIQUE index with duplicate rows (invalid, as it's not unique any more)
- delete the duplicate rows (unsafe, how can it know which rows are needed?)
- do nothing and throw an error (safest option, you are here)
What you can do to resolve this:
run a query to find duplicates - if you group the rows by those columns used by the index, some of the groups will have multiple rows. Those are your duplicates; you need to somehow eliminate their duplicity.