Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Revision on Triggers and Cursors

2 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

3 Solution Create or Replace Trigger roomsavailable After insert on booking For each row Begin Update HOTEL Set 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

4 Marking scheme Create or Replace Trigger roomsavailable 1 mark for correct call After insert 1 mark for correct timing on booking 1 mark for correct table For each row 1 mark for correct level call Begin Update HOTEL 2 marks for correct SQL Set AvailableRooms = AvailableRooms – :new.NumOfRooms ½ mark for use of :new Where HotelID = :new.HotelID; End; ½ mark for correct syntax

5 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]

6 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 name 2. 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

7 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 IDinteger; AvRmsinteger; TotRmsinteger; 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;

8 Cont …. Create or replace procedure peakavail as1 mark for declaration IDinteger; AvRmsinteger; TotRmsinteger; cursor hotels is select HotelID, AvailableRooms, TotalRooms from HOTEL; Percentinteger;2 mark for variable declarations Begin Open hotels;½ mark for opening curser Loop1 mark for loop call Fetch hotels into ID,avrms,totrms;1 mark for fetch in correct order Exit 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;


Download ppt "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."

Similar presentations


Ads by Google