24
Final-Exam Revision Instructor: Mohamed Eltabakh [email protected] 1

Final-Exam Revision Instructor: Mohamed Eltabakh [email protected] 1

Embed Size (px)

Citation preview

Page 1: Final-Exam Revision Instructor: Mohamed Eltabakh meltabakh@cs.wpi.edu 1

Final-Exam Revision

Instructor: Mohamed Eltabakh [email protected]

1

Page 2: Final-Exam Revision Instructor: Mohamed Eltabakh meltabakh@cs.wpi.edu 1

Reminder…..Final Exam Dec. 13, at 8:15am – 9:30am (75 mins) Closed book, open sheet Answer in the same exam sheet

Material Included ERD SQL (Select, Insert, Update, Delete) Views, Triggers, Assertions Cursors, Stored Procedures/Functions

Material Excluded Relational Model & Algebra Normalization Theory ODBC/JDBC Indexes and Transactions

Page 3: Final-Exam Revision Instructor: Mohamed Eltabakh meltabakh@cs.wpi.edu 1

SQL Commands

3

Page 4: Final-Exam Revision Instructor: Mohamed Eltabakh meltabakh@cs.wpi.edu 1

4

Question 1

Page 5: Final-Exam Revision Instructor: Mohamed Eltabakh meltabakh@cs.wpi.edu 1

5

Question 2

Select the most expensive generic medicineSelect the most expensive generic medicine

Select * From MedicineWhere UnitPrice = (

Select max(unitPrice)From MedicineWhere genericFlag = ‘T’)

And genericFlag = ‘T’;

Select * From MedicineWhere UnitPrice = (

Select max(unitPrice)From MedicineWhere genericFlag = ‘T’)

And genericFlag = ‘T’;

Page 6: Final-Exam Revision Instructor: Mohamed Eltabakh meltabakh@cs.wpi.edu 1

6

Question 2: Another way

Select the most expensive generic medicineSelect the most expensive generic medicine

Select * From ( Select *

From MedicineWhere genericFlag = ‘T’Order By UnitPrice Desc)

Where rownum = 1;

Select * From ( Select *

From MedicineWhere genericFlag = ‘T’Order By UnitPrice Desc)

Where rownum = 1;

Use of rownum(Oracle specific)

Must be in outer select after sorting

Page 7: Final-Exam Revision Instructor: Mohamed Eltabakh meltabakh@cs.wpi.edu 1

7

Question 3

Page 8: Final-Exam Revision Instructor: Mohamed Eltabakh meltabakh@cs.wpi.edu 1

8

Use of rownum(Oracle specific)

Question 3: Another Way

Page 9: Final-Exam Revision Instructor: Mohamed Eltabakh meltabakh@cs.wpi.edu 1

9

Question 4

Delete prescription lines for prescriptions written on date ‘Jan-01-2010’Delete prescription lines for prescriptions written on date ‘Jan-01-2010’

Delete From Prescription_MedicineWhere prescription_id in ( Select id From Prescription Where date = ‘Jan-01-2010’);

Delete From Prescription_MedicineWhere prescription_id in ( Select id From Prescription Where date = ‘Jan-01-2010’);

Page 10: Final-Exam Revision Instructor: Mohamed Eltabakh meltabakh@cs.wpi.edu 1

10

Question 5

Delete prescriptions that have no lines (no records in prescription_medicine)Delete prescriptions that have no lines (no records in prescription_medicine)

Delete From PrescriptionWhere id not in ( Select prescription_id From prescription_medicine);

Delete From PrescriptionWhere id not in ( Select prescription_id From prescription_medicine);

Page 11: Final-Exam Revision Instructor: Mohamed Eltabakh meltabakh@cs.wpi.edu 1

11

Question 6

Select patients who have no primary doctors Select patients who have no primary doctors

Select * From PatientWhere primaryDoctor_SSN is null;

Select * From PatientWhere primaryDoctor_SSN is null;

Page 12: Final-Exam Revision Instructor: Mohamed Eltabakh meltabakh@cs.wpi.edu 1

12

Question 7

Report the prescription id and its total cost of prescriptions having total cost between $100 and $200. Sort ascending based on the total cost

Report the prescription id and its total cost of prescriptions having total cost between $100 and $200. Sort ascending based on the total cost

Select prescription_id, sum(unitPrice * NumOfUnits) As totalCostFrom Medicine M, Prescription_Medicine PMWhere M.TradeName = PM.TradeNameGroup By prescription_idHaving totalCost > 100 And totalCost < 200Order By totalCost;

Select prescription_id, sum(unitPrice * NumOfUnits) As totalCostFrom Medicine M, Prescription_Medicine PMWhere M.TradeName = PM.TradeNameGroup By prescription_idHaving totalCost > 100 And totalCost < 200Order By totalCost;

Page 13: Final-Exam Revision Instructor: Mohamed Eltabakh meltabakh@cs.wpi.edu 1

13

Question 8

Double the number of units in prescription id 11111 and tradeName ‘Aspirin’. Double the number of units in prescription id 11111 and tradeName ‘Aspirin’.

Update Prescription_MedicineSet NumOfUnits = NumOfUnits * 2Where Prescription_id = 11111And tradeName = ‘Aspirin’;

Update Prescription_MedicineSet NumOfUnits = NumOfUnits * 2Where Prescription_id = 11111And tradeName = ‘Aspirin’;

Page 14: Final-Exam Revision Instructor: Mohamed Eltabakh meltabakh@cs.wpi.edu 1

14

Question 9

For medicines in prescription 11111, double their number of units if is it currently below 5For medicines in prescription 11111, double their number of units if is it currently below 5

Update Prescription_MedicineSet NumOfUnits = NumOfUnits * 2Where Prescription_id = 11111And tradeName in (Select tradeName From prescription_medicine Where prescription_id = 11111 And numOfUnits < 5);

Update Prescription_MedicineSet NumOfUnits = NumOfUnits * 2Where Prescription_id = 11111And tradeName in (Select tradeName From prescription_medicine Where prescription_id = 11111 And numOfUnits < 5);

Page 15: Final-Exam Revision Instructor: Mohamed Eltabakh meltabakh@cs.wpi.edu 1

Advanced SQL Commands

15

Page 16: Final-Exam Revision Instructor: Mohamed Eltabakh meltabakh@cs.wpi.edu 1

16

Question 10

Create a view that reports the trade name, unit price, and the generic flag of the most expensive and cheapest medicines. Create a view that reports the trade name, unit price, and the generic flag of the most expensive and cheapest medicines.

Page 17: Final-Exam Revision Instructor: Mohamed Eltabakh meltabakh@cs.wpi.edu 1

17

Question 11

Create a view that reports the trade name, unit price, and the generic flag of the most expensive and cheapest medicines. Each record should have an indicator on whether it is the most expensive or cheapest.

Create a view that reports the trade name, unit price, and the generic flag of the most expensive and cheapest medicines. Each record should have an indicator on whether it is the most expensive or cheapest.

Create View MedicineView2 AS Select TradeName, unitPrice, genericFlag, ‘Max’ as typeFrom Medicine Where unitPrice in (Select max(unitPrice) price From Medicine)UnionSelect TradeName, unitPrice, genericFlag, ‘Min’ as typeFrom Medicine Where unitPrice in (Select min(unitPrice) price From Medicine);

Create View MedicineView2 AS Select TradeName, unitPrice, genericFlag, ‘Max’ as typeFrom Medicine Where unitPrice in (Select max(unitPrice) price From Medicine)UnionSelect TradeName, unitPrice, genericFlag, ‘Min’ as typeFrom Medicine Where unitPrice in (Select min(unitPrice) price From Medicine);

