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.