-
Notifications
You must be signed in to change notification settings - Fork 923
/
Copy pathinit.sql
55 lines (44 loc) · 1.5 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
43
44
45
46
47
48
49
50
51
52
53
54
55
-- CreateEnum
CREATE TYPE "TipoTransacao" AS ENUM ('c', 'd');
-- CreateTable
CREATE TABLE "clientes" (
"id_cliente" SERIAL NOT NULL,
"nome" VARCHAR(256) NOT NULL,
"limite" INTEGER NOT NULL,
CONSTRAINT "clientes_pkey" PRIMARY KEY ("id_cliente")
);
-- CreateTable
CREATE TABLE "transacoes" (
"id" SERIAL NOT NULL,
"id_cliente" INTEGER NOT NULL,
"valor" INTEGER NOT NULL,
"tipo" "TipoTransacao" NOT NULL,
"descricao" VARCHAR(10) NOT NULL,
"realizada_em" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "transacoes_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "saldos" (
"id_cliente" INTEGER NOT NULL,
"valor" INTEGER NOT NULL,
CONSTRAINT "saldos_pkey" PRIMARY KEY ("id_cliente")
);
-- CreateIndex
CREATE UNIQUE INDEX "saldos_id_cliente_key" ON "saldos"("id_cliente");
-- AddForeignKey
ALTER TABLE "transacoes" ADD CONSTRAINT "transacoes_id_cliente_fkey" FOREIGN KEY ("id_cliente") REFERENCES "clientes"("id_cliente") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "saldos" ADD CONSTRAINT "saldos_id_cliente_fkey" FOREIGN KEY ("id_cliente") REFERENCES "clientes"("id_cliente") 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);
INSERT INTO saldos (id_cliente, valor)
SELECT id_cliente, 0 FROM clientes;
END;
$$;