forked from zanfranceschi/rinha-de-backend-2024-q1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.sql
36 lines (29 loc) · 847 Bytes
/
script.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
36
DROP TABLE IF EXISTS transacoes;
DROP TABLE IF EXISTS clientes;
CREATE TABLE clientes (
id BIGSERIAL PRIMARY KEY,
limite NUMERIC NOT NULL,
saldo NUMERIC NOT NULL DEFAULT 0,
CONSTRAINT saldo_nao_negativo check (saldo >= (limite * -1))--o que me permite usar ON CONFLICT
);
CREATE TABLE transacoes (
id BIGSERIAL PRIMARY KEY,
cliente_id BIGINT NOT NULL,
valor NUMERIC NOT NULL,
tipo BOOLEAN NOT NULL,--1 se D, 0 se C
descricao VARCHAR(10) NOT NULL,
realizada_em TIMESTAMP NOT NULL DEFAULT NOW(),
CONSTRAINT fk_clientes_transacoes_id
FOREIGN KEY (cliente_id) REFERENCES clientes(id)
);
--talvez criar uma view materialized para as 10 ultimas transações...
DO $$
BEGIN
INSERT INTO clientes (id, limite)
VALUES
(1, 1000 * 100),
(2, 800 * 100),
(3, 10000 * 100),
(4, 100000 * 100),
(5, 5000 * 100);
END; $$