forked from zanfranceschi/rinha-de-backend-2024-q1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.sql
37 lines (28 loc) · 1.23 KB
/
init.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
drop table if exists transacao;
drop table if exists cliente;
create table cliente (
id integer constraint pk_cliente primary key,
limite bigint not null,
saldo bigint not null,
constraint chk_saldo CHECK(saldo >= (-limite))
);
create table transacao (
id uuid constraint pk_transacao primary key,
id_cliente integer references cliente(id),
valor integer not null,
tipo char not null,
descricao character varying (10) not null,
realizada_em timestamp not null default current_timestamp
);
create index CONCURRENTLY idx_transacao_id_cliente ON transacao (id_cliente);
create index CONCURRENTLY idx_transacao_realizada_em ON transacao (realizada_em DESC);
insert into cliente(id, limite, saldo) values(1, 100000, 0);
insert into cliente(id, limite, saldo) values(2, 80000, 0);
insert into cliente(id, limite, saldo) values(3, 1000000, 0);
insert into cliente(id, limite, saldo) values(4, 10000000, 0);
insert into cliente(id, limite, saldo) values(5, 500000, 0);
select c.saldo, c.limite, r.credito - s.debito from
cliente c,
(select sum(valor) as credito from transacao where tipo = 'c' and id_cliente = 1) r,
(select sum(valor) as debito from transacao where tipo = 'd' and id_cliente = 1) s
where c.id = 1;