Sub topic 7
Constants, Defaults, and NOT NULL
Assume you already know basic SQL and can run an anonymous PL/SQL block in SQL*Plus, SQL Developer, or SQLcl. This chapter focuses on declarations inside the DECLARE section.
PL/SQL variables can start with a value, require a value, or remain changeable throughout a block. Constants and NOT NULL variables help express those rules clearly.
Constants
A constant is a named value that cannot change after the block starts. Declare it with the CONSTANT keyword and initialize it immediately.
SET SERVEROUTPUT ON
DECLARE
c_tax_rate CONSTANT NUMBER(4,3) := 0.075;
v_price NUMBER(10,2) := 100;
v_total NUMBER(10,2);
BEGIN
v_total := v_price * (1 + c_tax_rate);
DBMS_OUTPUT.PUT_LINE('Total: ' || v_total);
END;
/The declaration gives c_tax_rate the value 0.075. This assignment is allowed only in the declaration. An executable statement such as c_tax_rate := 0.08; causes a compilation error.
Use constants for values that have a stable meaning inside the block, such as a tax rate, a status code, or a limit. A descriptive name also makes calculations easier to read.
Defaults and initial values
A variable declaration can include an initial value with := or a default value with DEFAULT. Both initialize the variable when the block begins.
SET SERVEROUTPUT ON
DECLARE
v_attempts NUMBER := 1;
v_status VARCHAR2(20) DEFAULT 'NEW';
BEGIN
DBMS_OUTPUT.PUT_LINE('Attempts: ' || v_attempts);
DBMS_OUTPUT.PUT_LINE('Status: ' || v_status);
v_attempts := v_attempts + 1;
v_status := 'PROCESSING';
DBMS_OUTPUT.PUT_LINE('Updated status: ' || v_status);
END;
/Unlike a constant, a normally declared variable can receive new values in the executable section. If you omit an initial value, the variable starts as NULL, which means it has no value. NULL is not the same as zero, an empty string, or a blank space.
The DEFAULT form can make the intent of a fallback value clear. The := form is also common, especially when the declaration looks like an assignment. Choose one style and use it consistently within a project.
| Declaration | Can change later? | Must be initialized? |
|---|---|---|
| `c_limit CONSTANT NUMBER := 10` | No | Yes |
| `v_status VARCHAR2(20) DEFAULT 'NEW'` | Yes | No |
| `v_count NUMBER NOT NULL := 0` | Yes | Yes |
NOT NULL variables
Adding NOT NULL states that a variable must always contain a value. Because the variable cannot begin as NULL, PL/SQL requires an initialization expression in its declaration.
SET SERVEROUTPUT ON
DECLARE
v_count NUMBER NOT NULL := 0;
v_name VARCHAR2(30) NOT NULL := 'Ada';
BEGIN
v_count := v_count + 1;
DBMS_OUTPUT.PUT_LINE(v_name || ' processed ' || v_count || ' item.');
END;
/This declaration is invalid because it does not provide an initial value:
v_count NUMBER NOT NULL;A later assignment of NULL also violates the rule and raises a runtime error. Check values before assigning them to a NOT NULL variable, especially when they come from a query or another variable.
Choosing between the rules
Use a constant when reassignment would be a programming mistake. Use a default when a variable should start with a useful value but may change. Use NOT NULL when the variable must never lose its value.
Exercise
What is wrong with c_rate CONSTANT NUMBER;, and how would you correct it for a five percent rate?
Show answer
A constant must be initialized when declared. Write c_rate CONSTANT NUMBER := 0.05;.