Change the id of an existing table in mysql to auto-increment.

Source: Internet
Author: User

For example, in table: user field: userid name pw, The useid of the user table is not auto-incrementing. Change userid to auto-increment. Run the following statement to add userid auto-increment by 1 each time. Statement: alter table 'user' change userid int not null auto_increment primary key; table 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;


Extended knowledge: // ADD a field and set the primary key alter table tabelname ADD new_field_id int (5) unsigned default 0 not null auto_increment, ADD primary key (new_field_id ); // ADD the index alter table tablename ADD primary key (id); // ADD the index www.bkjia.com alter table tablename CHANGE depno int (5) not null; alter table tablename add index name (field name 1 [, field name 2…]); Alter table tablename add index emp_name (name); // ADD the index alter table tablename add unique emp_name2 (cardnumber) with UNIQUE restrictions; // delete an index alter table tablename drop index emp_name; // ADD a field: alter table table_name ADD field_name field_type; // Delete the field alter table table_name DROP field_name; // rename the alter table table_name CHANGE field_name1 field_name2 integer; // adjust the field order alter table 'users' CHANGE 'user _ password' varchar (20) not null after user_name; // alter table table_name CHANGE field_name bigint not null; alter table infos CHANGE list tinyint not null default '0'; // modify the original field name and type: www.bkjia.com alter table table_name CHANGE old_field_name new_field_name field_type; // rename the alter table table_name rename new_table_name; // cascade update and delete (red, Case Insensitive) 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;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.