I'm new with PostgreSQL, and I already have my first problem..
I wrote some code to understand how transactions work, following the manual step by step.
To make it short, I've created 2 tables, user and movements: in the first one there are the name, email and credit columns, in the second the columns from, to, import.
So, I was trying this way:
BEGIN;
INSERT INTO movements (from, to, import) VALUES ('mary', 'steve', 600);
UPDATE users SET credit = credit - 600 WHERE name = 'mary';
UPDATE users SET credit = credit + 600 WHERE name = 'steve';
--here comes the problem!
IF (SELECT credit FROM users WHERE name = 'mary') < 0 THEN
ROLLBACK;
END IF
COMMIT;
I always get the error:
ERROR: syntax error at or near "IF"
Where am I mistaken?
P.S.: Don't focus on the example functionality, it's just a trial for me to understand the transactions.. and now, the IF clause...