forked from zanfranceschi/rinha-de-backend-2024-q1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit_tables.sql
38 lines (32 loc) · 1.03 KB
/
init_tables.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
37
38
DROP TABLE IF EXISTS clientes;
DROP TABLE IF EXISTS transacoes;
DROP TYPE IF EXISTS "tipo_transacao";
CREATE TYPE "tipo_transacao" AS ENUM ('c', 'd');
CREATE TABLE IF NOT EXISTS clientes (
"id" SERIAL NOT NULL,
"saldo" INTEGER NOT NULL CHECK (saldo >= -"limite"),
"limite" INTEGER NOT NULL,
CONSTRAINT "clientes_pkey" PRIMARY KEY ("id")
);
CREATE INDEX idx_cliente_client_id
ON clientes ("id");
CREATE TABLE IF NOT EXISTS transacoes (
"id" SERIAL NOT NULL,
"valor" INTEGER NOT NULL,
"id_cliente" INTEGER NOT NULL,
"tipo" "tipo_transacao" NOT NULL,
"descricao" VARCHAR(10) NOT NULL,
"realizada_em" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT transacoes_pkey PRIMARY KEY ("id"),
CONSTRAINT fk_clientes_transacoes_id FOREIGN KEY ("id_cliente") REFERENCES clientes("id")
);
CREATE INDEX idx_transacoes_client_id
ON transacoes ("id_cliente");
INSERT INTO
clientes (saldo, limite)
VALUES
(0, 1000 * 100),
(0, 800 * 100),
(0, 10000 * 100),
(0, 100000 * 100),
(0, 5000 * 100);