?
Solved

mysql alter table to make a column unique

Posted on 2011-09-14
3
Medium Priority
?
532 Views
Last Modified: 2012-05-12
Hi All,

How do I set a column in MySQL as unique?

There maybe duplicates, so they can be deleted.
0
Comment
Question by:detox1978
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 36536673
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
 
LVL 2

Author Comment

by:detox1978
ID: 36536722
thanks for the info.  created a new table, inserted the data, dropped the original and renamed the new.

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 36537269
Bingo!  Thanks for the points, ~Ray
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
In this article, I’ll talk about multi-threaded slave statistics printed in MySQL error log file.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question