Mysql 設定主鍵自動增長起始值

2022-02-06 18:24:09 字數 2984 閱讀 2618

比較鬱悶昨天在家使用『alter table `tablename` auto_increment=10000;』怎麼也不起效,但是今天下班時間公司一同事嘗試了一下就可以了。搞不明白自己當時是怎麼操作的,導致最終不起效。

實現目標:mysql下將自增主鍵的值,從10000開始,即實現自增主鍵的種子為10000。

建立自增主鍵之後,使用alter table `tablename` auto_increment=10000實現修改表起始值。

drop

table

ifexists

`trace_test`;

create

table

`trace_test` (

`id`

int(11) not

null

auto_increment,

`name`

varchar(255) default

null,

primary

key(`id`)

) engine

=innodb default charset=

utf8 ;

alter

table `trace_test` auto_increment=

10000

;insert

into `trace_test`(`name`)values('

name2');

select

*from `trace_test`;

result:

id     name

10000 name2

drop

table

ifexists

`trace_test`;

create

table

`trace_test` (

`id`

int(11) not

null

auto_increment,

`name`

varchar(255) default

null,

primary

key(`id`)

) engine

=innodb auto_increment 10000

default charset=

utf8 ;

insert

into `trace_test`(`name`)values('

name2');

select

*from `trace_test`;

result:

id     name

10000 name2

drop

table

ifexists

`trace_test`;

create

table

`trace_test` (

`id`

int(11) not

null

auto_increment,

`name`

varchar(255) default

null,

primary

key(`id`)

) engine

=innodb default charset=

utf8 ;

insert

into `trace_test`(`name`)values('

name1');

select

*from

`trace_test`;

truncate

table

`trace_test`;

alter

table `trace_test` auto_increment=

10000

;insert

into `trace_test`(`name`)values('

name2');

select

*from `trace_test`;

result1:

id     name

10000 name

result2:

id     name

10000 name2

drop

table

ifexists

trace_test;

create

table

trace_test (

id int(20) not

null

auto_increment,

name

varchar(255) default

null,

primary

key(id)

) engine

=innodb default charset=

utf8 ;

insert

into trace_test(name)values('

name1');

select

*from

trace_test;

delete

from

`trace_test`;

alter

table trace_test auto_increment=

10000

;insert

into trace_test(name)values('

name2');

select

*from trace_test;

result1:

id     name

10000 name

result2:

id     name

10000 name2

MySql 主鍵自動增長

mysql alter table user modify id integer auto increment query ok,1 row affected 0.28 sec records 1 duplicates 0 warnings 0 給主鍵增加乙個自增的功能 mysql create d...

MySql 主鍵自動增長

建立資料庫,建立表。mysql create database ssh2 query ok,1 row affected 0.04 sec mysql use ssh2 database changed mysql create table user id integer primary key,f...

Mysql主鍵自動增長

mysql create database ssh2 query ok,1 row affected 0.04 sec mysql usessh2 database changed mysql create table user id integer primary key,firstname va...