-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCFG_Assignment.sql
More file actions
334 lines (234 loc) · 6.01 KB
/
Copy pathCFG_Assignment.sql
File metadata and controls
334 lines (234 loc) · 6.01 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
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
-- Assignment
# Core requirement 1: Create a relational database of your choice with minimum 5 tables
# First table
CREATE DATABASE Project;
USE Project;
# Creating table
CREATE TABLE Sales (
Rank_ID CHAR(6) NOT NULL PRIMARY KEY, # Setting primary key
Brand VARCHAR(20),
Generic VARCHAR(25),
Sales INT
);
# Inserting data
USE Project;
INSERT INTO Sales (Rank_ID, Brand, Generic, Sales) VALUES
("2021_1","Comirnaty","Tozinameran",59),
("2021_2","Humira","Adalimumab",21),
("2021_3","Spikevax","Elasomeran",18),
("2021_4","Keytruda","Pembrolizumab",17),
("2021_5","Eliquis","Apixaban",17),
("2022_1","Comirnaty","Tozinameran",41),
("2022_2","Spikevax","COVID Vaccine",22),
("2022_3","Humira","Adalimumab",22),
("2022_4","Keytruda","Pembrolizumab",21),
("2022_5","Paxlovid","Nirmatrelvir",19),
("2023_1","Keytruda","Pembrolizumab",24),
("2023_2","Comirnaty","Tozinameran",18),
("2023_3","Humira","Adalimumab",13),
("2023_4","Paxlovid","Nirmatrelvir",13),
("2023_5","Eliquis","Apixaban",13);
# Second table
# Creating table
USE Project;
CREATE TABLE Manufacturer (
Brand VARCHAR(20) NOT NULL PRIMARY KEY, # Setting the primary key
Manufacturer VARCHAR (20)
);
# Inserting data
Use Project;
INSERT INTO Manufacturer (Brand, Manufacturer) VALUES
("Keytruda","Merck"),
("Humira","AbbVie"),
("Paxlovid","Pfizer"),
("Spikevax","Moderna"),
("Comirnaty","Pfizer / BioNTech"),
("Eliquis","BMS / Pfizer");
# Third table
# Making table
USE Project;
CREATE TABLE Location (
Manufacturer VARCHAR (20) NOT NULL PRIMARY KEY, # Setting the primary key
Location VARCHAR(20)
);
# Inserting data into the table
USE Project;
INSERT INTO Location (Manufacturer, Location) VALUES
("Merck","Darmstadt"),
("AbbVie","Chicago"),
("Pfizer","New York"),
("Moderna","Massachusetts"),
("Pfizer / BioNTech","Mainz"),
("BMS / Pfizer","New York");
# Fourth table
# Making table
USE Project;
CREATE TABLE Patent (
Brand VARCHAR (20) NOT NULL PRIMARY KEY, # Setting the primary key
Patent_Expiry INT,
Indication VARCHAR(20)
);
# Inserting data into the table
USE Project;
INSERT INTO Patent (Brand, Patent_Expiry, Indication) VALUES
("Keytruda", 2028,"Cancer"),
("Humira", 2023,"Arthritis"),
("Paxlovid", 2027,"COVID"),
("Spikevax", 2029,"COVID"),
("Comirnaty", NULL ,"COVID"),
("Eliquis", 2027, "Vascular disease");
# Fifth table
# Making table
USE Project;
CREATE TABLE RankID (
Rank_ID CHAR(6) NOT NULL PRIMARY KEY, # Setting the primary key
Year INT,
Place INT
);
# Inserting data into the table
USE Project;
INSERT INTO RankID (Year, Place, Rank_ID) VALUES
("2021","1","2021_1"),
("2021","2","2021_2"),
("2021","3","2021_3"),
("2021","4","2021_4"),
("2021","5","2021_5"),
("2022","1","2022_1"),
("2022","2","2022_2"),
("2022","3","2022_3"),
("2022","4","2022_4"),
("2022","5","2022_5"),
("2023","1","2023_1"),
("2023","2","2023_2"),
("2023","3","2023_3"),
("2023","4","2023_4"),
("2023","5","2023_5");
# Core requirement 2: Set primary and foreign key constraints to create relations between the tables
-- Setting foreign key for Rank_ID in tables Sales and RankID
Use Project;
ALTER TABLE Sales
ADD CONSTRAINT
fk_Rank_ID
FOREIGN KEY
(Rank_ID)
REFERENCES
RankID
(Rank_ID);
-- Setting foreign key for Manufacturer in tables manufacturer and location
Use Project;
ALTER TABLE Manufacturer
ADD CONSTRAINT
fk_Manufacturer
FOREIGN KEY
(Manufacturer)
REFERENCES
Location
(Manufacturer);
-- Setting foreign key for Brand in tables Sales and Patent
Use Project;
ALTER TABLE Sales
ADD CONSTRAINT
fk_Brand
FOREIGN KEY
(Brand)
REFERENCES
Patent
(Brand);
-- Setting foreign key for Brand in tables Sales and manufacturer
Use Project;
ALTER TABLE Sales
ADD CONSTRAINT
fk_Brand_2
FOREIGN KEY
(Brand)
REFERENCES
Manufacturer
(Brand);
# Using any type of the joins create a view that combines multiple tables in a logical way
Use Project;
CREATE VIEW Top_Sellers
AS
SELECT Sales.Brand, Sales.Generic
FROM Sales
LEFT JOIN RankID
on Sales.Rank_ID = RankID.Rank_ID
WHERE RankID.Place = 1;
-- Core requirement 3: Prepare an example query with a subquery to demonstrate how to extract data from your DB for analysis
-- What are the indications of drugs sold over 20 billion?
Use Project;
SELECT Indication FROM Patent # Query
WHERE Brand in (
SELECT Brand # Subquery
FROM Sales
WHERE Sales >= 20);
-- Core requirement 4: In your database, create a stored function that can be applied to a query in your DB
-- Stored function to see whether product is still in patent
use Project;
DELIMITER //
CREATE FUNCTION In_Patent(
patentyear INT
)
RETURNS VARCHAR(20)
DETERMINISTIC
BEGIN
DECLARE patent_status VARCHAR(20);
IF patentyear > 2023 THEN
SET patent_status = 'In patent';
ELSEIF patentyear <= 2023 THEN
SET patent_status = 'Out of patent';
END IF;
RETURN (patent_status);
END//balance
DELIMITER ;
select * from Project.Patent;
SELECT
Brand,
Indication,
Patent_Expiry,
In_Patent(Patent_Expiry)
FROM Patent;
-- Advanced requirement 1: Create a stored procedure and demonstrate how it runs
# Stored procedure to change / update the patent of a drug
use Project;
SELECT *
FROM patent;
DELIMITER //
CREATE PROCEDURE UpdatePatent(
NewYear INT,
BrandID VARChAR(25))
BEGIN
UPDATE Patent
SET Patent_Expiry = NewYear
WHERE Brand = BrandID;
END //
DELIMITER ;
# Calling procedure to check it runs
CALL UpdatePatent(2040, "Comirnaty");
# Demonstrating that the new value has been inserted
Select * From Patent;
-- Advanced requirement 2; create an event and demonstrate how it runs
# Creating an event that will insert 5 new rankIDs into Sales every year
# Note I would have to update the year annually, e.g. 2025, 2026 etc
SET GLOBAL event_scheduler = ON;
Use Project;
DELIMITER //
CREATE EVENT AnnualUpdate
ON SCHEDULE EVERY 1 MINUTE
STARTS NOW()
DO BEGIN
INSERT INTO RankID(Rank_ID)
VALUES("2024_1"),
("2024_2"),
("2024_3"),
("2024_4"),
("2024_5");
INSERT INTO Sales(Rank_ID)
VALUES("2024_1"),
("2024_2"),
("2024_3"),
("2024_4"),
("2024_5");
END //
DELIMITER ;
Select * from Sales;
DROP EVENT AnnualUpdate;