In Oracle's PL/SQL (Procedural Language/SQL), constants play an important role in maintaining the integrity and predictability of the values used throughout the program. Declaring the constants enhances the program readability and reduces the errors, it also provides the performance benefits in certain scenarios where the fixed value is repeatedly used.
In this article, we will explain PL/SQL constants in detail, including their characteristics and examples.
PL/SQL Constants
In PL/SQL, constants are declared with the help of the CONSTANT keyword, followed by data type and initialization of value. They are especially useful in scenarios where the fixed value is required multiple times but most remain unchanged to avoid unintended modifications or errors.
Characteristics of PL/SQL Constants:
- Immutable: Once initialized, their value cannot be changed further.
- Declaration: Constants must be assigned a value at the time of the declaration.
- Readability: Enhances the code clarity and makes it easier to understand and maintain.
- Performance: Reduces the overload of the repeated calculations or lookups by the storing the fixed value.
Syntax:
DECLARE
constant_name CONSTANT data_type := value;
BEGIN
-- Code block where the constant can be used
END;
Explanation:
- constant_name: The name of the constant should be a variable. The name must be meaningful and should follow the PL/SQL naming conventions.
- CONSTANT: The keyword is indicating that the value is assigned to constant cannot changed after the initialization.
- data_type: The data type of constant like NUMBER, VARCHAR2 or DATE.
- := : This is assignment operator which is used to initialize constant with the value.
- value: The value that is assigned to constant, which cannot be modified the later.
Example 1: Using a Constant in a Simple Arithmetic Operation
- By Using a Constant in a Simple Arithmetic Operation, pi is declared as the constant with value 3.14159 .
- The radius is assigned value 5, and the area of circle is calculated with the help of formula pi * radius^2 .
- The result of calculation is displayed withe the DBMS_OUTPUT.PUT_LINE .
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
Explanation:
- In the above output, the constant pi is used in formula and since it cannot be changed, it is ensure that consistent results for all the calculations involving it.
Example 2: Using Constants in Conditional Logic
In PL/SQL, constants are fixed values that cannot be changed once defined. In the example, min_age
is declared as a constant with a value of 18, representing the minimum voting age.
The conditional IF
statement checks if the user_age
(set to 20) is greater than or equal to min_age
, determining the user's eligibility to vote. Since user_age
is 20, the output is "User is eligible to vote."
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.
Explanation:
- In the above code, min_age is the constant with value 18, representing minimum voting age.
- user_age is assigned the value 20.
- A conditional statement is checks whether the user_age is greater than or equal to the min_age to determine the eligibility to the vote.
Example 3: Constant with String Data Type
- In given below example company_name is the constant with value Tech Innovators Inc. where Constant with String Data type is used.
- The employee_name is initialized with value John Doe .
- The program outputs the message that concatenates employee name and the name of the company.
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'; -- This would generate a compilation error
END;
/
Output:
Employee John Doe works at Tech Innovators Inc.
Explanation:
- Here, the constant company_name is ensure that company name is remain fixed throughout the program. Attempting to change the company_name would give the compilation error highlighting immutability of the constants.
Conclusion:
In conclusion, the PL/SQL constants are the essential feature that enhance the reliability, maintainability and readability of our code. By using the constants, developers can safeguard key values like configuration settings, thresholds and fixed parameters, make sure that the remain unchanged throughout the program.
This immutability prevents the accidental modifications which can lead to errors and unpredictable outcomes. Furthermore, constants can improve the performance by reducing the need to re-evaluate or repeatedly reference same fixed value during the execution.
Similar Reads
SQL | Constraints
SQL constraints are essential elements in relational database design that ensure the integrity, accuracy, and reliability of the data stored in a database. By enforcing specific rules on table columns, SQL constraints help maintain data consistency, preventing invalid data entries and optimizing que
5 min read
SQL DROP CONSTRAINT
In SQL, constraints are used to ensure data integrity and define rules for the data in our database tables. These rules include ensuring uniqueness, maintaining referential integrity, and validating data with conditions. By applying constraints such as primary key, foreign key, unique, and check con
4 min read
PL/SQL Comments
Oracle PL/SQL is an advanced procedural language designed to enhance SQL functionalities in Oracle Database environments. Comments can be used to make code more human-readable. It is a kind of note that programmers usually add while writing the code. In this article, we will learn about Comments in
4 min read
PL/SQL CHECK Constraint
In Oracle's PL/SQL, the CHECK constraint is an essential feature that enforces data integrity by ensuring that the values stored in a table's columns meet specific conditions. By applying logical rules to the data, this constraint helps maintain the validity and consistency of the database, preventi
4 min read
SQL | DEFAULT Constraint
In SQL, maintaining data integrity and ensuring consistency across tables is important for effective database management. One way to achieve this is by using constraints. Among the many types of constraints, the DEFAULT constraint plays an important role in automating data insertion and ensuring tha
3 min read
PL/SQL DEFAULT Constraint
In PL/SQL the DEFAULT constraint is used to automatically assign a default value to a column when an explicit value is not provided during the insertion of a new row. This feature is particularly useful for ensuring that columns always have a meaningful value even when the user or application provid
5 min read
Constants in LISP
In LISP all constants are global variables. Values of constant never change throughout the execution of the program. Defining constant in LISP: New global constants are defined using the DEFCONSTANT construct Syntax: (defconstant name initial-value-form "documentation-string") Example: Let's create
2 min read
Rust - Constants
Constants are the value that can not be changed after assigning them. If we created a constant, then there is no way of changing its value. The keyword for declaring constant is const. In Rust, constants must be explicitly typed. The below syntax is used to initialize a constant value: Syntax : cons
1 min read
PL/SQL INSERT INTO
PL/SQL (Procedural Language/Structured Query Language) is Oracle's procedural extension to SQL. It allows us to write complex queries and scripts that include procedural logic, control structures, and error handling. The INSERT INTO statement in PL/SQL is essential for adding new rows of data to tab
6 min read
SciPy - Constants
Scipy stands for Scientific Python and in any Scientific/Mathematical calculation, we often need universal constants to carry out tasks, one famous example is calculating the Area of a circle = 'pi*r*r' where PI = 3.14... or a more complicated one like finding forcegravity = G*M*m â (distance)2 wher
3 min read