Lab Activity 4 PDF
Lab Activity 4 PDF
CCS0029
(Database System with Administration)
LAB ACTIVITY
4
EXCEPTIONS
Sarte, Clarence
Name:
Torres, Eugene
Section:
B21
Professor:
Ma’am Fanny Almeniana
REQUIREMENTS:
• Screen shots (use snipping tool) of your code and output from SQL Developer.
• Paste your screen shots / answers after each questions.
_____________________________________________________________________________________________
PART – 1.
1. Run the following PL/SQL block, which tries to insert a new row (with department_id = 50) into the departments
table. What happens and why?
BEGIN
INSERT INTO departments (department_id, department_name, manager_id, location_id)
VALUES (50, 'A new department', 100, 1500); DBMS_OUTPUT.PUT_LINE('The new department was
inserted');
EXCEPTION
WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE ('An exception has occurred.');
END;
The INSERT statement failed because department_id 50 already exists and department_id is a unique
column. While the rest of the executable section was skipped the message ‘The new department was
inserted’ wasn't displayed and the WHEN OTHERS exception handler was executed.
DECLARE
v_employee_id employees.employee_id%TYPE;
v_last_name employees.last_name%TYPE;
BEGIN
SELECT employee_id, last_name INTO v_employee_id, v_last_name FROM employees
WHERE department_id = 10;
DBMS_OUTPUT.PUT_LINE('The SELECT was successful');
EXCEPTION
WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An exception has occurred');
END;
Modify the code to add two more exception handlers to trap the possible exceptions individually. Use
NO_DATA_FOUND and TOO_MANY_ROWS. Re-run the block three times, using 10, 20, and 30 as before. Write
your observation in each cases.
A. Add an exception handler to the following code to trap the following predefined Oracle Server errors:
NO_DATA_FOUND, TOO_MANY_ROWS, and DUP_VAL_ON_INDEX.
Note: Test your block twice using each of the following language substrings: ba, ce. There are several
language_names beginning with “Ba,” but none beginning with “Ce”.
B. Now test your block a third time using substring: al. There is exactly one language_name beginning with “Al”.
Note that language_id 80 (Arabic) already exists. Explain the output.
C. Now (keeping the substring as “al”), add a non_predefined exception handler to trap then encountered oracle
exception code. Rerun the code and explain the result.
PART – 2.
For the following exercises use a copy of the employees table. Create this copy by running the following SQL
statement:
4. Create a PL/SQL block that updates the salary of every employee to a new value of 10000 in a chosen
department. Include a user-defined exception handler that handles the condition where no rows are updated
and displays a custom message. Also include an exception handler that will trap any other possible error
5. Be careful incorporating DELETE statements in PL/SQL blocks. If you make a mistake, you may inadvertently
delete data that you didn't mean to delete.
A. Enter and run the following PL/SQL block using department_id = 40, and explain the output.
DECLARE
v_dept_id excep_emps.department_id%TYPE;
v_count NUMBER;
END;
Both the SELECT and DELETE statements were executed successfully, even though there were no
employees in department 40.
B. Modify your code to include two user-defined exception handlers, one to test whether SELECT returns a
value of 0, and the other to test if no rows were DELETED. Declare the exceptions and RAISE them
explicitly before trapping them in the EXCEPTION section. Test your modified block using department_id 40.