-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.sql
More file actions
200 lines (155 loc) · 5.08 KB
/
Copy pathschema.sql
File metadata and controls
200 lines (155 loc) · 5.08 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
create database portfolio;
use portfolio;
create table users (
user_id int primary key auto_increment,
name varchar(50) not null,
email varchar(100) not null unique,
phone varchar(10) ,
created_at timestamp default current_timestamp
constraint chk_phone_length check (LEN(phone) = 10),
constraint chk_phone_numeric check (phone not like '%[^0-9]%')
);
create table stocks (
stock_id int primary key auto_increment,
stock_name varchar(100) not null,
symbol varchar(20) not null unique,
sector varchar(50),
current_price decimal(10,2) not null check (current_price >= 0)
);
create table mutual_funds (
mf_id int primary key auto_increment,
mf_name varchar(150) not null,
fund_house varchar(100) not null,
category varchar(50),
nav decimal(10,2) not null check (nav >= 0),
unique (mf_name, fund_house)
);
create table stock_holdings (
holding_id int primary key auto_increment,
user_id int not null,
stock_id int not null,
quantity int not null check (quantity > 0),
average_price decimal(12,2) not null check (average_price >= 0),
unique (user_id, stock_id),
foreign key (user_id) references users(user_id),
foreign key (stock_id) references stocks(stock_id)
);
create table mf_holdings (
holding_id int primary key auto_increment,
user_id int not null,
mf_id int not null,
units decimal(12,4) not null check (units > 0),
average_nav decimal(10,2) not null check (average_nav >= 0),
unique (user_id, mf_id),
foreign key (user_id) references users(user_id),
foreign key (mf_id) references mutual_funds(mf_id)
);
create table stock_transactions (
transaction_id int primary key auto_increment,
user_id int not null,
stock_id int not null,
transaction_type enum('buy','sell') not null,
quantity int not null check (quantity > 0),
price decimal(12,2) not null check (price >= 0),
transaction_date timestamp default current_timestamp,
foreign key (user_id) references users(user_id),
foreign key (stock_id) references stocks(stock_id)
);
create table mf_transactions (
transaction_id int primary key auto_increment,
user_id int not null,
mf_id int not null,
transaction_type enum('buy','sell') not null,
units decimal(12,4) not null check (units > 0),
nav_price decimal(12,2) not null check (nav_price >= 0),
transaction_date timestamp default current_timestamp,
foreign key (user_id) references users(user_id),
foreign key (mf_id) references mutual_funds(mf_id)
);
create index idx_stock_transaction_user on stock_transactions(user_id);
create index idx_stock_transaction_stock on stock_transactions(stock_id);
create index idx_mf_transaction_user on mf_transactions(user_id);
create index idx_mf_transaction_fund on mf_transactions(mf_id);
create index idx_stock_hold_user on stock_holdings(user_id);
create index idx_stock_hold_stock on stock_holdings(stock_id);
create index idx_mf_hold_user on mf_holdings(user_id);
create index idx_mf_hold_fund on mf_holdings(mf_id);
-- dml queries
-- insert into users values
-- (1,'aarav','aarav@mail.com','101',now()),
-- (2,'meera','meera@mail.com','102',now());
-- insert into stocks values
-- (1,'tcs','tcs','it',3800),
-- (2,'infosys','infy','it',1600);
-- insert into mutual_funds values
-- (1,'sbi bluechip','sbi','large cap',120);
-- insert into stock_holdings values
-- (1,1,1,5,3500),
-- (2,2,2,4,1500);
-- insert into mf_holdings values
-- (1,1,1,10,115);
-- insert into stock_transactions values
-- (1,1,1,'buy',5,3500,now()),
-- (2,2,2,'buy',4,1500,now());
-- dql queries
select * from users;
select
u.name as user_name,
s.stock_name as stock,
sh.quantity as shares_owned,
sh.quantity * sh.average_price as total_investment
from users u
join stock_holdings sh
on u.user_id = sh.user_id
join stocks s
on sh.stock_id = s.stock_id;
select user_id, count(*) as total_trades
from stock_transactions
group by user_id
having count(*) >= 1;
select avg(price) from stock_transactions;
select name
from users
where user_id in (
select user_id
from stock_transactions
group by user_id
having sum(price*quantity) >
(select avg(price*quantity) from stock_transactions)
);
create view portfolio_view as
select u.user_id, u.name, s.stock_name, sh.quantity
from users u
join stock_holdings sh on u.user_id = sh.user_id
join stocks s on sh.stock_id = s.stock_id;
delimiter //
create procedure getportfolio(in uid int)
begin
select * from portfolio_view
where user_id = uid;
end //
delimiter ;
call getportfolio(1);
delimiter //
create function totalvalue(uid int)
returns decimal(12,2)
deterministic
begin
declare total decimal(12,2);
select sum(quantity*average_price)
into total
from stock_holdings
where user_id = uid;
return ifnull(total,0);
end //
delimiter ;
select totalvalue(1);
start transaction;
update stock_holdings
set quantity = quantity + 1
where user_id = 1 and stock_id = 1;
commit;
start transaction;
insert into stock_transactions(user_id,stock_id,transaction_type,quantity,price)
values (1,1,'buy',1,3800);
rollback;