您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

MySQL为已经存在的表的id改为自增

2024/8/16 5:19:56发布112次查看
如表: user字段: userid name pw本来user表的useid不是自增的。 我们把userid改为自增。执行以下语句就可以把userid自增,
如表: user
字段: userid  name  pw
本来user表的useid不是自增的。  我们把userid改为自增。
执行以下语句就可以把userid自增, 每次加1。
语句:alter table `user` change userid userid int not null auto_increment primary key;
表的sql:
create table `user` (
  `username` varchar(20) not null,
  `pw` varchar(20) not null,
  `userid` int(11) not null auto_increment,
  primary key (`userid`)
) engine=innodb default charset=utf8;
扩展知识:
//添加字段并设置主键
alter table tabelname add new_field_id int(5) unsigned default 0 not null auto_increment ,add primary key (new_field_id);
//加主关键字的索引
alter table tablename add primary key(id);
//加索引 
alter table tablename change depno depno int(5) not null;
alter table tablename add index 索引名 (字段名1[,,字段名2 …]);
alter table tablename add index emp_name (name);
//加唯一限制条件的索引
alter table tablename add unique emp_name2(cardnumber);
//删除某个索引
alter table tablename drop index emp_name;
//增加字段:
alter table table_name add field_name field_type;
//删除字段
alter table table_name drop field_name;
//重命名列
alter table table_name change field_name1 field_name2 integer;
//调整字段顺序
alter table `users` change `user_password` `user_password` varchar( 20 ) not null after user_name;
//改变列的类型
alter table table_name change field_name field_name bigint not null;
alter table infos change list list tinyint not null default '0';
//修改原字段名称及类型:
alter table table_name change old_field_name new_field_name field_type;
//重命名表
alter table table_name rename new_table_name;
//级联更新 和 删除(红色部分,不区分大小写 )
drop table if exists `mail_model`;create table mail_model(id varchar(50) primary key not null ,mail_filename varchar(200),content varchar(2000))engine=innodb default charset=gbk;
drop table if exists `mail_model_extend`;create table mail_model_extend(id int(6) auto_increment not null primary key,rid varchar(50) not null,content varchar(2000),index (rid),foreign key (rid) references mail_model(id) on delete cascade on update cascade)engine=innodb default charset=gbk;
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product