-
Notifications
You must be signed in to change notification settings - Fork 925
/
init.sql
47 lines (42 loc) · 1.24 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
--- TABLES
CREATE TABLE customers (
id SERIAL PRIMARY KEY,
name VARCHAR(50) NOT NULL,
account_limit INTEGER NOT NULL
);
CREATE TABLE transactions (
id SERIAL PRIMARY KEY,
customer_id INTEGER NOT NULL,
amount INTEGER NOT NULL,
type CHAR(1) NOT NULL,
description VARCHAR(10) NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
CONSTRAINT fk_customers_transactions_id
FOREIGN KEY (customer_id) REFERENCES customers(id)
);
CREATE TABLE balances (
id SERIAL PRIMARY KEY,
customer_id INTEGER NOT NULL,
balance INTEGER NOT NULL,
CONSTRAINT fk_customers_balances_id
FOREIGN KEY (customer_id) REFERENCES customers(id)
);
--- INDEX
CREATE INDEX idx_customers_id ON customers (id);
CREATE INDEX idx_transactions_customer_id ON transactions (customer_id);
CREATE INDEX idx_balances_customer_id ON balances (customer_id);
CREATE INDEX idx_transactions_customer_id_created_at ON transactions (customer_id, created_at DESC);
--- SEED
DO $$
BEGIN
INSERT INTO customers (name, account_limit)
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 balances (customer_id, balance)
SELECT id, 0 FROM customers;
END;
$$;