Page 18: Final-Exam Revision Instructor: Mohamed Eltabakh meltabakh@cs.wpi.edu 1

18

Question 12

Using the view created in Question 11, count the number of trade names having the most expensive price.Using the view created in Question 11, count the number of trade names having the most expensive price.

Select Count(*)From MedicineView2Where type = ‘Max’;

Select Count(*)From MedicineView2Where type = ‘Max’;

Page 19: Final-Exam Revision Instructor: Mohamed Eltabakh meltabakh@cs.wpi.edu 1

19

Question 13

Create an assertion that ensures that no prescription has a tradeName having number of units > 10 Create an assertion that ensures that no prescription has a tradeName having number of units > 10

Create Assertion NumUnitsBelowTen As( 10 >= All

(Select NumOfUnits From Prescription_Medicine)

);

Create Assertion NumUnitsBelowTen As( 10 >= All

(Select NumOfUnits From Prescription_Medicine)

);

Page 20: Final-Exam Revision Instructor: Mohamed Eltabakh meltabakh@cs.wpi.edu 1

20

Question 14

Create an assertion that ensures any primary doctor has at least 3 years of experienceCreate an assertion that ensures any primary doctor has at least 3 years of experience

Create Assertion NumUnitsBelowTen As( Not Exists

(Select * From doctor D, patient P Where D.SSN = P.PrimaryDoctor_SSN And YearsOfExperience < 3)

);

Create Assertion NumUnitsBelowTen As( Not Exists

(Select * From doctor D, patient P Where D.SSN = P.PrimaryDoctor_SSN And YearsOfExperience < 3)

);

Page 21: Final-Exam Revision Instructor: Mohamed Eltabakh meltabakh@cs.wpi.edu 1

21

Question 15

Create a stored function that takes a date as a parameter and returns the number of prescriptions on that dateCreate a stored function that takes a date as a parameter and returns the number of prescriptions on that date

Create Function NumPrescriptions (inDate IN date) Return int As temp int;Begin

Select count(*) into temp From prescription where date = inDate;

return temp;End;

Create Function NumPrescriptions (inDate IN date) Return int As temp int;Begin

Select count(*) into temp From prescription where date = inDate;

return temp;End;

Page 22: Final-Exam Revision Instructor: Mohamed Eltabakh meltabakh@cs.wpi.edu 1

22

Question 16

Using the function created in Question 15, report the prescriptions written on a date in which more than 10 prescriptions have been written Using the function created in Question 15, report the prescriptions written on a date in which more than 10 prescriptions have been written

Select *From prescriptionWhere NumPrescriotions(date) > 10;

Select *From prescriptionWhere NumPrescriotions(date) > 10;

Page 23: Final-Exam Revision Instructor: Mohamed Eltabakh meltabakh@cs.wpi.edu 1

23

Question 17

Create trigger that ensures that if the medicine is generic, then its unit price is below $100, and if it is not generic then its unit price >= $100Create trigger that ensures that if the medicine is generic, then its unit price is below $100, and if it is not generic then its unit price >= $100

Create Trigger UnitPrice

Before Insert Or Update On Medicine

For Each Row

Begin

IF (:new.GenericFlag = ‘T’ and :new.UnitPrice >= 100) Then

RAISE_APPLICATION_ERROR(-20004, ‘Price should be < $100’);

ELSIF (:new.GenericFlag = ‘F’ and :new.UnitPrice < 100) Then

RAISE_APPLICATION_ERROR(-20004, ‘Price should be >= $100’);

END IF;

End;

Page 24: Final-Exam Revision Instructor: Mohamed Eltabakh meltabakh@cs.wpi.edu 1

Evaluation Forms

Course Number: CS3431 Course Name: Database Systems I Term: B11

Instructor Name: Mohamed Eltabakh TAs

Name: Kenneth Loomis Name: Chiying Wang

24