Friday, November 30, 2007

Removing duplicate entries from a MYSQL table

This is a very easy problem to solve, but when you run into it you just get all be-fuddled and stuff.
Lets say you have a table userinfo which looks like:
userid int
email varchar(50)
first_name varchar(50)
last_name varchar(50)
address varchar(500)
city varchar(50)
state varchar(2)
zip varchar(10)

The best design is to index on userid. If its a web application, then after the initial registration send over a cookie. Upon login, if you can't find a cookie, only then will you need to select on email, which would be their username as well.

OK, so that was a slight digression. But if you didn't index on userid, then your code may have created multiple userid entries, like so:
userid username
100 sandeep
101 loki
102 mari
103 sandeep
104 raja
105 sandeep

So how do you get rid of these multiple entries?
1. Create another table with the unique entries from this table. You're "grouping by" the duplicate entries.
mysql> CREATE TABLE nuserinfo SELECT * FROM userinfo GROUP BY username;

2. Delete the old table:
mysql> DROP TABLE userinfo;

3. Rename the new table:
mysql> RENAME TABLE nuserinfo TO userinfo;

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home