MySQL tips-last_insert_id

Source: Internet
Author: User

In fact, there is a difference between the two. last_insert_id () can return the ID of the bigint value. However, mysql_insert_id returns Int. If you
The ID of is unsigned Int or bigint. The returned result may be incorrect. Instead of last_insert_id.

Some friends still return 0 and do not know what is going on. In fact, last_insert_id () returns the auto_increment ID.
If the table structure is not set, auto_increment cannot be returned.

Some still return 0. Check whether the insert delay function is used. In this case, no immediate id value is returned.

Many people prefer to use select max (ID)... to replace this last_insert_id. In fact, select max (ID) is non-thread-safe and likely,
When new data is inserted in other threads, you cannot find the id you inserted last time. The last_insert_id corresponds to a MySQL connect, that is
It corresponds to your current thread and will not be disturbed by other threads. If some strange errors occur in your database, for example, the information of Data A is to be updated,
Result B data is updated, which is sometimes correct or incorrect. When there are many people, the data is very incorrect. Check whether select max (ID) is used)

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.