MySQL Incremental ID Start value change method

Source: Internet
Author: User

Many friends in MySQL feel the field auto_increment increment ID value it cannot be changed, in fact this realization is wrong, here MySQL Increment ID start value change and set.
The usual way to set an auto-increment field:

When you join the grid:

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 it to primary key.

Many times you want the ID of the data in the table not to start with 1, like QQ. ID starts 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;

The statement also applies to changing the ID of an existing table. For example, after you delete data in large batches, you want to return the ID from 654321 to 123456.

ALTER TABLE users auto_increment=123456;


Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.

MySQL Incremental ID Start value change 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.