PHPMyAdmin / MySql - Add ID field and autopopulate ID numbers

旧街凉风 提交于 2019-12-05 03:34:49

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.

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.

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.

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