Back to blog
Databases

PL/SQL Packages: Organizing Code in Real Systems

Introduction

As systems grow, code becomes harder to manage.

Packages help organize everything into one place.

What a Package Does

A package groups:

  • procedures
  • functions
  • variables

into a single unit.

Basic Structure

CREATE OR REPLACE PACKAGE emp_pkg IS
   PROCEDURE hire_employee(p_id NUMBER, p_name VARCHAR2);
   FUNCTION get_salary(p_id NUMBER) RETURN NUMBER;
END emp_pkg;
/
CREATE OR REPLACE PACKAGE BODY emp_pkg IS
   PROCEDURE hire_employee(p_id NUMBER, p_name VARCHAR2) IS
   BEGIN
      INSERT INTO employees VALUES (p_id, p_name);
   END;

   FUNCTION get_salary(p_id NUMBER) RETURN NUMBER IS
      v_salary NUMBER;
   BEGIN
      SELECT salary INTO v_salary FROM employees WHERE id = p_id;
      RETURN v_salary;
   END;
END emp_pkg;
/

How This Looks in Practice

In a real system, you might have:

User package:

  • create user
  • disable user
  • reset password

Finance package:

  • calculate tax
  • process payments

Each package handles a specific part of the system.

Why This Helps

Packages:

  • keep code organized
  • make updates easier
  • allow reuse
  • hide internal logic

Only what is defined in the package interface is visible.

In Larger Systems

Instead of giving direct access to database tables, systems often expose only package functions.

This helps control how data is accessed and modified.

Conclusion

Packages are used to organize and control code as systems grow.

They make projects easier to manage and maintain.