-
Notifications
You must be signed in to change notification settings - Fork 0
/
VenkataGayathriPeri PL.sql
417 lines (352 loc) · 13.1 KB
/
VenkataGayathriPeri PL.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
--- VENKATA GAYATHRI PERI---
---BUS ADM 749 ASSIGNMENT 4----
--1. Write a function to calculate the commission a salesperson earned during a period.--
CREATE OR REPLACE FUNCTION CommissionEarned(
salespersonid IN NUMBER,
commission_start_date IN DATE,
commission_end_date IN DATE
) RETURN NUMBER
IS
total_commission NUMBER := 0;
FLAG NUMBER(3);
BEGIN
-- Check if the salesperson ID exists in the Employee table
SELECT COUNT(*) INTO FLAG
FROM EMPLOYEE
WHERE ID = salespersonid;
IF FLAG < 1 THEN
RAISE_APPLICATION_ERROR('-20001', 'Employee ' || salespersonid || ' does not exist!');
END IF;
-- Check if the salesperson ID exists in the Salesperson table
BEGIN
SELECT 1
INTO FLAG
FROM Salesperson
WHERE ID = salespersonid;
-- If no exception is raised, the salesperson ID exists in the Salesperson table
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR('-20002', 'Employee ' || salespersonid || ' is not a salesperson!');
END;
-- Sum the commission for sales within the specified period
SELECT NVL(SUM(ST.quantity * P.unitPrice * SP.commission / 100), 0)
INTO total_commission
FROM SALEITEM ST
JOIN PRODUCT P ON P.UPC = ST.UPC
JOIN SALE S ON S.INVOICENO = ST.INVOICENO
JOIN SALESPERSON SP ON SP.ID = S.SALESPERSON
WHERE S.SALESPERSON = salespersonid
AND S.INVOICEDATE BETWEEN commission_start_date AND commission_end_date;
RETURN total_commission;
EXCEPTION
WHEN OTHERS THEN
RAISE_APPLICATION_ERROR('-20003', 'An error occurred: ' || SQLERRM);
END CommissionEarned;
/
-- Use the function in queries
INSERT INTO Client (clientNo, name, salesperson)
VALUES (1001, 'Main St Hardware', 103);
INSERT INTO Client (clientNo, name, salesperson)
VALUES (1002, 'ABC Home Store', 104);
INSERT INTO Client (clientNo, name, salesperson)
VALUES (1003, 'City Hardware', 104);
INSERT INTO Client (clientNo, name, salesperson)
VALUES (1004, 'Western Hardware', 105);
INSERT INTO Client (clientNo, name, salesperson)
VALUES (1005, 'Central Store', 105);
INSERT INTO Sale
VALUES (124001, '10-NOV-2022', 103, 1001);
INSERT INTO Sale
VALUES (124002, '15-NOV-2022', 104, 1002);
INSERT INTO Sale
VALUES (124003, '20-NOV-2022', 104, 1003);
INSERT INTO Sale
VALUES (124004, '30-NOV-2022', 105, 1004);
INSERT INTO Sale
VALUES (124005, '10-DEC-2022', 105, 1005);
INSERT INTO SaleITEM
VALUES (124001, '234569', 10);
INSERT INTO SaleITEM
VALUES (124001, '235569', 10);
INSERT INTO SaleITEM
VALUES (124002, '234569', 10);
INSERT INTO SaleITEM
VALUES (124002, '338569', 50);
INSERT INTO SaleITEM
VALUES (124003, '235569', 20);
INSERT INTO SaleITEM
VALUES (124004, '238569', 10);
INSERT INTO SaleITEM
VALUES (124005, '236569', 20);
INSERT INTO SaleITEM
VALUES (124005, '237569', 10);
SELECT CommissionEarned(999, '01-JAN-2022', '31-DEC-2022') FROM DUAL;
SELECT CommissionEarned(101, '01-JAN-2022', '31-DEC-2022') FROM DUAL;
SELECT CommissionEarned(102, '01-JAN-2022', '31-DEC-2022') FROM DUAL;
SELECT CommissionEarned(103, '01-JAN-2022', '31-DEC-2022') FROM DUAL;
SELECT CommissionEarned(104, '01-JAN-2022', '31-DEC-2022') FROM DUAL;
SELECT CommissionEarned(105, '01-JAN-2022', '31-DEC-2022') FROM DUAL;
SELECT ID, CommissionEarned(ID, '01-NOV-2022', '30-NOV-2022') AS C_NOV_2013,
CommissionEarned(ID, '01-DEC-2022', '31-DEC-2022') AS C_DEC_2013,
CommissionEarned(ID, '01-JAN-2022', '31-DEC-2022') AS C2013
FROM Salesperson;
SELECT SUM(CommissionEarned(ID, '01-NOV-2022', '30-NOV-2022')) AS C_NOV_2013,
SUM(CommissionEarned(ID, '01-DEC-2022', '31-DEC-2022')) AS C_DEC_2013,
SUM(CommissionEarned(ID, '01-JAN-2022', '31-DEC-2022')) AS C2013
FROM Salesperson;
------------------------------------------------------------------------------------------------------------------------------------
--2. Write a trigger to check and enforce the following constraints when a new record is inserted into the Salesperson table: The supervision relationship between salespersons is hierarchical up to three levels--
CREATE OR REPLACE TRIGGER SupervisionConstraint
BEFORE INSERT ON Salesperson
FOR EACH ROW
DECLARE
hlevel NUMBER := 0;
mngr NUMBER := :NEW.manager;
mngr_count NUMBER;
BEGIN
-- Check if the manager ID exists in the Salesperson table
SELECT COUNT(*)
INTO mngr_count
FROM Salesperson
WHERE ID = mngr;
-- If the manager does not exist, return to let the foreign key constraint handle it
IF mngr_count = 0 THEN
RETURN;
END IF;
-- If the manager is NULL, no need to perform hierarchy checks
IF mngr IS NULL THEN
RETURN;
END IF;
-- Check if the supervisor ID is within the hierarchy up to three levels using a loop
WHILE mngr IS NOT NULL AND hlevel < 3 LOOP
SELECT manager INTO mngr
FROM Salesperson
WHERE ID = mngr;
hlevel := hlevel + 1;
END LOOP;
-- If the hierarchy level is greater than or equal to 3, raise an error
IF hlevel >= 3 THEN
RAISE_APPLICATION_ERROR(-20003, 'Integrity Constraint Violated: The supervision relationship is hierarchical up to three levels!');
END IF;
END;
/
--test the trigger---
--first insert into employees--
INSERT INTO Employee (ID, firstName, lastName, title, gender, officeNo, birthdate)
VALUES (
119, 'Adam', 'Baker', 'Sales Representative', 'M', 105, '7-JUL-1972'
);
INSERT INTO Employee (ID, firstName, lastName, title, gender, officeNo, birthdate)
VALUES (
120, 'Steve', 'Dickens', 'Sales Representative', 'M', 105, '7-AUG-1972'
);
INSERT INTO Employee (ID, firstName, lastName, title, gender, officeNo, birthdate)
VALUES (
121, 'Alana', 'Carlyle', 'Sales Representative', 'F', 105, '17-JUL-1992'
);
select * from Employee;
--now insert into Salesperson---
INSERT INTO Salesperson
VALUES (
119, 4142296541, 20, null
); --no error
INSERT INTO Salesperson
VALUES (
120, 4142296542, 20, 118
); --no error
INSERT INTO Salesperson
VALUES (
121, 4142296543, 20, 999
); -- parent key not found error is expected
INSERT INTO Salesperson
VALUES (
121, 4142296543, 20, 120
); -- here 117-118-120 heirarchy done.117 is manager of 118.
--118 is manager of 120. 117-118-120, already 3 levels. 120 cannot be a manager of anyone else. So trigger should work and display error
INSERT INTO Salesperson
VALUES (
121, 4142296543, 20, 118
); -- no error
SELECT * FROM Salesperson;
----------------------------------------------------------------------------------------------------------------------------------
----3. A salesperson is leaving CPHC. Write a procedure to make related changes in the database.
--Pass the following responsibilities from salesperson A to salesperson B: products authorized to sell, clients, sales, payments,
--backups, other salespersons A is a backup of, manager (if A was the manager of B), other salespersons A manages. Then delete salesperson A from the Employee and Salesperson tables. You can assume that all integrity constraints are enforced and do not need to consider them in this procedure.
CREATE OR REPLACE PROCEDURE Salesperson_Leave (
theSalesperson IN Salesperson.ID%TYPE,
substituteSalesperson IN Salesperson.ID%TYPE
) AS
EmpA INT;
EmpB INT;
SalesPersonA INT;
SalesPersonB INT;
BEGIN
-- Check if employee A exists in the Employee table
SELECT COUNT(*)
INTO EmpA
FROM Employee
WHERE ID = theSalesperson;
IF EmpA = 0 THEN
RAISE_APPLICATION_ERROR(-20005, 'Employee ' || theSalesperson || ' does not exist!');
END IF;
-- Check if employee B exists in the Employee table
SELECT COUNT(*)
INTO EmpB
FROM Employee
WHERE ID = substituteSalesperson;
IF EmpB = 0 THEN
RAISE_APPLICATION_ERROR(-20005, 'Employee ' || substituteSalesperson || ' does not exist!');
END IF;
-- Check if employee A is a salesperson in the Salesperson table
SELECT COUNT(*)
INTO SalesPersonA
FROM Salesperson
WHERE ID = theSalesperson;
IF SalesPersonA = 0 THEN
RAISE_APPLICATION_ERROR(-20006, 'Employee ' || theSalesperson || ' is not a salesperson!');
END IF;
-- Check if employee B is a salesperson in the Salesperson table
SELECT COUNT(*)
INTO SalesPersonB
FROM Salesperson
WHERE ID = substituteSalesperson;
IF SalesPersonB = 0 THEN
RAISE_APPLICATION_ERROR(-20006, 'Employee ' || substituteSalesperson || ' is not a salesperson!');
END IF;
-- Check if salesperson A and B are the same
IF theSalesperson = substituteSalesperson THEN
RAISE_APPLICATION_ERROR(-20007, 'The two salespersons cannot be the same person!');
END IF;
-- Pass products authorized to sell
DELETE FROM Sells
WHERE salesperson = theSalesperson
AND UPC IN
(
SELECT UPC
FROM Sells
WHERE salesperson = substituteSalesperson
);
UPDATE Sells
SET salesperson = substituteSalesperson
WHERE salesperson = theSalesperson;
-- Transfer clients from A to B
UPDATE Client
SET salesperson = substituteSalesperson
WHERE salesperson = theSalesperson;
-- Transfer sales from A to B
UPDATE Sale
SET salesperson = substituteSalesperson
WHERE salesperson = theSalesperson;
-- pass payments
UPDATE Payment
SET receivingEmployee = substituteSalesperson
WHERE receivingEmployee = theSalesperson;
-- Assuming a Backup table structure, transfer backups from A to B
-- The specific logic will depend on the Backup table's structure
-- Pass backups
DELETE FROM Backup
WHERE salesperson = theSalesperson AND backup = substituteSalesperson;
DELETE FROM Backup
WHERE salesperson = theSalesperson
AND backup IN
(
SELECT backup
FROM Backup
WHERE salesperson = substituteSalesperson
);
UPDATE Backup
SET salesperson = substituteSalesperson
WHERE salesperson = theSalesperson;
-- Pass other salespersons A is a backup of
DELETE FROM Backup
WHERE salesperson = substituteSalesperson AND backup = theSalesperson;
DELETE FROM Backup
WHERE backup = theSalesperson
AND salesperson IN
(
SELECT salesperson
FROM Backup
WHERE backup = substituteSalesperson
);
UPDATE Backup
SET backup = substituteSalesperson
WHERE backup = theSalesperson;
-- Update manager field if A was the manager of B
UPDATE Salesperson
SET manager =
(
SELECT manager
FROM Salesperson
WHERE ID = theSalesperson
)
WHERE ID = substituteSalesperson and manager = theSalesperson;
-- Update other salespersons A manages to B
UPDATE Salesperson
SET manager = substituteSalesperson
WHERE manager = theSalesperson;
-- Delete salesperson A from Salesperson and Employee tables
DELETE FROM Salesperson WHERE ID = theSalesperson;
DELETE FROM Employee WHERE ID = theSalesperson;
-- Commit the changes
COMMIT;
EXCEPTION
WHEN OTHERS THEN
RAISE;
END Salesperson_Leave;
/
-- execute the procedure
UPDATE Salesperson SET manager=105 WHERE ID=103;
UPDATE Salesperson SET manager=105 WHERE ID=104;
UPDATE Salesperson SET manager=102 WHERE ID=105;
INSERT INTO Backup
VALUES (102, 103);
INSERT INTO Backup
VALUES (102, 105);
INSERT INTO Backup
VALUES (103, 104);
INSERT INTO Backup
VALUES (103, 105);
INSERT INTO Backup
VALUES (104, 105);
INSERT INTO Backup
VALUES (105, 103);
INSERT INTO Backup
VALUES (105, 104);
INSERT INTO Backup
VALUES (105, 102);
INSERT INTO ClientEmployee
VALUES (1001, 101, 'Michael', 'Smith', 'Purchase Manager');
INSERT INTO ClientEmployee
VALUES (1002, 101, 'John', 'Kaplan', 'CFO');
INSERT INTO ClientEmployee
VALUES (1003, 101, 'Beth', 'Chen', 'Purchase Manager');
INSERT INTO ClientEmployee
VALUES (1004, 101, 'Linda', 'Jones', 'Purchase Manager');
INSERT INTO ClientEmployee
VALUES (1005, 901, 'Lisa', 'Garcia', 'Purchase Manager');
INSERT INTO Payment
VALUES (700001, 124001, 103, 1001, 101, '18-NOV-2022', 'Check', 17.16);
INSERT INTO Payment
VALUES (700002, 124002, 104, 1002, 101, '19-NOV-2022', 'Credit', 100);
INSERT INTO Payment
VALUES (700003, 124005, 105, 1005, 901, '12-DEC-2022', 'Check', 33.27);
SELECT ID, firstName, lastName FROM Employee;
SELECT * FROM Salesperson;
SELECT * FROM Backup;
SELECT * FROM Sells;
SELECT * FROM Client;
SELECT * FROM Sale;
SELECT * FROM Payment;
EXECUTE Salesperson_Leave (999, 103);
EXECUTE Salesperson_Leave (105, 999);
EXECUTE Salesperson_Leave (101, 103);
EXECUTE Salesperson_Leave (105, 101);
EXECUTE Salesperson_Leave (105, 105);
EXECUTE Salesperson_Leave (105, 103);
SELECT ID, firstName, lastName FROM Employee;
SELECT * FROM Salesperson;
SELECT * FROM Backup;
SELECT * FROM Sells;
SELECT * FROM Client;
SELECT * FROM Sale;
SELECT * FROM Payment;