Table of Contents
如表: user
字段: userid   name   pw
本来user表的useid不是自增的。  我们把userid改为自增。
执行以下语句就可以把userid自增, 每次加1。
语句:alter table `user` change userid userid int not null auto_increment primary key; 
Home Database Mysql Tutorial mysql替已经存在的表的id改为自增

mysql替已经存在的表的id改为自增

Jun 07, 2016 pm 04:24 PM
mysql self-increasing

mysql为已经存在的表的id改为自增 如表: user 字段: userid name pw 本来user表的useid不是自增的。 我们把userid改为自增。 执行以下语句就可以把userid自增, 每次加1。 语句: alter table `user` change userid userid int not null auto_increment pri

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

如表: 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); //加索引   www.2cto.com   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'; //修改原字段名称及类型:   www.2cto.com   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;
Statement of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP's big data structure processing skills PHP's big data structure processing skills May 08, 2024 am 10:24 AM

PHP's big data structure processing skills

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

How to optimize MySQL query performance in PHP?

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

How to use MySQL backup and restore in PHP?

How to insert data into a MySQL table using PHP? How to insert data into a MySQL table using PHP? Jun 02, 2024 pm 02:26 PM

How to insert data into a MySQL table using PHP?

What are the application scenarios of Java enumeration types in databases? What are the application scenarios of Java enumeration types in databases? May 05, 2024 am 09:06 AM

What are the application scenarios of Java enumeration types in databases?

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

How to fix mysql_native_password not loaded errors on MySQL 8.4

How to use MySQL stored procedures in PHP? How to use MySQL stored procedures in PHP? Jun 02, 2024 pm 02:13 PM

How to use MySQL stored procedures in PHP?

Performance optimization strategies for PHP array paging Performance optimization strategies for PHP array paging May 02, 2024 am 09:27 AM

Performance optimization strategies for PHP array paging

See all articles