?
Solved

SELECT DISTINCT / Add auto_increment field

Posted on 2005-04-25
4
Medium Priority
?
1,103 Views
Last Modified: 2008-01-09
I have a table:

CREATE TABLE `asdf` (
  `asdf` int(10) unsigned NOT NULL auto_increment,
  `sdfg` varchar(30) NOT NULL default '',
  PRIMARY KEY  (`asdf`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;

Whereby I use CREATE TABLE tmp SELECT DISTINCT sdfg FROM asdf; to eliminate duplicates.
Then I drop the original table, asdf, and rename tmp to asdf.

So the duplicates are gone. My question is this:

How do I add a field to this table that is of type UNSIGNED INT, AUTO_INCREMENT, and is the PRIMARY KEY of the table, AND... that numbers each record in the table starting at 1?
0
Comment
Question by:DrDamnit
  • 2
4 Comments
 
LVL 32

Accepted Solution

by:
ldbkutty earned 1000 total points
ID: 13858787
CREATE TABLE tmp (`your_column_name` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY) SELECT DISTINCT sdfg FROM asdf ;
0
 
LVL 14

Assisted Solution

by:cracky
cracky earned 1000 total points
ID: 13858793
First you need to remove the auto_increment and drop the primary key:

ALTER TABLE `asdf` CHANGE `asdf` `asdf` INT( 10 ) UNSIGNED NOT NULL;
ALTER TABLE `asdf` DROP PRIMARY KEY;

Then create your new column:

ALTER TABLE `asdf` ADD `id` INT( 10 ) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST;

You can then safely drop your own column if you wish after checking that your new column has been properly numbered.

Is this what you are after?
0
 
LVL 32

Author Comment

by:DrDamnit
ID: 13861024
When I create the auto_increment column, will it number the existing records automatically?
0
 
LVL 32

Expert Comment

by:ldbkutty
ID: 13861028
Yes.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month14 days, 2 hours left to enroll

807 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