Home  >  Article  >  Database  >  MySQL为已经存在的表的id改为自增

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

WBOY
WBOYOriginal
2016-06-07 17:36:021248browse

如表: 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;

linux

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn