SQLize Online / PHPize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
create table users ( id serial primary key, name varchar, balance int check (balance>=0) ); create table transactions ( id serial primary key, user_id int, amount int, created_at timestamp, description varchar, constraint user_id_fk foreign key (user_id) references users(id) ); create or replace function transaction(_user_id int, _amount int, _description varchar) RETURNS void LANGUAGE plpgsql AS $fnc$ begin insert into transactions ( user_id, amount, created_at, description ) values ( _user_id, _amount, now(), _description ); update users set balance = balance + _amount where id = _user_id; end; $fnc$; insert into users values (1, 'U', 95); select from transaction(1, 10, 'test'); select * from users;
Stuck with a problem? Got Error? Ask ChatGPT!
Copy Clear