mysql alter table to make a column unique

Hi All,

How do I set a column in MySQL as unique?

There maybe duplicates, so they can be deleted.
Who is Participating?
Ray PaseurConnect With a Mentor Commented:
I have never tried to alter a table and add a UNIQUE index to a column that has duplicates.  That would probably fail (I hope).  I think you might want to create a new table with UNIQUE on the column, then INSERT into the new table with a SELECT from the old table, processing the rows one at a time.

MySQL will throw error number #1062 if you attempt to insert duplicate data into a UNIQUE column.  You can trap this error and ignore it if you want to discard the duplicate values.
detox1978Author Commented:
thanks for the info.  created a new table, inserted the data, dropped the original and renamed the new.

Ray PaseurCommented:
Bingo!  Thanks for the points, ~Ray
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.