Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPMyAdmin / MySql - Add ID field and autopopulate ID numbers

I have an extremely large database table - nearly 20 million records.

The records do not have a unique ID number. So, I've inserted the new field.

Now, I would like to populate it with ID numbers, increasing by 1, starting with the first ID number being 10,000,001.

FYI - I am using WAMP on a local machine and I've dialed all my max times upto 5000 seconds and dialed up several other variables in php.ini and mysql.ini in order to do the upload in the first place (which took more than 10 hours!!).

In the past, or with other DB's, I might have exported the data into excel and then whipped up some text to paste back into phpmyadmin to UPDATE the records. This is fine when working with 5K records, or even 100K records, but this seems unmanagable with 20 million records.

Thanks in advance!!

like image 919
Kevin Avatar asked Sep 08 '11 20:09

Kevin


1 Answers

Just run these two queries one after the other in the SQL tab:

ALTER TABLE mytable AUTO_INCREMENT=10000001;

ALTER TABLE mytable ADD `id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST;

MySQL will then create the id field and fill it in sequentially starting at 10000001.

like image 140
webbiedave Avatar answered Oct 24 '22 21:10

webbiedave