已经建立的表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);
结果显示正确!