How to prevent self-growth of MySQL self-growth fields when inserting data fails

Source: Internet
Author: User

Problem description: When MySQL set the self-growing field (note: A table can only set a self-growth field, may not be the primary key, but must be the key), if the insertion of data fails, then the self-growth field will still occupy the self-growth value, the data will be successfully inserted into the fault.

Cause: The self-increment is cached in the memory dictionary, and is allocated by first reserving and then inserting. Therefore, insert failure does not roll back the memory dictionary.

Workaround: In PHP program can add a piece of code, when inserting data fails, execute SQL statement reset Auto_increment value:

$sql = ALTER TABLE ' Tbname ' auto_increment=n;

Use function Mysqli_query ($sql) to execute SQL statements

How to prevent self-growth of MySQL self-growth fields when inserting data fails

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.