Best way to prevent a value from going negative in mysql

前端 未结 3 1338
鱼传尺愫
鱼传尺愫 2021-01-28 19:46

We have a table that maintains account balances by recording transactions in that table. i.e. the most recent row is the account balance.

When recording a withdrawal, we

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-28 19:58

    I have to agree with the trigger idea. If this is an accounting rule that must be followed no matter how the data is entered, it needs to be in a trigger.

    If this is true only for this particular case, then do it in the SQL code. I don't know mySQL but in SQL server Iwould put the check in an if statment and fail the transaction if the IF condition is met. The critical thing here isn't to ignore the data but to actively fail the transaction,otherwise the user thinks the data has been entered when it has not met the criteria to be entered. I would never write any cdode for a finacial system that isn't encapsulated in transactions which would rollback the entire transaction and send an error to the user if the business rules are not met. Business rules are extremely critical to financial applications (and should usually be in triggers so that they are never missed no matter how the data is put into the system) and data integrity can be a real problem if all the steps do not succeed and you are not in a transaction and rolling back when there is a problem.

提交回复
热议问题