MySQL self-increment ID Start Value modification method

Source: Internet
Author: User

Many friends in MySQL think that the field auto_increment type self-increment ID value can not be modified, in fact, this understanding is wrong, the following describes the MySQL self-increment ID starting value modification and setting method.
The usual way to set the self-increment field:

Add when creating a table:

CREATE TABLE table1 (ID int auto_increment PRIMARY key,...)

After creating the table, add:

ALTER TABLE table1 Add ID int auto_increment primary key self-increment field, be sure to set to primary key.

Many times you want the ID of the data in the table not starting from 1, like QQ, ID starting from 10000
The code is as follows:

Add when creating a table:

CREATE TABLE ' orders ' (  ' order_num ' int (one) not null auto_increment,  ' order_date ' datetime NOT NULL,  ' Cust_ ID ' int (one) not NULL,  PRIMARY key  (' Order_num '),  key ' fk_orders_customers ' (' cust_id '),  CONSTRAINT ' FK_Orders_Customers ' FOREIGN KEY (' cust_id ') REFERENCES ' customers ' (' cust_id ') on the DELETE CASCADE on UPDATE CASCADE) Engin E=innodb auto_increment=10000 DEFAULT Charset=utf8;


After creating the table, add:

ALTER TABLE users auto_increment=10000;

And the statement also applies to modify the ID of an existing table, such as large batches of deleted data, the ID from 654321 to 123456 start

ALTER TABLE users auto_increment=123456;


MySQL self-increment ID Start Value modification method

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.