PLSQL

PL/SQL constants and Literals

PL/SQL constants and Literals

A constant is defined as that holds a value that is once declared, and does not change in the program. Its declaration specified its name, data type, and its value and also allocates storage for it. This declaration can also be imposed on the NOT NULL constraint. 

Declaring the constant.

It is declared by using the CONSTANT keyword. That requires an initial value and also does not allow that the value to be changed. See the example given below:

PI CONSTANT NUMBER := 3.141592654;

DECLARE 

-- constant declaration

pi constant number := 3.141592654;

-- other declarations

radius number (5,2);

diameter number (5,2);

area number (10,2)

BEGIN

-- processing

radius := 9;

diameter := radius*2;

area := pi*radius*radius;

-- output

dbms_output.put_line(‘’Radius : ‘ || radius);

dbms_output.put_line(‘’Diameter : ‘ || diameter);

dbms_output.put_line(‘’Area : ‘ || area);

END;

/

When we execute the above code, we get following result;

Radius: 9;

Diameter: 18;

Area: 254.4690049

PL/SQL procedure successfully completed.