Linkedin_Articles

View on GitHub

PL/SQL Foundations: A Comprehensive Guide for Beginners and Experienced Developers

PL/SQL (Procedural Language/Structured Query Language) is Oracle Corporation’s procedural extension to SQL. It combines the data manipulation power of SQL with the procedural capabilities of programming languages, making it a cornerstone for database-driven applications.

Key Features of PL/SQL:

Advantages of Using PL/SQL:

Basic PL/SQL Block Structure

A PL/SQL block is the fundamental unit of the language. It consists of the following sections:

  1. DECLARE: For variable, constant, and cursor declarations (optional).
  2. BEGIN: For executable code.
  3. EXCEPTION: For handling runtime errors (optional).
  4. END: To conclude the block.

Example:

DECLARE
   v_employee_name VARCHAR2(50);
BEGIN
   SELECT first_name INTO v_employee_name 
   FROM employees
   WHERE employee_id = 101; 
   DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_employee_name);
EXCEPTION
   WHEN NO_DATA_FOUND THEN
      DBMS_OUTPUT.PUT_LINE('No such employee found.');
END;

Limitations to Note:

Data Types in PL/SQL

PL/SQL supports various data types to handle different kinds of data.

Scalar Data Types:

Example:

DECLARE
   v_salary NUMBER(10,2);
   v_first_name VARCHAR2(30);
   v_is_active BOOLEAN := TRUE;
BEGIN
   -- Use the variables in your logic
   NULL;
END;

Composite Data Types:

LOB and Reference Data Types:

Variables and Constants

Variables store data for processing, while constants store immutable values.

Example:

DECLARE
   v_department_name VARCHAR2(30);
   c_max_limit CONSTANT NUMBER := 100;
BEGIN
   -- Assign value to the variable
   v_department_name := 'HR';
   DBMS_OUTPUT.PUT_LINE('Department: ' || v_department_name);
END;

%TYPE and %ROWTYPE Attributes

These attributes dynamically link variables or records to the database schema, ensuring adaptability to schema changes.

%TYPE Example:

DECLARE
   v_employee_name employees.first_name%TYPE;
BEGIN
   SELECT first_name INTO v_employee_name 
   FROM employees 
   WHERE employee_id = 101; 
   DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_employee_name);
END;

%ROWTYPE Example:

DECLARE
   r_employee employees%ROWTYPE;
BEGIN
   SELECT * INTO r_employee 
   FROM employees 
   WHERE employee_id = 101;  
   DBMS_OUTPUT.PUT_LINE('Employee Name: ' || r_employee.first_name);
END;

Simple SELECT INTO Statements

The SELECT INTO construct allows fetching values into variables.

Example:

DECLARE
   v_salary employees.salary%TYPE;
BEGIN
   SELECT salary INTO v_salary 
   FROM employees 
   WHERE employee_id = 102; 
   DBMS_OUTPUT.PUT_LINE('Salary: ' || v_salary);
END;

Performance Tip:

Key Best Practices for PL/SQL Beginners

Example:

DECLARE
   c_tax_rate CONSTANT NUMBER := 0.18; -- Tax rate as constant
   v_total_amount NUMBER;
BEGIN
   v_total_amount := 1000 + (1000 * c_tax_rate);
   DBMS_OUTPUT.PUT_LINE('Total Amount: ' || v_total_amount);
END;