-
Notifications
You must be signed in to change notification settings - Fork 923
/
Copy pathinit.sql
42 lines (32 loc) · 1.7 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
36
37
38
39
40
41
42
DROP TABLE IF EXISTS "transacoes";
DROP TABLE IF EXISTS "clientes";
CREATE UNLOGGED TABLE "clientes" (
id SERIAL NOT NULL,
nome TEXT NOT NULL,
limite INTEGER NOT NULL,
saldo INTEGER NOT NULL DEFAULT 0,
CONSTRAINT "clientes_pkey" PRIMARY KEY ("id")
);
CREATE UNLOGGED TABLE "transacoes" (
id SERIAL NOT NULL,
valor INTEGER NOT NULL,
tipo CHAR(1) NOT NULL,
descricao VARCHAR(10) NOT NULL,
realizada_em TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
client_id INTEGER NOT NULL,
CONSTRAINT "transacoes_pkey" PRIMARY KEY ("id")
);
CREATE INDEX idx_extrato ON transacoes (id DESC);
CREATE UNIQUE INDEX "clientes_id_key" ON "clientes"("id");
CREATE UNIQUE INDEX "transacoes_id_key" ON "transacoes"("id");
ALTER TABLE "transacoes" ADD CONSTRAINT "transacoes_clientesId_fkey" FOREIGN KEY ("client_id") REFERENCES "clientes"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
DO $$
BEGIN
INSERT INTO clientes (nome, limite)
VALUES
('o barato sai caro', 1000 * 100),
('zan corp ltda', 800 * 100),
('les cruders', 10000 * 100),
('padaria joia de cocaia', 100000 * 100),
('kid mais', 5000 * 100);
END; $$