Auto-increment ID

Source: Internet
Author: User
About the auto-increment ID, each time a piece of data is deleted, and then 1, 2, 3, 4 cannot be connected... For example, if you delete the third data record, it will become 124.


Reply to discussion (solution)

Ignore him
A primary key (generally id) is a unique identifier that does not have other meanings.

The IDs of the auto-incrementing primary keys in Mysql database tables are messy and need to be rearranged. You can do this

Principle: delete the original auto-increment ID and create a new auto-increment ID.

1. delete the original primary key:

Alter table 'Table _ name' DROP 'id ';

2. add the new primary key field:

Alter table 'Table _ name' add'id' MEDIUMINT (8) not null first;

3. set the new primary key:

Alter table 'Table _ name' modify column 'id' MEDIUMINT (8) not null AUTO_INCREMENT, add primary key (id)

This is the case.

InnoDB records the maximum ID of the auto-increment primary key in the memory. Therefore, if your current id is 4, the auto-increment record is 4. even if you delete data, the maximum value is still this.
However, if you restart mysql, you will find that if it is inserted, it is connected in order, that is, what you said.

A table ID auto-incrementing primary key. after four records are inserted, 2, 3, and 4 records are deleted, Mysql is restarted, and another record is inserted, is the ID of this record 2 or 5?

Also, you can look at this:
Http://flandycheng.blog.51cto.com/855176/280224

This phenomenon is normal.

In fact, I just want to get the first 10 pieces of data. how can this problem be solved ??? How to write without auto-increment

Select... limit 10

The primary key is only a unique identifier. you don't have to worry about the break number. if you want to query 10 items, add limit 10 at the end of your query statement, you cannot use id <10 as the query condition, so the number of queried results will only be <= 10

It turned out to be something as good as limit. Potholes

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.