-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser_schema.sql
More file actions
78 lines (72 loc) · 3.22 KB
/
user_schema.sql
File metadata and controls
78 lines (72 loc) · 3.22 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
-- Simplified User Stock Market Database Schema
-- 1. User Wallets (Virtual money for trading)
CREATE TABLE IF NOT EXISTS user_wallets (
user_id INTEGER PRIMARY KEY,
balance DECIMAL(15,2) DEFAULT 0.00,
total_invested DECIMAL(15,2) DEFAULT 0.00,
total_current_value DECIMAL(15,2) DEFAULT 0.00,
total_profit_loss DECIMAL(15,2) DEFAULT 0.00,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE
);
-- 2. Portfolio Holdings (Current stock positions)
CREATE TABLE IF NOT EXISTS portfolio_holdings (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
symbol VARCHAR(20) NOT NULL,
company_name VARCHAR(255),
quantity INTEGER NOT NULL,
average_price DECIMAL(10,2) NOT NULL,
current_price DECIMAL(10,2) DEFAULT 0.00,
invested_amount DECIMAL(15,2) NOT NULL,
current_value DECIMAL(15,2) DEFAULT 0.00,
profit_loss DECIMAL(15,2) DEFAULT 0.00,
profit_loss_percent DECIMAL(5,2) DEFAULT 0.00,
first_buy_date DATETIME DEFAULT CURRENT_TIMESTAMP,
last_updated DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE,
UNIQUE(user_id, symbol)
);
-- 3. Stock Transactions (Buy/Sell history)
CREATE TABLE IF NOT EXISTS stock_transactions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
symbol VARCHAR(20) NOT NULL,
company_name VARCHAR(255),
transaction_type VARCHAR(10) NOT NULL, -- 'BUY', 'SELL'
quantity INTEGER NOT NULL,
price DECIMAL(10,2) NOT NULL,
total_amount DECIMAL(15,2) NOT NULL,
transaction_date DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE
);
-- 4. Wallet Transactions (Money in/out)
CREATE TABLE IF NOT EXISTS wallet_transactions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
transaction_type VARCHAR(20) NOT NULL, -- 'DEPOSIT', 'WITHDRAWAL', 'STOCK_PURCHASE', 'STOCK_SALE'
amount DECIMAL(15,2) NOT NULL,
balance_after DECIMAL(15,2) NOT NULL,
description TEXT,
reference_id VARCHAR(100), -- Transaction ID for stock purchases/sales
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE
);
-- 5. User Watchlist (Stocks user is watching)
CREATE TABLE IF NOT EXISTS user_watchlist (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
symbol VARCHAR(20) NOT NULL,
company_name VARCHAR(255),
added_date DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE,
UNIQUE(user_id, symbol)
);
-- Create indexes for better performance
CREATE INDEX IF NOT EXISTS idx_portfolio_user ON portfolio_holdings(user_id);
CREATE INDEX IF NOT EXISTS idx_stock_transactions_user ON stock_transactions(user_id, transaction_date DESC);
CREATE INDEX IF NOT EXISTS idx_wallet_transactions_user ON wallet_transactions(user_id, created_at DESC);
CREATE INDEX IF NOT EXISTS idx_holdings_symbol ON portfolio_holdings(symbol);
CREATE INDEX IF NOT EXISTS idx_watchlist_user ON user_watchlist(user_id);
CREATE INDEX IF NOT EXISTS idx_watchlist_symbol ON user_watchlist(symbol);