Dbms All Module Reviewer
Dbms All Module Reviewer
DBMS_OUTPUT.PUT_LINE(SQL%ROWCOUNT
|| ' rows deleted.');
END;
Module 1 Summary
SQL in PL/SQL:
SELECT, DML, transaction control.
Data Manipulation:
INSERT, UPDATE, DELETE, MERGE.
Cursors:
Implicit (SQL%FOUND, SQL%ROWCOUNT).
Control Structures:
IF, CASE, Loops (BASIC, WHILE, FOR,
Nested).
Loop Labels:
Help control nested loops.
❌ No data found" (because the correct country name v_country_name countries.country_name%TYPE := 'Korea, South';
v_elevation countries.highest_elevation%TYPE;
is "Republic of Korea"). BEGIN
SELECT highest_elevation INTO v_elevation
FROM countries WHERE country_name = v_country_name;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Country name ' || v_country_name || ' not
found.');
END;
retrieved multiple rows." ✅ Handled Error Message: "No such department ID."
The RAISE_APPLICATION_ERROR Procedure
Non-Predefined Oracle Server Errors
Use Case: Assign custom error messages with error
Oracle errors without a predefined name.
codes.
Solution: Use PRAGMA EXCEPTION_INIT to assign a
name.
RAISE_APPLICATION_ERROR Procedure Syntax
ex. Handling ORA-01400 (NULL Value Insert) RAISE_APPLICATION_ERROR(error_number, message
DECLARE [, {TRUE | FALSE}]);
e_insert_excep EXCEPTION;
PRAGMA EXCEPTION_INIT(e_insert_excep, -1400); error_number: Between -20000 and -20999 (reserved
BEGIN for user-defined errors).
INSERT INTO departments (department_id, department_name) message: Custom error message (max 2048 bytes).
VALUES (280, NULL);
TRUE | FALSE:
EXCEPTION TRUE → Keeps previous errors.
WHEN e_insert_excep THEN FALSE (default) → Replaces previous errors.
DBMS_OUTPUT.PUT_LINE('INSERT FAILED: Cannot insert
NULL.');
END;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Outer: No data found');
END;
Module 3 Summary
Always handle exceptions to prevent program crashes.
Use named exceptions (NO_DATA_FOUND, TOO_MANY_ROWS) when possible.
Define user exceptions for business rules.
Use RAISE_APPLICATION_ERROR for meaningful error messages.
Use SQLCODE & SQLERRM for debugging.
Calling it:
BEGIN
greet_user('Alice');
END;
Module 4 Summary
Procedures modularize code and improve security.
Can accept IN, OUT, and IN OUT parameters.
Exception handling ensures robustness.
Positional, Named, and Mixed parameter passing allow flexibility.
Local subprograms improve modularity inside procedures.
Restrictions: Functions used in SQL statements must NOT: Functions Without Parameters
Contain DML (INSERT, UPDATE, DELETE). Some built-in functions like USER and SYSDATE do not
Use COMMIT or ROLLBACK. require parameters.
Modify global variables.
ex. Assigning SYSDATE to a Variable
Benefits & Restrictions of Functions DECLARE v_today DATE;
BEGIN
Benefits v_today := SYSDATE;
Allows for quick calculations in SQL queries. END;
Extends SQL functionality with custom processing.
Can encapsulate reusable logic. ex. Using a Function in a SQL Statement
Restrictions SELECT job_id, SYSDATE - hire_date FROM employees;
PL/SQL types do not fully align with SQL types (e.g.,
BOOLEAN is not valid in SQL).
Size differences exist between PL/SQL and SQL Choosing Between a Procedure and a Function
(VARCHAR2 in PL/SQL can be 32KB, but in SQL, it's
only 4KB). Use a Function when:
You need a return value in a query.
Differences Between Procedures and Functions The function does not modify data (e.g., computing
salaries, validating input).
Use a Procedure when:
You need to execute multiple actions (e.g.,
inserting/updating records).
A return value is not necessary.
Module 5 Summary
1. Functions must return exactly one value.
2. Functions are invoked in expressions, SQL statements, or subprograms.
3. Functions cannot perform DML when used in SQL statements.
4. Syntax structure:
a. CREATE FUNCTION function_name RETURN datatype
b. RETURN statement is mandatory.
5. Procedures vs. Functions:
a. Functions must return a value.
b. Procedures may or may not return a value.
c. Functions must have at least one RETURN statement.
Module 6 Summary
1. Packages group related PL/SQL subprograms, variables, cursors, and exceptions.
2. A package consists of a public specification (interface) and a private body
(implementation).
3. Public subprograms are declared in the package specification.
4. Private subprograms are declared in the package body and are not accessible
externally.
5. Package bodies can be recompiled independently of their specification.
6. Use package_name.subprogram_name to call a procedure or function from a
package.
7. Packages improve performance by reducing redundant loading and compilation.
Performance Issues
Too many triggers can slow down transactions.
Example: Using Conditional Predicates Best Practice: Keep trigger logic short and efficient.
CREATE OR REPLACE TRIGGER restrict_salary_changes
BEFORE INSERT OR UPDATE OR DELETE ON employees
FOR EACH ROW
BEGIN
IF INSERTING THEN
RAISE_APPLICATION_ERROR(-20202, 'No new
employees can be added.');
ELSIF UPDATING THEN
RAISE_APPLICATION_ERROR(-20203, 'Employee
records cannot be changed.');
ELSIF DELETING THEN
RAISE_APPLICATION_ERROR(-20204, 'Employees
cannot be deleted.');
END IF;
END;
Module 7 Summary