Mysql concurrent query and insertion of data

Source: Internet
Author: User
When inserting data, you must first calculate the maximum value of this order_number, then add 1 to the order_number, and then insert a new data. In the case of program concurrency, the expected result is that order_number should be auto-incrementing. How can this problem be solved?


When inserting data, you must first calculate the maximum value of this order_number.
Then, add 1 to order_number and insert a new data record.

When the program is concurrent

The expected result is that order_number should be auto-incrementing.
How should this problem be solved?

Reply content:


When inserting data, you must first calculate the maximum value of this order_number.
Then, add 1 to order_number and insert a new data record.

When the program is concurrent

The expected result is that order_number should be auto-incrementing.
How should this problem be solved?

The program will lock the table.
Db. Use a trigger! Perfect for your needs, but you need to consider the possibility of locking tables for large concurrency

MySQL comes with auto-increment field settings: AUTO_INCREMENT, but only one field can be auto-increment.

We usually set the primary key to auto-increment. If you set another auto-increment field, it will conflict. If you do not set the primary key to auto-increment, the primary key is always unique. For exampleGuid.

If you do not want to use the above method, you may need to addLock Table Mechanism. In the case of concurrency, if the table is not locked, it cannot be ensured that order_number is the unique auto-increment.

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.