MySQL changes the ID of an existing table to a self-increasing

Source: Internet
Author: User

such as table: User

Field: UserID name PW

The Useid of the user table is not self increasing. We changed the UserID to a self increase.

Execute the following statement to increase the UserID by 1 each time.

Statement: ALTER TABLE ' user ' change userid userid int is not NULL auto_increment primary key;

SQL of table:

CREATE TABLE ' user ' (

' username ' varchar not NULL,

' PW ' varchar not NULL,

' userid ' int (one) not NULL auto_increment,

PRIMARY KEY (' userid ')

) Engine=innodb DEFAULT Charset=utf8;

Extend Knowledge://Add field and set Primary key ALTER TABLE tabelname add new_field_id int (5) unsigned default 0 NOT NULL auto_increment, add prima Ry Key (new_field_id); The index ALTER TABLE tablename Add primary key (ID) of the primary keyword; Gazzo Primer www.2cto.com ALTER TABLE tablename change depno depno int (5) is not null; ALTER TABLE tablename ADD index index name (field name 1[, field Name 2 ...]); ALTER TABLE tablename ADD INDEX emp_name (name); Index ALTER TABLE tablename add unique emp_name2 (Cardnumber) with unique restriction criteria; Deletes an index ALTER TABLE tablename DROP index emp_name; Add field: ALTER TABLE table_name add field_name field_type; Delete field ALTER TABLE table_name DROP field_name; Rename column ALTER TABLE table_name change field_name1 field_name2 integer; Adjust field order ALTER TABLE ' users ' change ' user_password ' user_password ' varchar ' isn't NULL after user_name; Alter the type of the column ALTER TABLE table_name change field_name field_name bigint not null; ALTER TABLE infos change list tinyint not null default ' 0 '; Modify the original field name and type: www.2cto.com ALTER TABLE table_name Change old_field_name NEW_field_name Field_type; Rename Table 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) PRIMARY key not NULL, M Ail_filename varchar (), content varchar) 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 varchar () not null,content varchar (?), INDEX (RID), FOREIGN KEY (RID) REFERENCES Mail_model (ID) on DELETE CASCADE on Up DATE 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.