mysql - Add auto_increment unique id to my sql? -
i have created table in mysql , want add column i.e, 'myid' table i.e, 'mytable'.
there primary key defined.
i use follwing query add auto increment:
alter table mytable add myid int unsigned not null auto_increment unique;
but want auto increment should start 30 rather 1.
what modification query required? other way add this?
secondly if use following procedure,
first run query: alter table mytable auto_increment = 30;
then run alter table mytable add myid int unsigned not null auto_increment unique;
will okay? yes or no?
if table empty or small first add auto_increment column , set it's start value this:
make myid
auto_increment
column:
alter table `mytable` add `myid` int unsigned not null auto_increment unique;
set start value 30
alter table mytable
auto_increment = 30;
caveat
if have lots of records in mytable
, use
alter table mytable auto_increment = 30;
mysql server create new table, copy data old table new, delete old , rename new 1 though there no structural change table. server response show rows have been affected
this:
mysql> alter table mytable auto_increment = 30; query ok, 609481 rows affected (1 min 4.55 sec) records: 609481 duplicates: 0 warnings: 0
there potential issues may arise table copy e.g. insufficient amount of free disk space second copy of data.
trick can use insert dummy row , explicitly set auto_increment column 29, delete dummy row. next row inserted start @ 30 , values go upwards there.