Upload
ursula-bond
View
229
Download
0
Embed Size (px)
Citation preview
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
SQL Commands
3
4
Question 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’;
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
7
Question 3
8
Use of rownum(Oracle specific)
Question 3: Another Way
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’);
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);
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;
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;
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’;
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);
Advanced SQL Commands
15
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.
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);
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’;
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)
);
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)
);
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;
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;
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;
Evaluation Forms
Course Number: CS3431 Course Name: Database Systems I Term: B11
Instructor Name: Mohamed Eltabakh TAs
Name: Kenneth Loomis Name: Chiying Wang
24