mysql 的表已经存在,但是我现在想添加一列 自动增长列

表里面有十几条记录了 表名 temp 列名id 要求是mysql的写法 添加数据进去id列自动增长

第1个回答  2013-05-04
alter table temp change id id int primary key auto_increment; ---------------------------id不是主键的情况下,

如果id已经是主键,alter table temp change id id int auto_increment;
第2个回答  2018-10-31
alter table temp
add id int primary key auto_increment first;
新建的字段,肯定是没有设置过主键的。add语句就可以解决。既然是id,所以建议放在第一列,因此最后可加first。
第3个回答  推荐于2016-08-29
办法是,创建一个带有id的新表,id是自动增长的,temp1,其余字段和temp表相同,
将temp表数据插入到temp1.

insert into temp1 select * from temp追问

假设我原来表有 字段 name age sex 这是三个字段 三个都是 字符型 那我创建新表的代码是怎么样的

追答

create table temp1
(
id int(10) unsigned NOT NULL AUTO_INCREMENT,
name varchar(20) not null,
sex char(2) not null,
age int not null,
primary key(id)
);

本回答被提问者采纳

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网