forked from HasithaAthukorala/mysqlProject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bank.sql
1268 lines (1150 loc) · 41.9 KB
/
bank.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
DROP DATABASE IF EXISTS bank;
CREATE DATABASE bank;
USE bank;
CREATE TABLE FDType (
typeId VARCHAR(20),
interest INT NOT NULL,
time INT NOT NULL,
PRIMARY KEY (typeId)
);
-- to validate fdtype
DELIMITER $$
CREATE PROCEDURE `check_fdtypes`(IN interest DECIMAL(13,2), IN time INT)
BEGIN
IF interest < 0
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Interest is incorrect!';
end if;
IF time < 0
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Time is incorrect!';
end if;
END$$
DELIMITER ;
DELIMITER $$
CREATE TRIGGER `check_fdtypes_before_insert`
BEFORE INSERT
ON `FDType`
FOR EACH ROW
BEGIN
CALL check_fdtypes(new.interest, new.time);
END$$
DELIMITER ;
CREATE TABLE Customer (
CustomerId VARCHAR(20),
Address TEXT NOT NULL,
PhoneNumber VARCHAR(10),
EmailAddress TEXT,
CHECK (CHAR_LENGTH(PhoneNumber) = 10),
CHECK (CHAR_LENGTH(PhoneNumber) = 10),
PRIMARY KEY (CustomerId)
);
DELIMITER $$
CREATE PROCEDURE `check_num`(IN phone VARCHAR(20))
BEGIN
IF CHAR_LENGTH(phone) != 10
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'length of the phone number must be equal to 10';
END IF;
END$$
DELIMITER ;
DELIMITER $$
CREATE TRIGGER `parts_before_insert`
BEFORE INSERT
ON `Customer`
FOR EACH ROW
BEGIN
CALL check_num(new.PhoneNumber);
END$$
DELIMITER ;
DELIMITER $$
CREATE TRIGGER `parts_before_update`
BEFORE UPDATE
ON `Customer`
FOR EACH ROW
BEGIN
CALL check_num(new.PhoneNumber);
END$$
DELIMITER ;
CREATE TABLE IndividualCustomer (
CustomerId VARCHAR(20),
FirstName TEXT NOT NULL,
LastName TEXT NOT NULL,
DateOfBirth DATE NOT NULL,
EmployementStatus ENUM ('Married', 'Unmarried') NOT NULL,
NIC VARCHAR(12) NOT NULL,
PRIMARY KEY (CustomerId),
FOREIGN KEY (CustomerId) REFERENCES Customer (CustomerId) ON DELETE CASCADE
);
-- to validate birthdays
DELIMITER $$
CREATE PROCEDURE `check_birthday`(IN dob DATE)
BEGIN
IF dob > NOW()
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Date of Birth is incorrect!';
end if;
END$$
DELIMITER ;
-- validate birthday of new customer
DELIMITER $$
CREATE TRIGGER `check_birthday_before_insert`
BEFORE INSERT
ON `IndividualCustomer`
FOR EACH ROW
BEGIN
CALL check_birthday(new.DateOfBirth);
END$$
DELIMITER ;
CREATE TABLE Organization (
CustomerId VARCHAR(20),
organizationName TEXT NOT NULL,
PRIMARY KEY (CustomerId),
FOREIGN KEY (CustomerId) REFERENCES Customer (CustomerId)
);
CREATE TABLE Interest (
accountType VARCHAR(20) PRIMARY KEY,
interest DECIMAL(13, 2) NOT NULL,
MinimumBalance DECIMAL(13, 2) NOT NULL
);
-- to validate interests and minimum balances
DELIMITER $$
CREATE PROCEDURE `check_rates_balances`(IN interest DECIMAL(13,2), IN minimum_bal DECIMAL(13,2))
BEGIN
IF interest < 0
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Interest is incorrect!';
end if;
IF minimum_bal < 0
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Minimum balance is incorrect!';
end if;
END$$
DELIMITER ;
-- before insert to interest table
DELIMITER $$
CREATE TRIGGER `check_rates_minimum_balance_when_insert`
BEFORE INSERT
ON `Interest`
FOR EACH ROW
BEGIN
CALL check_rates_balances(new.interest, new.MinimumBalance);
END$$
DELIMITER ;
CREATE TABLE Nominee (
NomineeId VARCHAR(20) PRIMARY KEY,
Name VARCHAR(20) NOT NULL,
Address TEXT NOT NULL,
Phone VARCHAR(10) NOT NULL
);
DELIMITER $$
CREATE TRIGGER `parts_before_insert_nominee`
BEFORE INSERT
ON `Nominee`
FOR EACH ROW
BEGIN
CALL check_num(new.Phone);
END$$
DELIMITER ;
DELIMITER $$
CREATE TRIGGER `parts_before_update_nominee`
BEFORE UPDATE
ON `Nominee`
FOR EACH ROW
BEGIN
CALL check_num(new.Phone);
END$$
DELIMITER ;
CREATE TABLE Branch (
branchCode VARCHAR(20) PRIMARY KEY,
branchName VARCHAR(20) NOT NULL,
branchManagerID VARCHAR(20)
);
CREATE TABLE Employee (
employeeID VARCHAR(20) PRIMARY KEY,
branchCode VARCHAR(20) NOT NULL,
firstName varchar(20) NOT NULL,
LastName varchar(20) NOT NULL,
dateOfBirth DATE NOT NULL,
address TEXT NOT NULL,
FOREIGN KEY (branchCode) REFERENCES Branch (branchCode)
);
-- before insert to employee
DELIMITER $$
CREATE TRIGGER `check_employee_birthday_before_insert`
BEFORE INSERT
ON `Employee`
FOR EACH ROW
BEGIN
CALL check_birthday(new.dateOfBirth);
END$$
DELIMITER ;
-- before update an employee
DELIMITER $$
CREATE TRIGGER `check_employee_before_update`
BEFORE UPDATE
ON `Employee`
FOR EACH ROW
BEGIN
CALL check_birthday(new.dateOfBirth);
END$$
DELIMITER ;
CREATE TABLE BranchManager (
branchID VARCHAR(20) PRIMARY KEY,
employeeID VARCHAR(20) NOT NULL,
FOREIGN KEY (employeeID) REFERENCES Employee (employeeID)
);
CREATE TABLE Account (
AccountId VARCHAR(20) NOT NULL,
CustomerId VARCHAR(20) NOT NULL,
branchCode VARCHAR(20) NOT NULL,
AccountBalance DECIMAL(13, 2) NOT NULL,
NomineeId VARCHAR(20) NOT NULL,
PRIMARY KEY (AccountId),
FOREIGN KEY (CustomerId) REFERENCES Customer (CustomerId),
FOREIGN KEY (branchCode) REFERENCES Branch (branchCode),
FOREIGN KEY (NomineeId) REFERENCES Nominee (NomineeId)
);
CREATE TABLE SavingsAccount (
AccountId VARCHAR(20) NOT NULL,
noOfWithdrawals INT NOT NULL,
accountType VARCHAR(20) NOT NULL,
PRIMARY KEY (AccountId),
FOREIGN KEY (AccountId) REFERENCES Account (AccountId),
FOREIGN KEY (accountType) REFERENCES Interest (accountType)
);
CREATE TABLE LateLoans(
loanId INT(11) primary key ,
customerId VARCHAR(20) NOT NULL,
FOREIGN KEY (customerId) REFERENCES Customer(CustomerId)
);
DELIMITER $$
CREATE PROCEDURE `check_balance`(IN AccBalance DECIMAL(13,2), IN AccId VARCHAR(20))
BEGIN
DECLARE account_type VARCHAR(20);
DECLARE minbal DECIMAL(13,2);
SET account_type = (SELECT accountType from SavingsAccount where AccountId = AccId);
SET minbal = (SELECT MinimumBalance from Interest where accountType = account_type);
IF AccBalance < 0
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'check constraint on interest failed!';
END IF;
IF minbal > AccBalance
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Account must keep the minimal balance';
END IF;
END$$
DELIMITER $$
-- when updating an account balance
CREATE TRIGGER `check_account_balance_when_update`
BEFORE UPDATE
ON `Account`
FOR EACH ROW
BEGIN
CALL check_balance(new.AccountBalance, new.AccountId);
END$$
DELIMITER ;
DELIMITER $$
-- to validate no of withdrawals and account type
CREATE PROCEDURE `update_savings_account`(IN noOfWithdrawals INT )
BEGIN
IF noOfWithdrawals < 0
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Withdrawal count should be greater than zero...!';
END IF;
IF noOfWithdrawals > 5
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Withdrawal limit has been exceeded...!';
END IF;
END$$
DELIMITER ;
-- when updating an savings account
DELIMITER $$
CREATE TRIGGER `check_savings_account_when_update`
BEFORE UPDATE
ON `SavingsAccount`
FOR EACH ROW
BEGIN
CALL update_savings_account(new.noOfWithdrawals);
END$$
DELIMITER ;
DELIMITER $$
-- to validate no of account type
CREATE PROCEDURE `insert_savings_account`(IN account VARCHAR(20), IN type VARCHAR(20))
BEGIN
DECLARE dob DATE;
DECLARE age INT;
SET dob = (SELECT DateOfBirth from Account inner join IndividualCustomer on Account.CustomerId = IndividualCustomer.CustomerId where Account.AccountId = account);
SET age = DATEDIFF(CURDATE(),dob)/365;
IF type = "Children"
THEN
IF age > 18
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Incorrect account type for this customer...!';
END IF;
END IF;
IF type = "Teen" OR type = "Adult"
THEN
IF age < 18
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Incorrect account type for this customer...!';
END IF;
END IF;
IF type = "Senior"
THEN
IF age < 60 OR age < 18
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Incorrect account type for this customer...!';
END IF;
END IF;
END$$
DELIMITER ;
-- when inserting to an savings account
DELIMITER $$
CREATE TRIGGER `check_savings_account_when_insert`
BEFORE INSERT
ON `SavingsAccount`
FOR EACH ROW
BEGIN
CALL insert_savings_account(new.AccountId, new.accountType);
END$$
DELIMITER ;
CREATE TABLE FixedDeposit (
FDid VARCHAR(20) NOT NULL,
AccountId VARCHAR(20) NOT NULL,
typeId VARCHAR(20) NOT NULL,
amount DECIMAL(13, 2) NOT NULL,
nextInterestDate DATE NOT NULL,
PRIMARY KEY (FDid),
FOREIGN KEY (typeId) REFERENCES FDType (typeId),
FOREIGN KEY (AccountId) REFERENCES SavingsAccount (AccountId) ON DELETE CASCADE
);
DELIMITER $$
-- to validate fd amount
CREATE PROCEDURE `check_fd_amount`(IN amount DECIMAL(13,2))
BEGIN
IF amount < 0
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'FD amount should be greater than zero...!';
END IF;
END$$
DELIMITER ;
-- when inserting to a fd
DELIMITER $$
CREATE TRIGGER `check_fd_when_insert`
BEFORE INSERT
ON `FixedDeposit`
FOR EACH ROW
BEGIN
CALL check_fd_amount(new.amount);
END$$
DELIMITER ;
CREATE TABLE Gurantor (
gurantoID VARCHAR(20) NOT NULL,
NoOfLoans INT(2),
PRIMARY KEY (gurantoID),
FOREIGN KEY (gurantoID) REFERENCES Customer (CustomerId)
);
DELIMITER $$
CREATE TRIGGER `parts_before_update_Gurantor`
BEFORE UPDATE
ON `Gurantor`
FOR EACH ROW
BEGIN
DECLARE count INT(2);
SELECT NoOfLoans INTO count FROM `Gurantor` WHERE gurantoID = NEW.gurantoID;
SET count = count + 1;
IF count>3 THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Guranor has already guranterd for 3 loans';
end if ;
END$$
DELIMITER ;
CREATE TABLE LoanInterest (
loanType ENUM ("1", "2", "3"),
interest DECIMAL(13,2) NOT NULL,
installmentDuration INT NOT NULL,
PRIMARY KEY (loanType)
);
CREATE TABLE LoanApplicaton (
applicationID INT NOT NULL AUTO_INCREMENT,
gurrantorID VARCHAR(20),
purpose TEXT NOT NULL,
sourceOfFunds TEXT NOT NULL,
collateralType TEXT NOT NULL,
collateraNotes TEXT NOT NULL,
applicationStatus BOOLEAN NOT NULL,
customerID VARCHAR(20) NOT NULL,
loanType ENUM ("1", "2", "3") NOT NULL,
loanAmount DECIMAL(13, 2) NOT NULL,
startDate DATE NOT NULL,
endDate DATE NOT NULL,
PRIMARY KEY (applicationID),
FOREIGN KEY (gurrantorID) REFERENCES Gurantor (gurantoID),
FOREIGN KEY (customerID) REFERENCES Customer (CustomerId),
FOREIGN KEY (loanType) REFERENCES LoanInterest (loanType)
);
# Validation for LoanInterest table
DELIMITER $$
CREATE PROCEDURE `check_LoanInterest`(IN interest DECIMAL(13,2), IN installmentDuration INT)
BEGIN
IF interest < 0
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'check constraint on interest failed!';
END IF;
IF installmentDuration < 0
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'check constraint on installmentDuration failed!';
END IF;
END$$
DELIMITER ;
DELIMITER $$
CREATE TRIGGER `LoanInterest_before_insert`
BEFORE INSERT
ON `LoanInterest`
FOR EACH ROW
BEGIN
CALL check_LoanInterest(new.interest, new.installmentDuration);
END$$
DELIMITER ;
-- before update
DELIMITER $$
CREATE TRIGGER `LoanInterest_before_update`
BEFORE UPDATE
ON `LoanInterest`
FOR EACH ROW
BEGIN
CALL check_LoanInterest(new.interest, new.installmentDuration);
END$$
DELIMITER ;
CREATE TABLE Loan (
loanID INT AUTO_INCREMENT NOT NULL,
customerID VARCHAR(20) NOT NULL,
loanType ENUM ("1", "2", "3") NOT NULL,
loanAmount DECIMAL(13, 2) NOT NULL,
startDate DATE NOT NULL,
endDate DATE NOT NULL,
nextInstallmentDate DATE NOT NULL,
nextInstallment DECIMAL(13, 2) NOT NULL,
numberOfInstallments INT NOT NULL,
applicationID INT NOT NULL,
PRIMARY KEY (loanID),
FOREIGN KEY (loanType) REFERENCES LoanInterest (loanType),
FOREIGN KEY (applicationID) REFERENCES LoanApplicaton (applicationID),
FOREIGN KEY (customerID) REFERENCES Customer (CustomerId)
);
CREATE TABLE OnlineLoan (
loanID INT,
FDid VARCHAR(20) NOT NULL,
PRIMARY KEY (loanID),
FOREIGN KEY (FDid) REFERENCES FixedDeposit (FDid)
);
-- validate online loan
# validation for Loan table
DELIMITER $$
CREATE PROCEDURE `check_Loan`(IN loanAmount DECIMAL(13, 2), IN numberOfInstallments INT, IN nextInstallment DECIMAL(13, 2))
BEGIN
IF loanAmount < 0
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'check constraint on loan amount failed!';
END IF;
IF numberOfInstallments < 0
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'check constraint on number of installments failed!';
END IF;
IF nextInstallment < 0
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'check constraint on next installment failed!';
END IF;
END$$
DELIMITER ;
DELIMITER $$
CREATE TRIGGER `Loan_before_insert`
BEFORE INSERT
ON `Loan`
FOR EACH ROW
BEGIN
CALL check_Loan(new.loanAmount, new.numberOfInstallments, new.nextInstallment);
END$$
DELIMITER ;
-- before update
DELIMITER $$
CREATE TRIGGER `Loan_before_update`
BEFORE UPDATE
ON `Loan`
FOR EACH ROW
BEGIN
CALL check_Loan(new.loanAmount, new.numberOfInstallments, new.nextInstallment);
END$$
DELIMITER ;
CREATE TABLE LoanInstallment (
installmentID INT NOT NULL AUTO_INCREMENT,
loanID INT,
installmentTimeStamp TIMESTAMP NOT NULL,
installmentAmount DECIMAL(13, 2) NOT NULL,
PRIMARY KEY (installmentID),
FOREIGN KEY (loanID) REFERENCES Loan (loanID)
);
# validation for LoanInstallment table
DELIMITER $$
CREATE PROCEDURE `check_LoanInstallment`(IN installmentAmount DECIMAL(13, 2))
BEGIN
IF installmentAmount < 0
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'check constraint on interest failed!';
END IF;
END$$
DELIMITER ;
DELIMITER $$
CREATE TRIGGER `LoanInstallment_before_insert`
BEFORE INSERT
ON `LoanInstallment`
FOR EACH ROW
BEGIN
CALL check_LoanInstallment(new.installmentAmount);
END$$
DELIMITER ;
-- before update
DELIMITER $$
CREATE TRIGGER `LoanInstallment_before_update`
BEFORE UPDATE
ON `LoanInstallment`
FOR EACH ROW
BEGIN
CALL check_LoanInstallment(new.installmentAmount);
END$$
DELIMITER ;
CREATE TABLE ATMInformation (
ATMId varchar(20) PRIMARY KEY,
OfficerInCharge VARCHAR(20) NOT NULL,
location VARCHAR(20) NOT NULL,
branchCode VARCHAR(20) NOT NULL,
Amount DECIMAL(13,2),
FOREIGN KEY (branchCode) REFERENCES Branch (branchCode),
FOREIGN KEY (OfficerInCharge) REFERENCES Employee (employeeID)
);
CREATE TABLE ATMTransaction (
TransactionID INT PRIMARY KEY AUTO_INCREMENT,
fromAccountID VARCHAR(20) NOT NULL,
ATMId VARCHAR(20) NOT NULL,
TimeStamp TIMESTAMP NOT NULL,
Amount DECIMAL(13,2),
FOREIGN KEY (fromAccountID) REFERENCES Account (AccountId),
FOREIGN KEY (ATMId) REFERENCES ATMInformation (ATMId)
);
CREATE TABLE Transaction (
TransactionID INT PRIMARY KEY AUTO_INCREMENT,
fromAccountID VARCHAR(20) NOT NULL,
toAccountID VARCHAR(20) NOT NULL,
branchCode VARCHAR(20) NOT NULL,
TimeStamp TIMESTAMP NOT NULL,
Amount DECIMAL(13,2),
FOREIGN KEY (fromAccountID) REFERENCES Account (AccountId),
FOREIGN KEY (toAccountID) REFERENCES Account (AccountId),
FOREIGN KEY (branchCode) REFERENCES Branch (branchCode)
);
CREATE TABLE ATMCard (
cardID varchar(20) PRIMARY KEY,
AccountID VARCHAR(20) NOT NULL,
startDate DATE NOT NULL,
ExpireDate DATE NOT NULL,
FOREIGN KEY (AccountID) REFERENCES Account (AccountId)
);
CREATE TABLE UserLogin (
id INT AUTO_INCREMENT,
username VARCHAR(255),
CustomerId VARCHAR(20),
passsword VARCHAR(32),
role ENUM ("admin", "user", "employee"),
PRIMARY KEY (id),
FOREIGN KEY (CustomerId) REFERENCES Customer(CustomerId)
);
DELIMITER $$
CREATE PROCEDURE `check_password_length`(IN pass VARCHAR(32))
BEGIN
IF CHAR_LENGTH(pass) != 32
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'password must be in md5 format';
END IF;
END$$
DELIMITER ;
DELIMITER $$
CREATE TRIGGER `parts_before_insert_password`
BEFORE INSERT
ON `UserLogin`
FOR EACH ROW
BEGIN
CALL check_password_length(new.passsword);
END$$
DELIMITER ;
DELIMITER $$
CREATE TRIGGER `parts_before_update_password`
BEFORE UPDATE
ON `UserLogin`
FOR EACH ROW
BEGIN
CALL check_password_length(new.passsword);
END$$
DELIMITER ;
#Adding a transaction
DELIMITER $$
CREATE FUNCTION check_account_balance(old_balance DECIMAL(13, 2), transaction_amount DECIMAL(13, 2))
RETURNS BOOLEAN
DETERMINISTIC
BEGIN
DECLARE remained_amount DECIMAL(13, 2);
SET remained_amount = (old_balance - transaction_amount);
IF remained_amount < 0
THEN
RETURN false;
ELSE
RETURN true;
END IF;
END$$
DELIMITER ;
DROP TRIGGER IF EXISTS `parts_before_insert_transaction_normal`;
DROP TRIGGER IF EXISTS `parts_before_update_transaction_normal`;
DELIMITER $$
CREATE TRIGGER `parts_before_insert_transaction_normal`
BEFORE INSERT
ON `Transaction`
FOR EACH ROW
BEGIN
DECLARE old_balance DECIMAL(13, 2);
SELECT AccountBalance INTO old_balance FROM `Account` WHERE AccountId = NEW.fromAccountID;
IF check_account_balance(old_balance, NEW.Amount) = false
THEN
SIGNAL SQLSTATE '45002'
SET MESSAGE_TEXT = 'Account balance not enough to transfer';
END IF;
END$$
DELIMITER ;
DELIMITER $$
CREATE TRIGGER `parts_before_update_transaction_normal`
BEFORE UPDATE
ON `Transaction`
FOR EACH ROW
BEGIN
DECLARE old_balance DECIMAL(13, 2);
SELECT AccountBalance INTO old_balance FROM `Account` WHERE AccountId = NEW.fromAccountID;
IF check_account_balance(old_balance, NEW.Amount) = false
THEN
SIGNAL SQLSTATE '45002'
SET MESSAGE_TEXT = 'Account balance is not enough to transfer';
END IF;
END$$
DELIMITER ;
SELECT COUNT(*) AS 'result'
FROM UserLogin
WHERE EXISTS(SELECT passsword
FROM UserLogin
WHERE username = 'TESTOR01'
AND passsword = MD5('0773842106')
AND role = 'user');
CREATE VIEW branchDetailView AS
SELECT branchCode,branchName FROM Branch;
#SELECT * FROM branchDetailView;
CREATE VIEW accountDetailsView AS
SELECT AccountID,customerID,branchCode,AccountBalance,NomineeId FROM Account;
CREATE VIEW customerDetailView AS
SELECT CustomerId,FirstName,LastName FROM IndividualCustomer;
CREATE VIEW userLoginView AS
SELECT username,passsword,role FROM UserLogin;
CREATE VIEW accountTypeDetails AS
SELECT accountType FROM Interest;
CREATE VIEW pendingLoanStatus AS
SELECT applicationID, applicationStatus FROM LoanApplicaton;
CREATE VIEW transactionHistoryView AS
SELECT fromAccountID,toAccountID,TimeStamp,Amount ,(SELECT CustomerId FROM Account JOIN Transaction T on Account.AccountId = T.fromAccountID LIMIT 1) AS fromCustomerId ,(SELECT CustomerId FROM Account JOIN Transaction T on Account.AccountId = T.toAccountID LIMIT 1) AS toCustomerId FROM Transaction ORDER BY Transaction.TransactionID DESC ;
CREATE VIEW atmTransactionHistoryView AS
SELECT fromAccountID,TimeStamp,Amount,(SELECT CustomerId FROM Account JOIN ATMTransaction T on Account.AccountId = T.fromAccountID LIMIT 1) AS fromCustomerId FROM ATMTransaction;
CREATE VIEW atmDetails AS
SELECT ATMId FROM ATMInformation;
DELIMITER $$
CREATE PROCEDURE creditTransferAccounts(IN fromAccount VARCHAR(20), IN toAccount VARCHAR(20),IN branchCode VARCHAR(20),IN amount DECIMAL(13,2))
BEGIN
DECLARE newBalance_from DECIMAL(13,2);
DECLARE newBalance_to DECIMAL(13,2);
DECLARE withdrawals INT(11);
SET withdrawals = (SELECT noOfWithdrawals FROM SavingsAccount WHERE AccountId = fromAccount) + 1;
SET newBalance_from = (SELECT AccountBalance FROM Account WHERE AccountId = fromAccount) - amount;
SET newBalance_to = (SELECT AccountBalance FROM Account WHERE AccountId = fromAccount) + amount;
IF amount > 0 THEN
START TRANSACTION ;
INSERT INTO Transaction(`fromAccountID`,`toAccountID`,`branchCode`,`amount`)
VALUES (fromAccount,toAccount,branchCode,amount);
UPDATE Account
SET AccountBalance = newBalance_from WHERE AccountId = fromAccount;
UPDATE Account
SET AccountBalance = newBalance_to WHERE AccountId = toAccount;
UPDATE SavingsAccount
SET noOfWithdrawals = withdrawals WHERE AccountId = fromAccount;
COMMIT;
ELSE
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'TRANSACTION FAILED';
END IF;
END
$$
DELIMITER ;
DELIMITER $$
CREATE PROCEDURE atmWithdraw(IN fromAccount VARCHAR(20), IN atmId VARCHAR(20),IN _amount DECIMAL(13,2))
BEGIN
DECLARE newBalance DECIMAL(13,2);
DECLARE atmBalance DECIMAL(13,2);
DECLARE withdrawals INT(11);
SELECT (AccountBalance ) INTO newBalance FROM Account WHERE AccountId = fromAccount LIMIT 1;
SELECT (Amount ) INTO atmBalance FROM ATMInformation WHERE ATMId=atmId LIMIT 1;
SELECT (noOfWithdrawals ) INTO withdrawals FROM SavingsAccount WHERE AccountId = fromAccount LIMIT 1;
SET newBalance = newBalance - _amount;
SET atmBalance = atmBalance - _amount;
SET withdrawals = withdrawals + 1;
IF atmBalance >= 0 AND _amount > 0 THEN
START TRANSACTION ;
INSERT INTO ATMTransaction(`fromAccountID`,`ATMId`,`amount`)
VALUES (fromAccount,atmId,_amount);
UPDATE Account
SET AccountBalance = newBalance WHERE AccountId = fromAccount;
UPDATE ATMInformation
SET Amount = atmBalance WHERE ATMId= atmId;
UPDATE SavingsAccount
SET noOfWithdrawals = withdrawals WHERE AccountId = fromAccount;
COMMIT;
ELSE
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'TRANSACTION FAILED';
END IF ;
END
$$
DELIMITER ;
DELIMITER $$
CREATE PROCEDURE deposite(IN toAccount VARCHAR(20),IN _amount DECIMAL(13,2))
BEGIN
DECLARE newBalance DECIMAL(13,2);
SELECT (AccountBalance ) INTO newBalance FROM Account WHERE AccountId = fromAccount LIMIT 1;
SET newBalance = newBalance + _amount;
IF _amount > 0 THEN
START TRANSACTION ;
UPDATE Account
SET AccountBalance = newBalance WHERE AccountId = toAccount;
COMMIT;
ELSE
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'TRANSACTION FAILED';
END IF ;
END
$$
DELIMITER ;
DELIMITER $$
CREATE PROCEDURE createSavingAccount(IN accountId VARCHAR(20),
IN CustomerId VARCHAR(20),
IN branchCode VARCHAR(20),
IN accountBalance DECIMAL(13,2),
IN NomineeId VARCHAR(20),
IN accountType VARCHAR(20))
BEGIN
# CHECK MINIMUM BALANCE
DECLARE minimumBlance DECIMAL(13,2);
SELECT MinimumBalance INTO minimumBlance FROM Interest WHERE Interest.accountType = accountType;
IF minimumBlance <= accountBalance THEN
START TRANSACTION ;
INSERT INTO `Account` (`AccountId`, `CustomerId`, `branchCode`, `AccountBalance`, `NomineeId`)
VALUES (accountId,CustomerId,branchCode,accountBalance,NomineeId);
INSERT INTO `SavingsAccount` (`AccountId`,`noOfWithdrawals`,`accountType`)
VALUES (accountId,0,accountType);
COMMIT ;
ELSE
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'ACCOUNT BALANCE IS LESS THAN MINIMUM BALANCE OR ACCOUNT TYPE IS INVALID';
END IF ;
END
$$
DELIMITER ;
DELIMITER $$
CREATE PROCEDURE createFixedDeposit(IN FDid VARCHAR(20),
IN AccountId VARCHAR(20),
IN typeId VARCHAR(20),
IN amount DECIMAL(13,2))
BEGIN
DECLARE nextInterestDate DATETIME;
SET nextInterestDate = DATE_ADD(CURDATE(), INTERVAL 30 DAY);
IF amount > 0 THEN
START TRANSACTION;
INSERT INTO FixedDeposit(`FDid`,`AccountId`,`typeId`,`amount`,`nextInterestDate`)
VALUES (FDid,AccountId,typeId,amount,nextInterestDate);
COMMIT;
ELSE
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'FIXED DEPOSIT AMOUNT MUST BE GREATER THAN 0';
END IF ;
END
$$
DELIMITER ;
# time based events
SET GLOBAL event_scheduler = 1;
DELIMITER $$
CREATE EVENT savingAccountInterestCalculationEvent
ON SCHEDULE EVERY '1' MONTH
STARTS '2018-12-01 00:00:00'
DO
BEGIN
START TRANSACTION ;
UPDATE Account
SET AccountBalance = (SELECT AccountBalance * (1 + (interest/100)) FROM SavingsAccount left join Interest on SavingsAccount.accountType = Interest.accountType where Account.AccountId = SavingsAccount.AccountId)
WHERE AccountId IN (
SELECT AccountId FROM SavingsAccount
);
UPDATE SavingsAccount
SET noOfWithdrawals = 0;
COMMIT ;
END
$$
DELIMITER ;
DELIMITER $$
CREATE EVENT lateLoanLoggerFlusher
ON SCHEDULE EVERY '1' MONTH
STARTS '2018-12-01 00:00:00'
DO
BEGIN
START TRANSACTION ;
DELETE FROM LateLoans;
COMMIT ;
END
$$
DELIMITER ;
DELIMITER $$
CREATE EVENT lateLoanLogger
ON SCHEDULE EVERY '1' DAY
DO
BEGIN
START TRANSACTION ;
INSERT INTO LateLoans
SELECT loanID,customerId FROM Loan WHERE CURDATE()> nextInstallmentDate;
COMMIT;
END
$$
DELIMITER ;
DELIMITER $$
CREATE EVENT fixedDepositInterestEvent
ON SCHEDULE EVERY '1' DAY
DO
BEGIN
START TRANSACTION ;
UPDATE Account
SET AccountBalance = (SELECT AccountBalance * (1 + (interest/100)) FROM FixedDeposit LEFT JOIN FDType T on FixedDeposit.typeId = T.typeId where Account.AccountId = FixedDeposit.AccountId)
WHERE AccountId IN (
SELECT AccountId FROM FixedDeposit WHERE nextInterestDate = CURDATE()
);
UPDATE FixedDeposit
SET nextInterestDate = DATE_ADD(CURDATE(), INTERVAL 30 DAY)
WHERE nextInterestDate = curdate();
COMMIT ;
END
$$
DELIMITER ;
# Loan application
DELIMITER $$
CREATE FUNCTION check_acount
(id Varchar(20))
RETURNS boolean
BEGIN
DECLARE result boolean;
DECLARE newID INT;
SELECT COUNT(CustomerId) into newID from Customer WHERE CustomerId = id;
IF newID > 0
then
SET result = TRUE;
ELSE
SET result = FALSE;
end if;
RETURN result;
END $$
DELIMITER ;
DELIMITER $$
CREATE PROCEDURE update_loanCount(id VARCHAR(20))
BEGIN
DECLARE count INT(2);
SELECT NoOfLoans INTO count FROM Gurantor WHERE gurantoID = id;
SET count = count + 1;
UPDATE Gurantor SET NoOfLoans = count WHERE gurantoID = id;
END $$
DELIMITER ;
DELIMITER $$
CREATE FUNCTION check_gurantor
(id Varchar(20))
RETURNS boolean
BEGIN
DECLARE result boolean;
DECLARE newID INT;
SELECT COUNT(gurantoID) into newID from Gurantor WHERE gurantoID = id;
IF newID > 0
then
SET result = TRUE;
ELSE
SET result = FALSE;
end if;
RETURN result;
END $$
DELIMITER ;
DELIMITER $$
CREATE PROCEDURE approveLoanApplication(IN _applicationID INT(11))
BEGIN
START TRANSACTION ;
UPDATE pendingLoanStatus
SET applicationStatus = 1 WHERE applicationID = _applicationID;
INSERT INTO Loan (customerID, loanType, loanAmount, startDate, endDate, nextInstallmentDate, nextInstallment, numberOfInstallments, applicationID)
SELECT customerID,loanType,loanAmount,startDate,endDate,DATE_ADD(startDate, INTERVAL 30 DAY),loanAmount/CAST(DATEDIFF(endDate,startDate)/30 AS INT),CAST(DATEDIFF(endDate,startDate)/30 AS INT),applicationID FROM LoanApplicaton;