-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.sql
38 lines (34 loc) · 985 Bytes
/
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
CREATE TABLE clientes (
id SERIAL PRIMARY KEY,
nome VARCHAR(50) NOT NULL,
limite INTEGER NOT NULL
);
CREATE TABLE transacoes (
id SERIAL PRIMARY KEY,
cliente_id INTEGER NOT NULL,
valor INTEGER NOT NULL,
tipo CHAR(1) NOT NULL,
descricao VARCHAR(10) NOT NULL,
realizada_em TIMESTAMP NOT NULL DEFAULT NOW(),
CONSTRAINT fk_clientes_transacoes_id
FOREIGN KEY (cliente_id) REFERENCES clientes(id)
);
CREATE TABLE saldos (
id SERIAL PRIMARY KEY,
cliente_id INTEGER NOT NULL,
valor INTEGER NOT NULL,
CONSTRAINT fk_clientes_saldos_id
FOREIGN KEY (cliente_id) REFERENCES clientes(id)
);
DO $$
BEGIN
INSERT INTO clientes (nome, limite)
VALUES ('Cleiton Rasta', 1000 * 100),
('Alonzo Church', 800 * 100),
('Marcos Valle', 10000 * 100),
('Vinicius de Moraes', 100000 * 100),
('Jose Raul Capablanca', 5000 * 100);
INSERT INTO saldos (cliente_id, valor)
SELECT id, 0 FROM clientes;
END;
$$;