PHPMyAdmin / MySql - Add ID field and autopopulate ID numbers

帅比萌擦擦* 提交于 2019-12-22 04:25:18

问题


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!!


回答1:


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.




回答2:


This works in SQL Server, maybe you can adapt it to MySQL:

declare @value int 
set @value=10000000

update your_table
set @value+=1,id=@value

It will update all your ID rows starting at 10000001 increasing by 1.

I hope, at least, gives you some ideas.




回答3:


All you need to do is set the column to be AUTO_INCREMENT and mysql will number the rows for you. Let's say you want your new column to be named 'id'.

alter table yourtable auto_increment = 10000001;
alter table yourtable add id int unsigned primary key auto_increment;

You can issue these commands in the sql panel of phpMyAdmin -- just leave off the semicolon at the end.



来源:https://stackoverflow.com/questions/7354114/phpmyadmin-mysql-add-id-field-and-autopopulate-id-numbers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!