Mysql auto-increment ID start value modification method, mysql modification method

Source: Internet
Author: User

Mysql auto-increment ID start value modification method, mysql modification method
Many friends in mysql think that the field AUTO_INCREMENT Type Auto-increment ID value cannot be modified. In fact, this is an incorrect understanding. The following describes how to modify and set the starting value of mysql auto-increment ID.
The following describes how to set an auto-increment field:

Add the following when creating a table:

create table table1(id int auto_increment primary key,...)

After creating a table, add:

Alter table table1 add id int auto_increment primary key auto-increment field, must be set to primary key.

In many cases, it is expected that the table data IDs do not start from 1, such as qq, and IDs start from 10000.
The Code is as follows:

Add the following when creating a table:

CREATE TABLE `orders` (  `order_num` int(11) NOT NULL auto_increment,  `order_date` datetime NOT NULL,  `cust_id` int(11) 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 DELETE CASCADE ON UPDATE CASCADE) ENGINE=InnoDB AUTO_INCREMENT=10000 DEFAULT CHARSET=utf8;


After creating a table, add:

alter table users AUTO_INCREMENT=10000;

This statement can also be used to modify the id of an existing table. For example, if you want to return the id from 654321 to 123456 after deleting a large number of data

alter table users AUTO_INCREMENT=123456;


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.