I currently have a table as follows:
CREATE TABLE Account (
ID int NOT NULL,
Balance int,
CHECK (Balance>=0)
);
I also have some application pseudo-code as follows:
function updateBalance(id Int, howMuch Int) {
check howMuch is non zero (can be negative or positive)
read balance for id as bal
if bal + howMuch >= 0 update else throw error
}
I feel that reading balance and checking >= 0 is unnecessary and time consuming, since the database I plan to use supports check constraints (most likely PostgreSQL or H2). Certain ones (such as MySQL) don't support check constraints and will silently ignore them in the create statement).
Should I rely on the database to ensure non-negative balance or do it also within my app?