-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathschema.sql
259 lines (233 loc) · 9.22 KB
/
schema.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
CREATE TABLE `User`
(
`ID` varchar(36) NOT NULL,
`Username` varchar(30) UNIQUE NOT NULL,
`Password` varchar(256) NOT NULL,
`Name` varchar(50) NOT NULL,
`User_Type` enum ('ADMIN', 'MANAGER', 'EMPLOYEE', 'CUSTOMER') NOT NULL,
`Phone_Number` varchar(10) NOT NULL,
`DOB` date NOT NULL,
`Address` varchar(100) NOT NULL,
PRIMARY KEY (`ID`)
);
CREATE TABLE `Employee`
(
`ID` varchar(36) NOT NULL,
`Branch_ID` varchar(36) NOT NULL,
PRIMARY KEY (`ID`),
FOREIGN KEY (`ID`) REFERENCES `User` (`ID`) ON DELETE CASCADE ON UPDATE CASCADE
);
CREATE TABLE `Branch`
(
`ID` varchar(36) NOT NULL,
`Name` varchar(50) NOT NULL,
`Address` varchar(100) NOT NULL,
`Manager_ID` varchar(36) NULL,
PRIMARY KEY (`ID`),
FOREIGN KEY (`Manager_ID`) REFERENCES `Employee` (`ID`)
);
ALTER TABLE `Employee` ADD FOREIGN KEY (`Branch_ID`) REFERENCES `Branch`(`ID`) ON DELETE RESTRICT ON UPDATE CASCADE;
CREATE TABLE `Savings_Plan`
(
`ID` int NOT NULL AUTO_INCREMENT,
`Name` varchar(20) NOT NULL,
`Interest_Rate` int NOT NULL,
`Minimum_Balance` decimal(15, 2) NOT NULL,
`Minimum_Age` int NULL,
`Maximum_Age` int NULL,
PRIMARY KEY (`ID`)
);
CREATE TABLE `Account`
(
`Account_Number` varchar(20) NOT NULL,
`User_ID` varchar(36) NOT NULL,
`Created_Time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`Branch_ID` varchar(36) NOT NULL,
`Account_Type` enum ('CURRENT', 'SAVINGS') NOT NULL,
`Amount` decimal(15, 2) NOT NULL,
PRIMARY KEY (`Account_Number`),
FOREIGN KEY (`Branch_ID`) REFERENCES `Branch` (`ID`),
FOREIGN KEY (`User_ID`) REFERENCES `User` (`ID`),
CHECK (`Amount` >= 0)
);
CREATE TABLE `Savings`
(
`Account_Number` varchar(20) NOT NULL,
`Plan_ID` int NOT NULL,
PRIMARY KEY (`Account_Number`),
FOREIGN KEY (`Account_Number`) REFERENCES `Account` (`Account_Number`),
FOREIGN KEY (`Plan_ID`) REFERENCES `Savings_Plan` (`ID`)
);
CREATE TABLE `Transaction`
(
`Transaction_ID` varchar(36) NOT NULL,
`From` varchar(20) NULL DEFAULT NULL,
`To` varchar(20) NULL DEFAULT NULL,
`Type` enum ('WITHDRAWAL', 'TRANSFER', 'DEPOSIT', 'PAYMENT') NOT NULL,
`Amount` decimal(15, 2) NOT NULL,
`Created_Time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`Description` text NULL DEFAULT NULL,
PRIMARY KEY (`Transaction_ID`),
FOREIGN KEY (`From`) REFERENCES `Account` (`Account_Number`),
FOREIGN KEY (`To`) REFERENCES `Account` (`Account_Number`),
CHECK (`Amount` > 0)
);
CREATE TABLE `FD_Plan`
(
`ID` int NOT NULL AUTO_INCREMENT,
`Duration` int NOT NULL,
`Interest_Rate` int NOT NULL,
PRIMARY KEY (`ID`)
);
CREATE TABLE `FD`
(
`ID` varchar(36) NOT NULL,
`Account_Number` varchar(20) NOT NULL,
`Plan_ID` int NOT NULL,
`Amount` decimal(15, 2) NOT NULL,
`Created_Time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`ID`),
FOREIGN KEY (`Plan_ID`) REFERENCES `FD_Plan` (`ID`),
FOREIGN KEY (`Account_Number`) REFERENCES `Account` (`Account_Number`),
CHECK (`Amount` > 0)
);
CREATE TABLE `Loan_Plan`
(
`ID` int NOT NULL AUTO_INCREMENT,
`Interest_Rate` decimal(5, 2) NOT NULL,
`Duration` int NOT NULL,
PRIMARY KEY (`ID`)
);
CREATE TABLE `Loan`
(
`ID` varchar(36) NOT NULL,
`User_ID` varchar(36) NOT NULL,
`Loan_Type` enum ('PERSONAL', 'BUSINESS') NOT NULL,
`Status` enum ('CREATED', 'APPROVED', 'REJECTED', 'PAID') NOT NULL,
`Created_Time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`Amount` decimal(15, 2) NOT NULL,
`Loan_Mode` enum ('NORMAL', 'ONLINE') NOT NULL,
`Plan_ID` int NOT NULL,
`Reason` text,
PRIMARY KEY (`ID`),
FOREIGN KEY (`User_ID`) REFERENCES `User` (`ID`),
FOREIGN KEY (`Plan_ID`) REFERENCES `Loan_Plan` (`ID`),
CHECK (`Amount` > 0)
);
CREATE TABLE `Normal_Loan`
(
`ID` varchar(36) NOT NULL,
`Account_Number` varchar(20) NOT NULL,
PRIMARY KEY (`ID`),
FOREIGN KEY (`ID`) REFERENCES `Loan` (`ID`),
FOREIGN KEY (`Account_Number`) REFERENCES `Account` (`Account_Number`)
);
CREATE TABLE `Online_Loan`
(
`ID` varchar(36) NOT NULL,
`FD_ID` varchar(36) NOT NULL,
PRIMARY KEY (`ID`),
FOREIGN KEY (`ID`) REFERENCES `Loan` (`ID`),
FOREIGN KEY (`FD_ID`) REFERENCES `FD` (`ID`)
);
CREATE TABLE `Installment`
(
`ID` varchar(36) NOT NULL,
`Loan_ID` varchar(36) NOT NULL,
`Year` int NOT NULL,
`Month` int NOT NULL,
`Amount` decimal(15, 2) NOT NULL,
`Status` enum ('CREATED', 'PAID') NOT NULL,
PRIMARY KEY (`ID`),
FOREIGN KEY (`Loan_ID`) REFERENCES `Loan` (`ID`),
CHECK (`Amount` > 0)
);
DELIMITER $$
CREATE EVENT fd_interest
ON SCHEDULE EVERY 1 DAY
ON COMPLETION PRESERVE
DO
BEGIN
DECLARE length INT DEFAULT 0;
DECLARE counter INT DEFAULT 0;
DECLARE month_length INT DEFAULT 30;
SELECT COUNT(*) FROM FD INTO length;
SET counter = 0;
SET month_length = 30;
WHILE counter < length
DO
SELECT F.ID, F.Account_Number, F.Amount, FP.Duration, FP.Interest_Rate, F.Created_Time
INTO @id, @s, @a, @d, @r, @t
FROM FD F
JOIN FD_Plan FP on F.Plan_ID = FP.ID
LIMIT counter, 1;
SELECT TIMESTAMPDIFF(DAY, @t, current_timestamp()) INTO @time_diff;
IF @time_diff MOD month_length = 0 AND @time_diff <= @d * month_length THEN
UPDATE Account SET Amount = Amount + @a * @r / (100 * 12) WHERE Account_Number = @s;
IF @time_diff = @d * month_length THEN
UPDATE Account SET Amount = Amount + @a WHERE Account_Number = @s;
DELETE FROM FD WHERE ID = @id;
END IF;
END IF;
SET counter = counter + 1;
END WHILE;
END $$
CREATE EVENT savings_interest
ON SCHEDULE EVERY 1 DAY
ON COMPLETION PRESERVE
DO
BEGIN
DECLARE length INT DEFAULT 0;
DECLARE counter INT DEFAULT 0;
DECLARE month_length INT DEFAULT 30;
SELECT COUNT(*) FROM Savings INTO length;
SET counter = 0;
SET month_length = 30;
WHILE counter < length
DO
SELECT S.Account_Number, A.Created_Time, SP.Interest_Rate, A.Amount
INTO @num, @t, @r, @a
FROM Savings S
JOIN Savings_Plan SP ON S.Plan_ID = SP.ID
JOIN Account A ON S.Account_Number = A.Account_Number
LIMIT counter, 1;
SELECT TIMESTAMPDIFF(DAY, @t, current_timestamp()) INTO @time_diff;
IF @time_diff MOD month_length = 0 THEN
UPDATE Account SET Amount = Amount + @a * @r / (100 * 12) WHERE Account_Number = @num;
END IF;
SET counter = counter + 1;
END WHILE;
END $$
CREATE TRIGGER limit_withdrawals
BEFORE INSERT
ON `Transaction`
FOR EACH ROW
BEGIN
DECLARE maximum_withdrawals INT;
SET maximum_withdrawals = 5;
IF (NEW.Type = 'WITHDRAWAL') THEN
IF (SELECT 1 FROM Account WHERE Account_Number = NEW.From AND Account_Type = 'SAVINGS') THEN
SELECT COUNT(*)
INTO @count
FROM Transaction
WHERE `From` = NEW.From
AND Type = 'WITHDRAWAL'
AND MONTH(Created_Time) = MONTH(NOW());
IF (@count >= maximum_withdrawals) THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'No more withdrawals this month';
END IF;
END IF;
END IF;
END $$
CREATE TRIGGER mark_loan_paid
AFTER UPDATE
ON `Installment`
FOR EACH ROW
BEGIN
IF (NEW.Status = 'PAID') THEN
IF (SELECT COUNT(*) FROM Installment WHERE Loan_ID = NEW.Loan_ID AND Status <> 'PAID') = 0 THEN
UPDATE Loan SET Status = 'PAID' WHERE ID = NEW.Loan_ID;
END IF;
END IF;
END $$
DELIMITER ;