bankserver_rust/migrations/000000_baseline.sql

28 lines
965 B
SQL

create table users(
id uuid primary key,
name varchar(32) not null unique,
password varchar(128)
);
create table accounts(
id uuid primary key,
"user" uuid not null references users(id),
name varchar(32) not null,
balance bigint not null default 0 constraint positive_balance check (balance >= 0),
unique ("user", name)
);
create table transactions(
id uuid primary key,
"from" uuid not null references accounts(id),
"to" uuid not null references accounts(id),
amount bigint not null constraint positive_amount check (amount > 0),
timestamp timestamp without time zone not null default now(),
message text
);
create index transactions_from on transactions ("from");
create index transactions_to on transactions ("to");
create view transactions_with_user as select t.*, (select user from accounts where id = t.from) as from_user, (select user from accounts where id = t.to) as to_user from transactions t;