• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 571
  • Last Modified:

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.
0
detox1978
Asked:
detox1978
  • 2
1 Solution
 
Ray PaseurCommented:
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.
0
 
detox1978Author Commented:
thanks for the info.  created a new table, inserted the data, dropped the original and renamed the new.

0
 
Ray PaseurCommented:
Bingo!  Thanks for the points, ~Ray
0
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.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now