Analysis of last_insert_id () _ MySQL in MySQL

Source: Internet
Author: User
Most of the time, when inserting a piece of data, we need to know the number of its primary keys. it will automatically return the first insert at the position of the auto_increment column. Note that each time a generated id is inserted into a piece of data, we need to know its primary key, it will automatically return the value of the first occurrence of the last insert and the position of the auto_increment column.

Note that the generated id is saved on the server after each connection, this means that the value returned by the function to a given client is the first auto_increment value generated by the client after the latest statement that affects the auto_increment column.

This value will not be affected by other clients. even if they generate their own auto_increment values, this behavior ensures that we can retrieve our own IDs without worrying about the activities of other clients, no locks or other processing is required. that is to say, last_insert_id is for connection.

If we use a single insert statement to insert multiple rows, last_insert_id () only returns the value of the first row to be inserted, this makes it easy to copy the same insert statement on other servers.

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.