Solved

mysql alter table to make a column unique

Posted on 2011-09-14
3
490 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
  • 2
3 Comments
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 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 108

Expert Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

920 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now