Home > database >  A pen questions about the database
A pen questions about the database

Time:10-04

Suppose you have A, B two people at the same time with the same account place an order for amount to 50 and 60, respectively, but the account remaining sum is only 100, if you don't do processing, their order will be successful, ask: how to do A simple processing,

CodePudding user response:

Row-level locks, do a lock

CodePudding user response:

 
- there is only one success

- the session 1
Update t set num=num - 50 where num & gt;=50;
- session 2
Update t set num=num - 60 where num & gt;=60;

CodePudding user response:

refer to the second floor wmxcn2000 response:
 
- there is only one success

- the session 1
Update t set num=num - 50 where num & gt;=50;
- session 2
Update t set num=num - 60 where num & gt;=60;

CodePudding user response:

reference cnceohjm reply: 3/f
Quote: refer to the second floor wmxcn2000 response:

 
- there is only one success

- the session 1
Update t set num=num - 50 where num & gt;=50;
- session 2
Update t set num=num - 60 where num & gt;=60;




Will this field combined with constraints, can not be less than zero, then the
Update t set num=num - 50 where id=user_id;
Update t set num=num - 50 where id=user_id;
The two corresponding SQL session will be a success, the other one will violate the constraint error, in violation of the constraints of the program can return the balance has been insufficient, at the same time users can also view the latest account balance again, if the fruit to SQL, the first does not consider user ID, will deduct all the user's money, on the other hand the program to determine what did UPDATE to UPDATE, namely because there is no error after you execute a statement, button all don't know whether there is no money, have to go to the inspection statement execution as a result, or we will be successful,

CodePudding user response:

refer to the second floor wmxcn2000 response:
 
- there is only one success

- the session 1
Update t set num=num - 50 where num & gt;=50;
- session 2
Update t set num=num - 60 where num & gt;=60;


Two will succeed, assume that the database as long as a data, a modified modified the article 1 session data, a modified the article 0 session data,

CodePudding user response:

refer to the second floor wmxcn2000 response:
 
- there is only one success

- the session 1
Update t set num=num - 50 where num & gt;=50;
- session 2
Update t set num=num - 60 where num & gt;=60;



This method is very tight and very simple
  • Related