-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_tables.sql
34 lines (30 loc) · 935 Bytes
/
create_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
CREATE TABLE stock (
id SERIAL PRIMARY KEY,
symbol TEXT NOT NULL,
name TEXT NOT NULL,
exchange TEXT NOT NULL,
is_etf BOOLEAN NOT NULL
);
CREATE TABLE etf_holding (
etf_id INTEGER NOT NULL,
holding_id INTEGER NOT NULL,
dt DATE NOT NULL,
shares NUMERIC,
weight NUMERIC,
PRIMARY KEY (etf_id, holding_id, dt),
CONSTRAINT fk_etf FOREIGN KEY (etf_id) REFERENCES stock (id),
CONSTRAINT fk_holding FOREIGN KEY (holding_id) REFERENCES stock (id)
);
CREATE TABLE stock_price (
stock_id INTEGER NOT NULL,
dt TIMESTAMP WITHOUT TIME ZONE NOT NULL,
open NUMERIC NOT NULL,
high NUMERIC NOT NULL,
low NUMERIC NOT NULL,
close NUMERIC NOT NULL,
volume NUMERIC NOT NULL,
PRIMARY KEY (stock_id, dt),
CONSTRAINT fk_stock FOREIGN KEY (stock_id) REFERENCES stock (id)
);
CREATE INDEX ON stock_price (stock_id, dt DESC);
SELECT create_hypertable('stock_price', 'dt');