-
Notifications
You must be signed in to change notification settings - Fork 0
/
Create.sql
443 lines (339 loc) · 16.5 KB
/
Create.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
# SP23: APPLIED DATABASE TECHNOLOGIES
# Week-11 Final Project – Phase-2
# Project Title: Blood Link: Blood Bank Management and Monitoring System
# Team Memeber: Aniruddho Chatterjee, Jerin Easo Thomas, Shreya Mariam Varghese
##------Section 1. Creation of tables-------
-- Section 1 Code Authorship : Shreya Mariam Varghese - svarghe
## Donor Table - Code Authorship : Shreya Mariam Varghese - svarghe
CREATE TABLE Donor (
DonorId INT PRIMARY KEY NOT NULL,
Name CHAR(200) NOT NULL,
Contact BIGINT(50) NOT NULL,
Email VARCHAR(200) NOT NULL,
BloodType VARCHAR(200) NOT NULL,
DOB DATE NOT NULL,
Gender VARCHAR(200) NOT NULL,
Address VARCHAR(200) NOT NULL
);
## Recipient Table - Code Authorship : Shreya Mariam Varghese - svarghe
CREATE TABLE Recipient (
RecipientId INT PRIMARY KEY NOT NULL,
Name CHAR(200) NOT NULL,
Contact BIGINT(50) NOT NULL,
Email VARCHAR(200) NOT NULL,
RequiredBloodType VARCHAR(200) NOT NULL,
DOB DATE NOT NULL,
Gender VARCHAR(200) NOT NULL,
Address VARCHAR(200) NOT NULL
);
## BloodBank Table - Code Authorship : Shreya Mariam Varghese - svarghe
CREATE TABLE BloodBank (
BloodBankId INT PRIMARY KEY NOT NULL,
Name VARCHAR(200) NOT NULL,
Contact VARCHAR(200),
Helpline VARCHAR(200),
Address VARCHAR(200),
State VARCHAR(200) NOT NULL,
District VARCHAR(200) NOT NULL,
City VARCHAR(200) NOT NULL,
Pincode BIGINT(50),
Fax VARCHAR(200),
Website VARCHAR(200),
Category VARCHAR(200),
ServiceTime VARCHAR(200)
);
## Appointment Table - Code Authorship : Shreya Mariam Varghese - svarghe
CREATE TABLE Appointment (
AppointmentId INT PRIMARY KEY NOT NULL,
DonorId INT,
RecipientId INT,
BloodBankId INT NOT NULL,
Date DATE NOT NULL,
Time TIME NOT NULL,
FOREIGN KEY (DonorId) REFERENCES Donor(DonorId),
FOREIGN KEY (RecipientId) REFERENCES Recipient(RecipientId),
FOREIGN KEY (BloodBankId) REFERENCES BloodBank(BloodBankId)
);
## BloodSample Table - Code Authorship : Shreya Mariam Varghese - svarghe
CREATE TABLE BloodSample (
SampleId INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
DonorId INT NOT NULL,
BloodBankId INT NOT NULL,
CollectionDate DATE NOT NULL,
ExpirationDate DATE NOT NULL,
BloodType VARCHAR(200) NOT NULL,
HaemoglobinLevel VARCHAR(200),
RBCCount VARCHAR(200),
WBCCount VARCHAR(200),
Quantity INT(50),
FOREIGN KEY (DonorId) REFERENCES Donor(DonorId),
FOREIGN KEY (BloodBankId) REFERENCES BloodBank(BloodBankId)
);
## BloodTransfusion Table - Code Authorship : Shreya Mariam Varghese - svarghe
CREATE TABLE BloodTransfusion (
TransfusionId INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
BloodBankId INT NOT NULL,
RecipientId INT NOT NULL,
SampleId INT NOT NULL,
TransfusionDate DATE NOT NULL,
BloodType VARCHAR(200) NOT NULL,
TransfusedQuantity VARCHAR(200),
TransfusionStatus VARCHAR(200) NOT NULL,
FOREIGN KEY (RecipientId) REFERENCES Recipient(RecipientId),
FOREIGN KEY (BloodBankId) REFERENCES BloodBank(BloodBankId),
FOREIGN KEY (SampleId) REFERENCES BloodSample(SampleId)
);
## Inventory Table - Code Authorship : Shreya Mariam Varghese - svarghe
CREATE TABLE Inventory (
BloodBankId INT NOT NULL,
BloodType VARCHAR(200) NOT NULL,
Quantity INT(200)NOT NULL,
FOREIGN KEY (BloodBankId) REFERENCES BloodBank(BloodBankId)
);
## Nodal Table - Code Authorship : Shreya Mariam Varghese - svarghe
CREATE TABLE Nodal (
BloodBankId INT NOT NULL,
OfficerName VARCHAR(200) NOT NULL,
OfficerContact VARCHAR(200),
OfficerEmail VARCHAR(200),
OfficerQualification varchar(200)
);
#Code Authorship : Shreya Mariam Varghese - svarghe
## Drop Table Commands
DROP table Inventory;
DROP table Appointment;
DROP table BloodTransfusion;
DROP table Donor;
DROP table Recipient;
Drop table Nodal;
Drop table mastertable;
DROP table BloodSample;
DROP table BloodBank;
##------Section 2. Project functionality along with Insert,Update and Delete Commands-----
-- Section2. Code Authorship : Jerin Easo Thomas - jerithom
## BloodBank Insert, Update, Truncate and Delete Commands
#Section2. Code Authorship : Jerin Easo Thomas - jerithom
ALTER TABLE BloodBank
MODIFY Contact VARCHAR(100),
MODIFY Helpline VARCHAR(100);
#Section2. Code Authorship : Jerin Easo Thomas - jerithom
SELECT * FROM BloodBank
where State = 'Maharashtra' and city = 'Aurangabad';
#Section2. Code Authorship : Jerin Easo Thomas - jerithom
TRUNCATE TABLE BloodBank;
#Section2. Code Authorship : Jerin Easo Thomas - jerithom
delete from BloodBank
where 1=1;
#Section2. Code Authorship : Jerin Easo Thomas - jerithom
INSERT INTO BloodBank
SELECT Sr_No, Blood_Bank_Name, concat(Contact_No, ', ', Mobile) as Contact, Helpline, Address, State, District, City, Pincode, Fax, Website, Category, Service_Time
FROM blood_bank_mastertable;
## Blood Tranfusion Insert commands
#Section2. Code Authorship : Jerin Easo Thomas - jerithom
INSERT INTO BloodTransfusion (TransfusionId, BloodBankId, RecipientId, SampleId, TransfusionDate, BloodType, TransfusedQuantity, TransfusionStatus)
VALUES (1, 1300, 1, 1, '2023-04-01', 'B+', 1, 'Complete');
#Section2. Code Authorship : Jerin Easo Thomas - jerithom
select RequiredBloodType from Recipient where RecipientId is not null;
#Section2. Code Authorship : Jerin Easo Thomas - jerithom
select * from BloodTransfusion;
##Inventory Table Insert and Delete Commands
#Section2. Code Authorship : Jerin Easo Thomas - jerithom
DELETE FROM Inventory;
#Section2. Code Authorship : Jerin Easo Thomas - jerithom
INSERT INTO Inventory (BloodBankId, BloodType, Quantity)
select A.BloodBankId, BloodType, sum(Quantity) as Quantity
from BloodBank A
INNER JOIN
BloodSample B
on A.BloodBankId = B.BloodBankId
group by 1,2;
#Section2. Code Authorship : Jerin Easo Thomas - jerithom
select * from Inventory;
## Nodal Table Insert and Delete Commands
#Section2. Code Authorship : Jerin Easo Thomas - jerithom
SELECT * FROM Nodal;
#Section2. Code Authorship : Jerin Easo Thomas - jerithom
delete from Nodal
where (OfficerName = '' or OfficerName = 'NA' or OfficerName = 'Dr.' or OfficerName = 'N/A');
#Section2. Code Authorship : Jerin Easo Thomas - jerithom
INSERT INTO Nodal
select Sr_No, Nodal_Officer, concat(Contact_Nodal_Officer, ', ', Mobile_Nodal_Officer) as OfficerContact, Email_Nodal_Officer, Qualification_Nodal_Officer
from blood_bank_mastertable;
## Data Insert to blood_bank_mastertable, the csv data for which is taken from Kaggle and inserted into BloodBank Table
#Section2. Code Authorship : Jerin Easo Thomas - jerithom
select * from blood_bank_mastertable;
#Section2. Code Authorship : Jerin Easo Thomas - jerithom
UPDATE blood_bank_mastertable SET Contact_No = CONCAT(Contact_No, ', ', Mobile);
#Section2. Code Authorship : Jerin Easo Thomas - jerithom
select Contact_No, Mobile, concat(Contact_No, ', ', Mobile) as Contact from blood_bank_mastertable;
## ---------Blood Donation process.-----------
##Initially the donor would have to create an account and the data for which would be inserted in the donor table.
#Section2. Code Authorship : Jerin Easo Thomas - jerithom
INSERT INTO Donor (DonorId, Name, Contact, Email, BloodType, DOB, Gender, Address)
VALUES (2, 'Shreya Varghese', 8256748193, '[email protected]', 'A+', '1999-04-17', 'Female', 'Thane, Mumbai, Maharashtra, India'),
(3, 'Chaitrali Ghanekar', 9215674712, '[email protected]', 'B+', '2000-04-20', 'Female', 'Karvenagar, Pune, Maharashtra, India'),
(4, 'Sushant Menon', 982279745, 'sushantmenon.com', 'A+', '2000-01-19', 'Male', 'Dandibeach, Surat, Gujarat, India'),
(5, 'Sakshi Sitoot', 936758123, 'ssitoot.com', 'AB+', '1997-10-31', 'Female', 'Meera Road, Surat, Maharashtra, India');
#Section2. Code Authorship : Jerin Easo Thomas - jerithom
INSERT INTO Donor (DonorId, Name, Contact, Email, BloodType, DOB, Gender, Address)
VALUES (6, 'Vicktor Dennis', 8192481950, '[email protected]', 'O+', '1997-06-12', 'Male', 'Camp, Pune, Maharashtra, India'),
(7, 'Yash Vibhute', 9876541236, '[email protected]', 'B+', '2000-06-14', 'Male', 'Chinchwad, Pune, Maharashtra, India'),
(8, 'Saurav', 8127784371, '[email protected]', 'AB-', '1998-10-07', 'Male', 'Hyderabad, India'),
(9, 'Atharv Bagade', 8135671034, '[email protected]', 'A-', '1998-11-24', 'Male', 'Andheri, Mumbai, Maharashtra, India'),
(10, 'Bhargave Desai', 9912674123, '[email protected]', 'O-', '1997-4-30', 'Male', 'Mumbai, Maharashtra, India');
## Once data is inserted the donor would have to crete an appointment to a particular hospital and the data for which
## would be inserted in Appointment table
## Below code is to insert records into appointment table for both Donor and Receipient, it also includes some update queries.
#Section2. Code Authorship : Jerin Easo Thomas - jerithom
ALTER TABLE Appointment DROP FOREIGN KEY appointment_ibfk_1;
#Section2. Code Authorship : Jerin Easo Thomas - jerithom
ALTER TABLE Appointment
ADD CONSTRAINT FOREIGN KEY (RecipientId) REFERENCES Recipient(RecipientId);
#Section2. Code Authorship : Jerin Easo Thomas - jerithom
ALTER TABLE Appointment
ADD CONSTRAINT appointment_ibfk_3
FOREIGN KEY (BloodBankId) REFERENCES BloodBank(BloodBankId);
#Section2. Code Authorship : Jerin Easo Thomas - jerithom
INSERT INTO Appointment (AppointmentId, DonorId, BloodBankId, Date, Time)
VALUES (1, 1, 1298, '2023-04-04', '16:00:00');
#Section2. Code Authorship : Jerin Easo Thomas - jerithom
INSERT INTO Appointment (AppointmentId, DonorId, BloodBankId, Date, Time)
VALUES (4, 3, 1271, '2023-02-14', '09:00:00'),
(5, 4, 1269, '2022-12-22', '16:15:00'),
(6, 5, 1287, '2022-11-21', '12:00:00'),
(7, 6, 1273, '2023-01-07', '10:45:00'),
(8, 7, 1271, '2023-03-25', '13:00:00'),
(9, 8, 1292, '2022-10-01', '15:30:00'),
(10, 9, 1301, '2023-2-07', '8:45:00'),
(11, 10, 1299, '2023-04-02', '10:15:00');
#Section2. Code Authorship : Jerin Easo Thomas - jerithom
select * from Appointment;
#Section2. Code Authorship : Jerin Easo Thomas - jerithom
delete from Appointment
where AppointmentID = 1;
#Section2. Code Authorship : Jerin Easo Thomas - jerithom
INSERT INTO Appointment (AppointmentId, DonorId, RecipientId, BloodBankId, Date, Time)
SELECT 2, 2, -999, BloodBankId, '2023-04-05', '13:00:00'
FROM BloodBank
WHERE BloodBankId = '1297'
LIMIT 1;
#Section2. Code Authorship : Jerin Easo Thomas - jerithom
SELECT constraint_name
FROM information_schema.table_constraints
WHERE table_name = 'Appointment'
AND constraint_type = 'FOREIGN KEY';
#Section2. Code Authorship : Jerin Easo Thomas - jerithom
select * from Donor;
## After appointment scheduling the Donor can now donot the blood at the respective Blood Bank and the data for the blood sample would be inserted in BloodSample table.
## Insert Queries provided below.
#Section2. Code Authorship : Jerin Easo Thomas - jerithom
INSERT INTO BloodSample (SampleId, DonorId, BloodBankId, CollectionDate, ExpirationDate, BloodType, Quantity)
VALUES (1, 1, 1298, '2023-04-04', '2023-07-03', 'B+', 1);
#Section2. Code Authorship : Jerin Easo Thomas - jerithom
INSERT INTO BloodSample (SampleId, DonorId, BloodBankId, CollectionDate, ExpirationDate, BloodType, Quantity)
VALUES (2, 2, 1297, '2023-03-31', '2023-06-30', 'A+', 2);
#Section2. Code Authorship : Jerin Easo Thomas - jerithom
INSERT INTO BloodSample (SampleId, DonorId, BloodBankId, CollectionDate, ExpirationDate, BloodType, Quantity)
VALUES (3, 3, 1271, '2023-03-31', '2023-06-30', 'A+', 2);
#Section2. Code Authorship : Jerin Easo Thomas - jerithom
INSERT INTO BloodSample (DonorId, BloodBankId, CollectionDate, ExpirationDate, BloodType, Quantity)
SELECT A.DonorId, A.BloodBankId, A.Date as CollectionDate, DATE_ADD(A.Date, INTERVAL 180 DAY) AS ExpirationDate, B.BloodType, 1 AS Quantity
FROM Appointment A
INNER JOIN
Donor B
ON A.DonorId = B.DonorId
where A.DonorId > 3;
#Section2. Code Authorship : Jerin Easo Thomas - jerithom
select * from BloodSample;
## After the data has been added in the BloodSample table the Invertory table is updated.
# ---------Blood tranfusion/receiving process--------
## Initially the recipeint would have to create and a check in the inventory if the required blood type is
## avaiable in the particular bank
#Section2. Code Authorship : Jerin Easo Thomas - jerithom
select * from Recipient;
#Section2. Code Authorship : Jerin Easo Thomas - jerithom
INSERT INTO Recipient (RecipientId, Name, Contact, Email, RequiredBloodType, DOB, Gender, Address)
VALUES (1, 'Ani Chatterji', 9998765381, '[email protected]', 'B+', '1998-01-21', 'Male', 'Baner, Pune, Maharashtra, India');
#Section2. Code Authorship : Jerin Easo Thomas - jerithom
INSERT INTO Recipient (RecipientId, Name, Contact, Email, RequiredBloodType, DOB, Gender, Address)
VALUES (2, 'Sarfaraz', 9998765413, '[email protected]', 'A+', '1998-09-14', 'Male', 'Baner, Pune, Maharashtra, India');
## Inventory view
#Section2. Code Authorship : Jerin Easo Thomas - jerithom
create view ViewInventory as
select I.BloodBankId,B.Name,B.Contact,B.State, B.City, I.BloodType, I.Quantity
from Inventory I
inner join
BloodBank B
on I.BloodBankId = B.BloodBankId;
## Based on the inventory the recipient as to book an appointment at the Blood Bank.
#Section2. Code Authorship : Jerin Easo Thomas - jerithom
select * from Appointment where AppointmentID = 2;
#Section2. Code Authorship : Jerin Easo Thomas - jerithom
select * from ViewInventory where BloodBankId = 1297;
## After making an appointment an entry to BloodTransfusion would be made with status as Initiated.
#Section2. Code Authorship : Jerin Easo Thomas - jerithom
INSERT INTO BloodTransfusion(BloodBankId, RecipientId, SampleId, TransfusionDate, BloodType, TransfusedQuantity, TransfusionStatus)
select A.BloodBankId,A.RecipientId,B.SampleId, A.Date, R.RequiredBloodType, 1 as TransfusedQuantity, 'Initiated' as TransfusionStatus
from Appointment A
inner join
Recipient R
inner join (select SampleId from BloodSample where BloodBankId = '1297' limit 1) B
on A.RecipientId = R.RecipientId
where A.RecipientId = 2;
#Section2. Code Authorship : Jerin Easo Thomas - jerithom
select * from BloodTransfusion;
## Once Blood has been Tranfused the hospital user can update the Sample quatity
#Section2. Code Authorship : Jerin Easo Thomas - jerithom
select * from BloodSample;
#Section2. Code Authorship : Jerin Easo Thomas - jerithom
update BloodSample set Quantity = 1
where BloodBankId = 1297;
## Once Sample has been update then update the Inventory Table.
#Section2. Code Authorship : Jerin Easo Thomas - jerithom
select * from Inventory;
#Section2. Code Authorship : Jerin Easo Thomas - jerithom
DELETE FROM Inventory;
#Section2. Code Authorship : Jerin Easo Thomas - jerithom
INSERT INTO Inventory (BloodBankId, BloodType, Quantity)
select A.BloodBankId, BloodType, sum(Quantity) as Quantity
from BloodBank A
INNER JOIN
BloodSample B
on A.BloodBankId = B.BloodBankId
group by 1,2;
#Section2. Code Authorship : Jerin Easo Thomas - jerithom
select * from ViewInventory;
##------- Section 3. Views created------
-- Section 3. Code Authorship : Aniruddho Swapan Chatterjee - caswapan
# Section 3. Code Authorship : Aniruddho Swapan Chatterjee - caswapan
CREATE OR REPLACE VIEW INVENTORYVIEW AS
# Section 3. Code Authorship : Aniruddho Swapan Chatterjee - caswapan
SELECT bb.BloodBankId,bb.name,bb.contact,bb.helpline,bb.email
,bb.address,bb.state,bb.state,bb.district,bb.city,bb.pincode,bb.website,inv.BloodType,inv.Quantity
FROM BloodBank bb
join inventory inv
on bb.BloodBankId = inv.BloodBankId;
-- View to check appointments of Donors
# Section 3. Code Authorship : Aniruddho Swapan Chatterjee - caswapan
CREATE OR REPLACE VIEW DonorAppointmentView AS
# Section 3. Code Authorship : Aniruddho Swapan Chatterjee - caswapan
SELECT don.name,don.contact,don.BloodType,don.gender,app.appointmentDate,app.appointmentTime
from Appointment app
join Donor don
on app.donorId=don.donorId;
-- View to check appointments of Recipients
# Section 3. Code Authorship : Aniruddho Swapan Chatterjee - caswapan
CREATE OR REPLACE VIEW DonorAppointmentView AS
# Section 3. Code Authorship : Aniruddho Swapan Chatterjee - caswapan
SELECT rec.name,rec.contact,rec.BloodType,rec.gender,rec.appointmentDate,rec.appointmentTime
from Appointment app
join Recipients rec
on app.recipientId=rec.recipientId;
-- Update to be called from Blood Bank Employees while collecting blood samples from Donor.
# Section 3. Code Authorship : Aniruddho Swapan Chatterjee - caswapan
update BloodSample set CollectionDate=curdate(),ExpirationDate=curdate()+180,BloodType=bloodType
,HaemoglobinLevel=Hlevel,RBCCount=RBCCnt,WBCCount=WBCCnt,Quantity=BloodQuantityDonated
where SampleID=sampleId and DonorId=donorId and bloodBankId=bloodBankId;
-- Update to be called when the the blood has been collected from the donor to increase the inventory and show the same to recipients
# Section 3. Code Authorship : Aniruddho Swapan Chatterjee - caswapan
update Inventory set Quantity=Quantity+BloodQuantityDonated where BloodBankId=bloodBankId;