-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinal project.sql
273 lines (155 loc) · 7.78 KB
/
final project.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
-- SCHEMA
CREATE TABLE "Doners"(
"id" INT PRIMARY KEY AUTO_INCREMENT,
"first_name" TEXT,
"last_name" TEXT,
"date_of_birth" DATE,
"gender" TEXT,
"blood_group" TEXT,
"contact_number" TEXT,
"address" TEXT
);
CREATE TABLE "Recipients"(
"id" INT PRIMARY KEY AUTO_INCREMENT,
"first_name" TEXT,
"last_name" TEXT,
"date_of_birth" DATE,
"gender" TEXT,
"blood_group" TEXT,
"contact_number" TEXT,
"address" TEXT,
"medical_issue" TEXT
);
CREATE TABLE "Blood_donations"(
"id" INT PRIMARY KEY AUTO_INCREMENT,
"doner_id" INT,
"blood_group" TEXT,
"donation_date" DATETIME DEFAULT CURRENT_TIME,
"expiration_date" DATE,
"deleted" INT DEFAULT 0,
FOREIGN KEY ("doner_id") REFERENCES "Doners"("id")
);
CREATE TABLE "Blood_given"(
"id" INT PRIMARY KEY AUTO_INCREMENT,
"donation_id" INT,
"date_when_blood_given" DATETIME DEFAULT CURRENT_TIME,
"recipient_id" INT,
FOREIGN KEY ("recipient_id") REFERENCES "Recipients"("id"),
FOREIGN KEY ("donation_id") REFERENCES "Blood_donations"("id")
);
CREATE VIEW "Current_blood" AS
SELECT "id", "doner_id", "donation_date", "expiration_date"
FROM "Blood_donations" WHERE "deleted" = 0;
CREATE TRIGGER "delete_blood"
INSTEAD OF DELETE ON "Current_blood"
FOR EACH ROW
BEGIN
UPDATE "Blood_donations" SET "deleted" = 1
WHERE "id" = OLD."id";
END;
-- we can simply add data in main table rather then using trigger.
-- CREATE TRIGGER "add_blood"
-- INSTEAD OF INSERT ON "Current_blood"
-- FOR EACH ROW
-- BEGIN
-- INSERT INTO "Blood_donations"("id", "doner_id", "expiration_date")
-- VALUES (NEW."id", NEW."doner_id", NEW."expiration_date");
-- END;
CREATE INDEX "doners_index" ON "Doners"("blood_group");
CREATE INDEX "recipients_index" ON "Recipients"("blood_group");
-- INSERTING VALUES
INSERT INTO "Doners" ("id", "first_name", "last_name", "date_of_birth", "gender", "blood_group", "contact_number", "address")
VALUES
(1000, 'Zulqarnain', 'Ali', '2004-06-16', 'Male', 'B+', '03069407213', 'Sargodha'),
(1001, 'John', 'Doe', '1990-05-15', 'Male', 'A+', '1234567890', '123 Main St'),
(1002, 'Jane', 'Smith', '1985-09-20', 'Female', 'O-', '9876543210', '456 Oak Ave'),
(1003, 'Michael', 'Johnson', '1982-03-10', 'Male', 'AB+', '5558889999', '789 Pine Rd'),
(1004, 'Emily', 'Brown', '1995-07-25', 'Female', 'B-', '1112223333', '321 Elm Blvd'),
(1005, 'David', 'Wilson', '1988-11-18', 'Male', 'O+', '4445556666', '555 Cedar Ln'),
(1006, 'Sarah', 'Martinez', '1992-01-30', 'Female', 'A-', '9990001111', '777 Birch Dr'),
(1007, 'Chris', 'Garcia', '1980-08-05', 'Male', 'B+', '7778889999', '888 Maple Ave'),
(1008, 'Jennifer', 'Davis', '1987-04-12', 'Female', 'AB-', '3334445555', '999 Pine St'),
(1009, 'Daniel', 'Rodriguez', '1993-06-22', 'Male', 'A+', '6667778888', '222 Oak Blvd'),
(1010, 'Lisa', 'Miller', '1984-12-07', 'Female', 'O-', '2223334444', '111 Cedar Rd');
INSERT INTO "Recipients" ("id", "first_name", "last_name", "date_of_birth", "gender", "blood_group", "contact_number", "address", "medical_issue")
VALUES
(1000, 'Umair', 'Akram', '1970-01-06', 'Male', 'B+', '0300 9407213', 'Sargodha', 'Blood Deficiency'),
(1001, 'Mark', 'Thomas', '1977-02-28', 'Male', 'A+', '5551112222', '321 Oak St', 'Blood Deficiency'),
(1002, 'Michelle', 'Lee', '1965-11-10', 'Female', 'B-', '7773331111', '456 Pine Ave', 'Blood Deficiency'),
(1003, 'Kevin', 'Clark', '1980-09-15', 'Male', 'AB+', '9998887777', '789 Elm Rd', 'Blood Deficiency'),
(1004, 'Amanda', 'White', '1972-03-03', 'Female', 'O+', '3336669999', '654 Maple Blvd', 'Blood Deficiency'),
(1005, 'Brian', 'Young', '1983-07-20', 'Male', 'A-', '1114447777', '987 Birch Ave', 'Blood Deficiency'),
(1006, 'Laura', 'Moore', '1979-05-08', 'Female', 'B+', '2225558888', '741 Cedar Ln', 'Blood Deficiency'),
(1007, 'Scott', 'Taylor', '1968-12-18', 'Male', 'AB-', '8882225555', '852 Oak Dr', 'Blood Deficiency'),
(1008, 'Rachel', 'Allen', '1986-08-25', 'Female', 'O-', '4447770000', '963 Elm St', 'Blood Deficiency'),
(1009, 'Justin', 'Scott', '1974-01-12', 'Male', 'A+', '6669991111', '147 Pine Ave', 'Blood Deficiency'),
(1010, 'Samantha', 'King', '1981-06-30', 'Female', 'B-', '1234567890', '369 Maple Rd', 'Blood Deficiency');
INSERT INTO "Blood_donations" ("id", "doner_id", "blood_group", "expiration_date")
VALUES
(1000, 1000, 'B+', '2024-12-31'),
(1001, 1001, 'AB+', '2024-12-31'), ---- is me se "id" hatana, kyu ke id tto auto_increment is khud aa jaye gi
(1002, 1002, 'B+', '2024-08-15'),
(1003, 1003, 'B+', '2024-07-28'),
(1004, 1004, 'B+', '2024-08-30'),
(1005, 1005, 'B+', '2024-07-31'),
(1006, 1006, 'B+', '2023-09-31'),
(1007, 1007, 'B+', '2024-08-30'),
(1008, 1008, 'B+', '2024-09-30'),
(1009, 1009, 'B+', '2024-05-11'),
(1010, 1010, 'B+', '2024-08-31');
-- This data will be added when blood will be given to recipients, writing the query.
-- INSERT INTO "Blood_given" ("id", "donation_id", "recipient_id")
-- VALUES
-- (1000, 1000, 1000),
-- (1001, 1001, 1001),
-- (1002, 1002, 1002),
-- (1003, 1003, 1003),
-- (1004, 1004, 1004),
-- (1005, 1005, 1005),
-- (1006, 1006, 1006),
-- (1007, 1007, 1007),
-- (1008, 1008, 1008),
-- (1009, 1009, 1009),
-- (1010, 1010, 1010);
-- QUERIRES
-- ADD NEW DONER
INSERT INTO "Doners" ("first_name", "last_name", "date_of_birth", "gender", "blood_group", "contact_number", "address")
VALUES ('Zulqarnain', 'Ali', '2004-06-16', 'Male', 'B+', '03069407213', 'Sargodha');
-- ADD NEW RECIPIENT
INSERT INTO "Recipients" ("first_name", "last_name", "date_of_birth", "gender", "blood_group", "contact_number", "address", "medical_issue")
VALUES ('Umair', 'Akram', '1970-01-06', 'Male', 'B+', '03009407213', 'Sargodha', 'Blood Deficiency');
-- ADD NEW BLOOD DONATION BY SOMEONE
INSERT INTO "Blood_donations" ("doner_id", "blood_group", "expiration_date")
VALUES (1000, 'B+', '2024-12-31');
-- GIVE BLOOD TO PATIENT
BEGIN TRANSACTION; --(First we will delete the blood from curren_blood and then we will add this record in Blood_given)
DELETE FROM "Current_blood" WHERE "id" = 1002;
INSERT INTO "Blood_given" ("donation_id", "recipient_id")
SELECT 1001, 1002 ;
COMMIT;
-- Delete expired blood till today
DELETE FROM "Blood_donations"
WHERE "expiration_date" <= DATE('now');
-- people who donated blood more than one time
SELECT "Doners"."id", "Doners"."first_name", "Doners"."last_name", COUNT("Blood_donations"."id") AS donation_count
FROM "Doners"
JOIN "Blood_donations" ON "Doners"."id" = "Blood_donations"."doner_id"
GROUP BY "Doners"."id", "Doners"."first_name", "Doners"."last_name"
HAVING COUNT("Blood_donations"."id") > 1;
-- Calculate the total number of blood donations per blood group
SELECT "blood_group", COUNT(*) AS "total_donations"
FROM "Blood_donations"
GROUP BY "blood_group";
-- Find which doner's blood is donated to which recipient and on which date.
SELECT "Doners"."first_name", "Doners"."last_name",
"Recipients"."first_name", "Recipients"."last_name",
"Blood_donations"."donation_date"
FROM "Blood_donations"
JOIN "Doners" ON "Blood_donations"."doner_id" = "Doners"."id"
JOIN "Blood_given" ON "Blood_donations"."id" = "Blood_given"."donation_id"
JOIN "Recipients" ON "Blood_given"."recipient_id" = "Recipients"."id";
-- erDiagram
-- Doners ||--o{ Blood_Donations : donate_blood
-- Blood_Donations ||--o{ Blood_Given : record_data
-- Blood_Given }|..|{ Recipients : receive_blood
-- Blood_Donations }|..|{ Recipients : give_blood