• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 563
  • 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

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

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