0% found this document useful (0 votes)
121 views

Lab Activity 4 PDF

The document is a lab activity on handling exceptions in PL/SQL blocks. It contains 5 parts with multiple questions in each part. The questions demonstrate how to handle predefined exceptions like NO_DATA_FOUND and user-defined exceptions. Examples include selecting from a table, inserting duplicate records, updating records, and deleting records - each intended to trigger different types of exceptions.

Uploaded by

Eugene Torres
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
121 views

Lab Activity 4 PDF

The document is a lab activity on handling exceptions in PL/SQL blocks. It contains 5 parts with multiple questions in each part. The questions demonstrate how to handle predefined exceptions like NO_DATA_FOUND and user-defined exceptions. Examples include selecting from a table, inserting duplicate records, updating records, and deleting records - each intended to trigger different types of exceptions.

Uploaded by

Eugene Torres
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

COLLEGE OF COMPUTER STUDIES & MULTIMEDIA ARTS

CCS0029
(Database System with Administration)

LAB ACTIVITY

4
EXCEPTIONS

Sarte, Clarence
Name:
Torres, Eugene
Section:
B21
Professor:
Ma’am Fanny Almeniana

Database System with Administration Page 1 of


18
COLLEGE OF COMPUTER STUDIES & MULTIMEDIA ARTS

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;

Database System with Administration Page 2 of


18
COLLEGE OF COMPUTER STUDIES & MULTIMEDIA ARTS

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.

2. Enter the following PL/SQL block:

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.

Database System with Administration Page 3 of


18
COLLEGE OF COMPUTER STUDIES & MULTIMEDIA ARTS

Database System with Administration Page 4 of


18
COLLEGE OF COMPUTER STUDIES & MULTIMEDIA ARTS

Database System with Administration Page 5 of


18
COLLEGE OF COMPUTER STUDIES & MULTIMEDIA ARTS

3. Oracle Server Errors:

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.

Database System with Administration Page 6 of


18
COLLEGE OF COMPUTER STUDIES & MULTIMEDIA ARTS
DECLARE
v_language_id languages.language_id%TYPE;
v_language_name languages.language_name%TYPE;
BEGIN
SELECT language_id, language_name INTO v_language_id, v_language_name FROM languages WHERE
LOWER(language_name) LIKE '<substring%>'; -- for example 'ab%'

INSERT INTO languages(language_id, language_name) VALUES(80, null);


END;

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”.

Database System with Administration Page 7 of


18
COLLEGE OF COMPUTER STUDIES & MULTIMEDIA ARTS

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.

Database System with Administration Page 8 of


18
COLLEGE OF COMPUTER STUDIES & MULTIMEDIA ARTS

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.

Database System with Administration Page 9 of


18
COLLEGE OF COMPUTER STUDIES & MULTIMEDIA ARTS

PART – 2.

For the following exercises use a copy of the employees table. Create this copy by running the following SQL
statement:

CREATE TABLE excep_emps AS SELECT * FROM employees;

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

Database System with Administration Page 10 of


18
COLLEGE OF COMPUTER STUDIES & MULTIMEDIA ARTS
condition and display the corresponding SQLCODE and SQLERRM. Test your code three times, using
department_ids 20, 30, and 40.

Database System with Administration Page 11 of


18
COLLEGE OF COMPUTER STUDIES & MULTIMEDIA ARTS

Database System with Administration Page 12 of


18
COLLEGE OF COMPUTER STUDIES & MULTIMEDIA ARTS

Database System with Administration Page 13 of


18
COLLEGE OF COMPUTER STUDIES & MULTIMEDIA ARTS

Database System with Administration Page 14 of


18
COLLEGE OF COMPUTER STUDIES & MULTIMEDIA ARTS

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;

BEGIN v_dept_id := 40;


SELECT COUNT(*) INTO v_count FROM excep_emps WHERE department_id = v_dept_id;
DBMS_OUTPUT.PUT_LINE('There are ' || v_count || ' employees');
DELETE FROM excep_emps WHERE department_id = v_dept_id;
DBMS_OUTPUT.PUT_LINE(SQL%ROWCOUNT || ' employees were deleted');
ROLLBACK;

END;

Database System with Administration Page 15 of


18
COLLEGE OF COMPUTER STUDIES & MULTIMEDIA ARTS

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.

Database System with Administration Page 16 of


18
COLLEGE OF COMPUTER STUDIES & MULTIMEDIA ARTS

Database System with Administration Page 17 of


18
COLLEGE OF COMPUTER STUDIES & MULTIMEDIA ARTS

Database System with Administration Page 18 of


18

You might also like