-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.sql
More file actions
64 lines (58 loc) · 1.79 KB
/
schema.sql
File metadata and controls
64 lines (58 loc) · 1.79 KB
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
56
57
58
59
60
61
62
63
64
PRAGMA foreign_keys = ON;
-- =========================
-- USERS TABLE
-- =========================
CREATE TABLE users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
username TEXT NOT NULL UNIQUE,
hash TEXT NOT NULL
);
-- =========================
-- BALANCE TABLE
-- =========================
CREATE TABLE balance (
u_id INTEGER PRIMARY KEY,
balance REAL NOT NULL DEFAULT 0.00,
FOREIGN KEY (u_id) REFERENCES users(id) ON DELETE CASCADE
);
-- =========================
-- PORTFOLIO TABLE
-- =========================
CREATE TABLE portfolio (
p_id INTEGER PRIMARY KEY AUTOINCREMENT,
u_id INTEGER NOT NULL,
symbol TEXT NOT NULL,
shares INTEGER NOT NULL DEFAULT 0,
FOREIGN KEY (u_id) REFERENCES users(id) ON DELETE CASCADE
);
-- =========================
-- TRANSACTIONS TABLE
-- =========================
CREATE TABLE transactions (
transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,
u_id INTEGER NOT NULL,
symbol TEXT NOT NULL,
shares INTEGER NOT NULL,
price REAL NOT NULL,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
seller_id INTEGER,
FOREIGN KEY (u_id) REFERENCES users(id) ON DELETE CASCADE,
FOREIGN KEY (seller_id) REFERENCES users(id) ON DELETE SET NULL
);
-- =========================
-- SHARES TABLE
-- =========================
CREATE TABLE shares (
symbol TEXT PRIMARY KEY,
price REAL NOT NULL,
last_updated DATETIME DEFAULT CURRENT_TIMESTAMP
);
-- =========================
-- INDEXES
-- =========================
CREATE INDEX idx_portfolio_user ON portfolio(u_id);
CREATE INDEX idx_portfolio_symbol ON portfolio(symbol);
CREATE INDEX idx_transactions_user ON transactions(u_id);
CREATE INDEX idx_transactions_symbol ON transactions(symbol);
CREATE INDEX idx_transactions_timestamp ON transactions(timestamp);