Automatic MySQL Growth

Source: Internet
Author: User

Use the SQL function last_insert_id () in MySQL ()
Last_insert_id ():

1.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 of the latest statement that affects the auto_increment column generated by the client. This value cannot be affected by other clients, even if they generate their own auto_increment values. This behavior ensures that you can retrieve your ID without worrying about the activity of other clients and does not need to be locked or processed. You only need to use last_insert_id in the same connection to obtain the last inserted or updated ID.

Note for last_insert_id of MySQL: first, the connection object used for query and insertion must be the same; otherwise, the return value is unpredictable. Second, last_insert_id is irrelevant to the table. If you insert data to table A and then insert data to table B, last_insert_id returns the id value of Table B. Third, if you use an insert statement to insert multiple rows, last_insert_id () Only returns the value generated when inserting the first row of data. Fourth, if you use insert ignore and the record is ignored, the auto_increment counter will not increment, and last_insert_id () will return 0, which reflects that no records are inserted. Based on these four principles, the inserted values taken from the Growth Value for highly concurrent Website access are mainly related to the first rule and the second rule. That is, to ensure the correctness of last_insert_id, the same connection must be established, and the last_insert_id must be executed in the insert statement. Therefore, if there is a problem with the public connection of the data card cache pool, multi-threaded operations may also cause problems when other insert operations are performed after the insert operation.

2.If you 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.