mysql中,已经建立的列添加 auto-increment

来源:互联网 发布:网络用语然是什么意思 编辑:程序博客网 时间:2024/05/17 04:04

已经建立的表test:

create table test

(

id int ,

age int not null

);

需求:给id列添加auto-increment

要给id添加auto-increment就得满足:id必须为主键,因为mysql表中,只有一个列能为auto-increment,而且这个列必须是主键。所以,先给id添加主键,然后再添加auto-increment,如下:

alter table test change id id int primary key;     添加主键

alter table test change id id int auto-increment;   添加auto-increment

接下来测试:

insert into test(age) values(22);

insert into test(age) values(33);

结果显示正确!

原创粉丝点击