Revision on Triggers and Cursors. Walk through of exam type question. Question 1. A trigger is...
-
Upload
beatrice-cunningham -
Category
Documents
-
view
212 -
download
0
Transcript of Revision on Triggers and Cursors. Walk through of exam type question. Question 1. A trigger is...
![Page 1: Revision on Triggers and Cursors. Walk through of exam type question. Question 1. A trigger is required to automatically update the number of rooms available.](https://reader036.fdocuments.us/reader036/viewer/2022072014/56649ea25503460f94ba6acc/html5/thumbnails/1.jpg)
Revision on Triggers and Cursors
![Page 2: Revision on Triggers and Cursors. Walk through of exam type question. Question 1. A trigger is required to automatically update the number of rooms available.](https://reader036.fdocuments.us/reader036/viewer/2022072014/56649ea25503460f94ba6acc/html5/thumbnails/2.jpg)
Walk through of exam type question.
Question 1.A trigger is required to
automatically update the number of rooms available at any hotel once a booking has been made. Using SQL write the trigger that would to perform this action.
HOTEL
:
availablerooms
HotelID
Booking
:
HotelID
![Page 3: Revision on Triggers and Cursors. Walk through of exam type question. Question 1. A trigger is required to automatically update the number of rooms available.](https://reader036.fdocuments.us/reader036/viewer/2022072014/56649ea25503460f94ba6acc/html5/thumbnails/3.jpg)
Solution Create or Replace Trigger roomsavailable
After insert on bookingFor each rowBegin
Update HOTELSet AvailableRooms
= AvailableRooms – :new.NumOfRooms Where HotelID = :new.HotelID;
End;
1. Call the create command and give the trigger a sensible name
2. State the timing o the trigger
3. What table is going to instigate the trigger
4. Row level or table level trigger
5. SQL commands
![Page 4: Revision on Triggers and Cursors. Walk through of exam type question. Question 1. A trigger is required to automatically update the number of rooms available.](https://reader036.fdocuments.us/reader036/viewer/2022072014/56649ea25503460f94ba6acc/html5/thumbnails/4.jpg)
Marking scheme Create or Replace Trigger roomsavailable 1 mark for correct callAfter insert 1 mark for correct timingon booking 1 mark for correct tableFor each row 1 mark for correct level callBegin
Update HOTEL 2 marks for correct SQLSet AvailableRooms= AvailableRooms – :new.NumOfRooms ½ mark for use of :newWhere HotelID = :new.HotelID;
End; ½ mark for correct syntax
![Page 5: Revision on Triggers and Cursors. Walk through of exam type question. Question 1. A trigger is required to automatically update the number of rooms available.](https://reader036.fdocuments.us/reader036/viewer/2022072014/56649ea25503460f94ba6acc/html5/thumbnails/5.jpg)
Cursors (includes also procedures)
A report is required; this report will contain details of all the hotel availability for the coming summer season. An additional field has been added to the HOTEL table, this field is to contain summer status and will be referred to as PeakAvailablility. The new field has 4 potential values: GOOD, RESTRICTED, POOR & FULL this relates to the percentage of rooms that are taken. If less than 60% of rooms are available then the availability is GOOD, between 60% and 80% RESTRICTED, 81% - 99% POOR and 100% FULL.A cursor will be required to retrieve the hotel details in order to perform the calculations. Declare the cursor. [ 3 marks]
Write the extract of code which will call the cursor and calculate and then update the value of PeakAvailablility, You should use SQL or Plain English, NOTE if you use plain English you will receive a max of 4 marks [10 marks]
![Page 6: Revision on Triggers and Cursors. Walk through of exam type question. Question 1. A trigger is required to automatically update the number of rooms available.](https://reader036.fdocuments.us/reader036/viewer/2022072014/56649ea25503460f94ba6acc/html5/thumbnails/6.jpg)
Solution part a) + marking scheme
A cursor will be required to retrieve the hotel details in order to perform the calculations
1. Declare variable as cursor with sensible name2. State select statement for cursor results.
cursor hotel_rating is 1 mark for correct declaration
select HotelID, AvailableRooms, TotalRooms ½ mark for remembering is
from HOTEL 1 ½ marks for SQL commands
![Page 7: Revision on Triggers and Cursors. Walk through of exam type question. Question 1. A trigger is required to automatically update the number of rooms available.](https://reader036.fdocuments.us/reader036/viewer/2022072014/56649ea25503460f94ba6acc/html5/thumbnails/7.jpg)
Solution part b)
Write the code which will call the cursor and calculate and then update the value of PeakAvailablility. You should use PL/SQL.
Create or replace procedure peakavail as
ID integer;
AvRms integer;
TotRms integer;
cursor hotels is
select HotelID, AvailableRooms, TotalRooms from HOTEL;
Begin
Open hotels;
Loop
Fetch hotels into ID,avrms,totrms;
Exit when hotels%notfound;
SQL CODE
End loop;
End;
![Page 8: Revision on Triggers and Cursors. Walk through of exam type question. Question 1. A trigger is required to automatically update the number of rooms available.](https://reader036.fdocuments.us/reader036/viewer/2022072014/56649ea25503460f94ba6acc/html5/thumbnails/8.jpg)
Cont ….
Create or replace procedure peakavail as 1 mark for declaration
ID integer;
AvRms integer;
TotRms integer;cursor hotels isselect HotelID, AvailableRooms, TotalRooms from HOTEL; Percent integer; 2 mark for variable declarationsBegin
Open hotels; ½ mark for opening curserLoop 1 mark for loop callFetch hotels into ID,avrms,totrms; 1 mark for fetch in correct orderExit when hotels%notfound; 1 mark for exit criteria percent = (avrms/totrms) * 100 1 mark for calculating % available if (percent <= 60) then 2 marks for IF statement code update hotel set PeakAvailablility = ‘GOOD’ where hotelID = ID if (percent >60) && (percent <=80) then update hotel set PeakAvailablility = ‘RESTRICTED’ where hotelID = ID if (percent > 80) && (percent <=99) then update hotel set PeakAvailablility = ‘POOR’ where hotelID = ID if (percent = 100) then
update hotel set PeakAvailablility = ‘FULL’ where hotelID = ID
End loop; ½ mark for end loop command
End;