Programming Questions
Please submit the CODE and OUTPUT as specified for all programming questions. You will submit this assignmentelectronically in the ASSESSMENT area of the Study Desk.
In this assignment, you will use the CAR HIRE database. The CAR HIRE database including appropriate data will be made available on the USQ Oracle server. You can query these tables as if they are in your own schema but you are not permitted to insert or update data in these tables. The specification for the CAR HIRE database is as follows:
The table descriptions appear below, including the column names, datatypes and the meanings for values in the columns. Familiarise yourself with the tables and the data.
Important Questions Restrictions.
A number of questions will impose which statements are allowed in the solution. They usually have to do with restricting the type of aggregate functions you are allowed to use for the solution. For example, they might specify that you are not to use the SUM function but instead sum the values using the loop operations. Please follow the restrictions closely. They are in place to make you better understand the operations and execution of the PL/SQL. You will be penalised for not following the restrictions.
Please read the questions carefully and make sure you format the output as requested.
I_CAR
I_CARGROUP
I_MODEL
I_CUSTOMER
I_BOOKING
Question 1 (25 marks)
Write an anonymous block that places a substitution variable (&) into a local variable of type varchar2. You will need to convert the types and round them to nearest tens unit. For example 84.4555 would be 84.5. You should check the value entered in the local variable and output different messages depending on the value provided. The anonymous block is a simple grade calculator with the following rules.
If value less than 44.5 output the following message “Resulting Grade for VALUE marks is ‘F’”
If value is between 44.5 and 49.4 output the following message “Resulting Grade for VALUE marks is ‘IS’”
If value is between 49.5 and 64.4 output the following message “Resulting Grade for VALUE marks is ‘C’”
If value between 64.5 and 74.4 output the following message “Resulting Grade for VALUE marks is ‘B’”
If value is between 74.5 and 84.4 output the following message “Resulting Grade for VALUE marks is ‘A’”
If value is between 84.5 and 100 output the following message “Resulting Grade for VALUE marks is ‘HD’”
If the value does not fall in the range of 0 – 100 print an error message “ONLY MARKS BETWEEN 0 AND 100 ACCEPTED)”
Note: The ‘VALUE’ in the message is to be replaced by the value of the substitution local variable value.
Single quotes are required around the grade results.
Sample run and output:
To be submitted:
Structured documented code to solve the problem listed.
Sample execution screens captures with output for following test values:
110, 44.45, 49.5, 70.07, 86.67
You will need to run your solution once for every test value and capture the resulting screen output.
Question 2 (25 marks)
Write an anonymous block that meets the following specification:
1. Using an explicit cursor, retrieve each row from the i_car table.
2. Create a local variable that inherits the structure of the i_car table (using %ROWTYPE attribute). Place each row returned into this variable.
3. Using an implicit cursor, calculate the number of bookings that belong to each car.
4. Display the car registration, car group name, model name and the number of bookings for all the cars that have two or more bookings.
5. If the car has no bookings sum up the number of cars without bookings.
6. At the end of the run display a message “There are VALUE cars that have no bookings recorded”.
Note: The VALUE is to be replaced with the total number of cars found during the process without bookings. The VALUE needs to be calculates during the explicit cursor execution.
You can only use one COUNT( ) statement for your solution in the body of the anonymous block.
To be submitted:
Structured documented code to solve the problem listed.
Screenshot of the output after executing your solution.
Question 3 (25 marks)
Create a bound/host (or global) variable named G_NEWCOST.
Write an anonymous block that declares two local variables: A3_COST and B3_COST. The data types of these two variables should be the same as the data type of the columncost ini_car table. Also declare a constant called DEPRECIATION with value 15.25 to be used for deprecation calculation.
In your BEGIN section, write two SELECT queries to obtain value of thecost column wherecar_group_name is ‘A3’ and ‘B3’ and assign the corresponding values to A3_COST and B3_COST variables.
Now use the IF-THEN statement to compare the two variables to find out which one has a highest value. Calculate the new cost by using constant DEPRECIATION to calculate the new valuetotal cost * ((100 – DEPRICATION)/100) to the higher value and assign the new value to bound/host variable G_NEWCOST.
Display a message in the following format from your code block: “The car group CARGROUP has total cost of $TOTALCOST.XX and has been depreciated to $G_NEWCOST.XX”
TheCARGROUP should display the highest car group (A3 or B3 depending on values).
TheTOTALCOST will be the total calculated in the SELECT query statement.
G_NEWCOST will be the derived value after decreasing the cost with the DEPRECIATION value. Format theG_NEWCOST value to 2 decimal places.
TheXX represent the decimal places in the format.
Using the PRINT command, output the value in the host variable (G_NEWCOST) after the anonymous block has run.
To be submitted:
Structured documented code to solve the problem listed.
Output screenshot after running the code with the message as requested.
Output screenshot after executing the PRINTG_NEWCOST statement.
Question 4 (25 marks)
Write a PL/SQL solution that displays the registration and the maximum and the minimum miles travelled by a car during the rental period for all the bookings where the booking has been paid for.
The output from the anonymous block will be two messages. One message will display the information for the maximum miles driven and one for the minimum miles driven in the following format.
“The maximum miles driven was MAXIMUM by car REGISTRATION over DAYS day’s”
“The minimum miles driven was MINIMUM by car REGISTRATION over DAYS day’s”
WhereMAXIMUM is the maximum miles calculated from the bookings table.
MINIMUM is the minimum miles calculated in the bookings table.
DAYS is the rental_period for the maximum and minimum miles travelled in the booking table.
Your program must use at least one explicit cursor and must NOT use any aggregate functions, for example SUM, MAX, COUNT, etc.
(Hint: miles driven for each booking will need to be calculated. Only the bookings where the booking has been paid for should be processed. All the information for the program is located in the i_booking table)
MARKING CRITERIA
1. The code produces the required output.
2. The code is appropriately structured and documented
3. The code addresses the specification.
4. The code provides a solution and covers every element in the specification.
5. The code has at least one explicit cursor
6. The code has no aggregate SQL functions.
7. The code adopts an optimal and sophisticated approach to PL/SQL.
To be submitted:
Structured documented code to solve the problem listed.
Screenshot of the output after executing your solution.
Solution Preview:
Assignment 1
Question 1 – PL/ SQL Code
SET SERVEROUTPUT ON
ACCEPT mark PROMPT “Enter value for Mark: “;
DECLARE
lv_marks varchar2(10);
msg VARCHAR2(60);
value NUMERIC(5,1);
grade VARCHAR2(2);
flag NUMBER(1);
BEGIN
lv_marks:= &mark;
flag:=0;
value := ROUND(lv_marks,1);
msg:=’Resulting Grade for ‘ || value || ‘ marks is ‘;
grade:=”;
IF value<44.5 THEN
grade:=’F’;
ELSIF value<49.5 THEN
grade:=’IS’;
ELSIF value<64.5 THEN
grade:=’C’;
ELSIF value<74.5 THEN
grade:=’B’;
ELSIF value<85.5 THEN
grade:=’A’;
ELSIF value<=100 THEN
grade:=’HD’;
ELSE
dbms_output.put_line(‘ONLY MARKS BETWEEN 0 AND 100 ACCEPTED’);
flag:=1;
END IF;
IF flag=0 THEN
dbms_output.put_line(msg || ”” || grade || ”” );
END IF;
END;
Question 2 – PL/SQL Code
SET SERVEROUTPUT ON
DECLARE
CURSOR car_table IS SELECT * FROM I_CAR;
car_record car_table %ROWTYPE;
totalcar NUMBER;
total NUMBER;
BEGIN
totalcar := 0;
total:=0;
FOR car_record in car_table
LOOP
totalcar:=0;
SELECT COUNT(*) INTO totalcar FROM I_BOOKING WHERE REGISTRATION = car_record.REGISTRATION;
IF totalcar =0 THEN
total:= total +1;
END IF;
dbms_output.put_line(‘Reg. Number : ‘ || car_record.REGISTRATION);
dbms_output.put_line(‘Car Group : ‘ || car_record.CAR_group_name);
dbms_output.put_line(‘Model Name : ‘ || car_record.MODEL_NAME );
dbms_output.put_line(‘Number of Booking : ‘ || totalcar);
dbms_output.put_line(”);
END LOOP;
dbms_output.put_line( ‘ There are ‘ || total || ‘ cars that have no bookings recorded ‘);
END;
Question 3 – PL/SQL Code
SET SERVEROUTPUT ON
DECLARE
A3_COST I_CAR.COST%TYPE;
B3_COST I_CAR.COST%TYPE;
G_NEWCOST NUMBER(15,2);
DEPRECIATION I_CAR.COST%TYPE;
CAR_GROUP I_CAR.CAR_GROUP_NAME%TYPE;
TOTAL_COST NUMBER(15,2);
BEGIN
DEPRECIATION := 15.25;
SELECT SUM(COST) INTO TOTAL_COST FROM I_CAR;
SELECT SUM(COST) INTO A3_COST FROM I_CAR WHERE CAR_GROUP_NAME=’A3′;
SELECT SUM(COST) INTO B3_COST FROM I_CAR WHERE CAR_GROUP_NAME=’B3′;
IF A3_COST > B3_COST THEN
CAR_GROUP :=’A3′;
ELSE
CAR_GROUP :=’B3′;
END IF;
G_NEWCOST := TOTAL_COST * ((100 – DEPRECIATION)/100);
dbms_output.put_line(‘The car group ‘ || CAR_GROUP || ‘ has total cost of ‘ || ROUND(TOTAL_COST,2) || ‘ and has been depreciated to ‘ || ROUND(G_NEWCOST,2));
END;
Question 4 – PL/ SQL Code
SET SERVEROUTPUT ON
DECLARE
CURSOR book_table IS SELECT * FROM I_BOOKING;
book_record book_table%ROWTYPE;
mini_mile I_BOOKING.MILES_IN%TYPE;
mini_regno I_BOOKING.REGISTRATION%TYPE;
max_mile I_BOOKING.MILES_IN%TYPE;
max_regno I_BOOKING.REGISTRATION%TYPE;
mile_in I_BOOKING.MILES_IN%TYPE;
mini_period I_BOOKING.RENTAL_PERIOD%TYPE;
max_period I_BOOKING.RENTAL_PERIOD%TYPE;
firsttime NUMBER(1);
BEGIN
mini_mile:=0;
max_mile:=0;
firsttime:=0;
FOR book_record in book_table
LOOP
mile_in := book_record.MILES_IN-book_record.MILES_OUT;
IF firsttime =0 THEN
mini_mile:=mile_in;
max_mile:=mile_in;
firsttime:=1;
END IF;
IF max_mile<mile_in THEN
max_mile:=mile_in;
max_regno:=book_record.REGISTRATION;
max_period:=book_record.RENTAL_PERIOD;
END IF;
IF mini_mile>=mile_in THEN
mini_mile:=mile_in;
mini_regno:=book_record.REGISTRATION;
mini_period:=book_record.RENTAL_PERIOD;
END IF;
END LOOP;
dbms_output.put_line(‘The maximum miles driven was ‘ || max_mile || ‘ by car ‘ || max_regno || ‘ over ‘ || max_period || ‘ day’ || ”” || ‘s’);
dbms_output.put_line(‘The minimum miles driven was ‘ || mini_period || ‘ by car ‘ || mini_regno || ‘ over ‘ || mini_period || ‘ day’ || ”” || ‘s’);
END;
What We Offer:
• On-time delivery guarantee
• PhD-level professionals
• Automatic plagiarism check
• 100% money-back guarantee
• 100% Privacy and Confidentiality
• High Quality custom-written papers