PL/SQL Constants

Last Updated : 27 May, 2026

PL/SQL provides constants to store fixed values that cannot be changed during program execution. Constants improve code readability, reliability and maintainability.

  • They store fixed values that remain unchanged throughout the program.
  • They are declared using the CONSTANT keyword.
  • They help prevent accidental modification of values.
  • They improve code readability and reduce errors.

Syntax:

DECLARE
constant_name CONSTANT data_type := value;
BEGIN
-- Code block where the constant can be used
END;
  • constant_name: Name of the constant variable.
  • CONSTANT: Specifies that the value cannot be changed after initialization.
  • data_type: Defines the type of data stored in the constant.
  • value: Fixed value assigned to the constant.

Example 1: Using a Constant in a Simple Arithmetic Operation

Using a constant value of pi, this example calculates the area of a circle.

Query:

DECLARE
pi CONSTANT NUMBER := 3.14159;
radius NUMBER := 5;
area NUMBER;
BEGIN
area := pi * radius * radius;
DBMS_OUTPUT.PUT_LINE('Area of the circle: ' || area);
END;
/

Output:

Area of the circle: 78.53975
  • By using a constant in a simple arithmetic operation, pi is declared as a constant with the value 3.14159.
  • The radius is assigned the value 5 and the area of the circle is calculated using the formula pi * radius * radius.

Example 2: Using Constants in Conditional Logic

In this example, a constant value is used to check whether the user is eligible to vote or not.

Query:

DECLARE
min_age CONSTANT NUMBER := 18;
user_age NUMBER := 20;
BEGIN
IF user_age >= min_age THEN
DBMS_OUTPUT.PUT_LINE('User is eligible to vote.');
ELSE
DBMS_OUTPUT.PUT_LINE('User is not eligible to vote.');
END IF;
END;
/

Output:

User is eligible to vote.
  • min_age is declared as a constant with value 18 and user_age is assigned the value 20.
  • Since user_age is greater than or equal to min_age, the output displayed is "User is eligible to vote."

Example 3: Constant with String Data Type

In this example, a string constant is used to store the company name and display employee details.

Query:

DECLARE
company_name CONSTANT VARCHAR2(50) := 'Tech Innovators Inc.';
employee_name VARCHAR2(30) := 'John Doe';
BEGIN
DBMS_OUTPUT.PUT_LINE('Employee ' || employee_name || ' works at ' || company_name);

-- Trying to change company_name will cause an error
-- company_name := 'New Company';
END;
/

Output:

Employee John Doe works at Tech Innovators Inc.
  • company_name is declared as a constant string and employee_name is assigned the value "John Doe".
  • The program displays the message "Employee John Doe works at Tech Innovators Inc." using string concatenation.
Comment