Following is the diagram of different Oracle PL/SQL Data Types:

In this PL/SQL Data Types Tutorial , you will learn-

PL/SQL CHARACTER Data Type PL/SQL NUMBER Data Type PL/SQL BOOLEAN Data Type PL/SQL DATE Data Type PL/SQL LOB Data Type

PL/SQL CHARACTER Data Type

This data type basically stores alphanumeric characters in string format. The literal values should always be enclosed in single quotes while assigning them to CHARACTER data type. This character data type is further classified as follows:

CHAR Data type (fixed string size) VARCHAR2 Data type (variable string size) VARCHAR Data type NCHAR (native fixed string size) NVARCHAR2 (native variable string size) LONG and LONG RAW

PL/SQL NUMBER Data Type

This data type stores fixed or floating point numbers up to 38 digits of precision. This data type is used to work with fields which will contain only number data. The variable can be declared either with precision and decimal digit details or without this information. Values need not enclose within quotes while assigning for this data type.

The first declaration statement declared the variable ‘grade’ of CHAR data type with the maximum size of 1 byte (default value). The second declaration statement declared the variable ‘manager’ of CHAR data type with the maximum size of 10 and assigned the value ‘guru99’ which is of 6 bytes. Oracle will allocate the memory of 10 bytes rather than 6 bytes in this case.

The above declaration statement declared the variable ‘manager’ of VARCHAR2 data type with the maximum size of 10 and assigned the value ‘guru99’ which is of 6 bytes. Oracle will allocate memory of only 6 bytes in this case.

The above declaration statement declared the variable ‘manager’ of VARCHAR data type with the maximum size of 10 and assigned the value ‘guru99’ which is of 6 bytes. Oracle will allocate memory of only 6 bytes in this case. (Similar to VARCHAR2)

The above declaration statement declares the variable ‘native’ of NCHAR data type with the maximum size of 10. The length of this variable depends upon the (number of lengths) per byte as defined in the character set.

The above declaration statement declares the variable ‘Native_var’ of NVARCHAR2 data type with the maximum size of 10.

These are mainly used in the data dictionary. LONG data type is used to store character set data, while LONG RAW is used to store data in binary format. LONG RAW data type accepts media objects, images, etc. whereas LONG works only on data that can be stored using character set.

The above declaration statement declares the variable ‘Large_text’ of LONG data type and ‘Large_raw’ of LONG RAW data type.

  Note: Using LONG data type is not recommended by Oracle. Instead, LOB data type should be preferred. 

Syntax Explanation:

In the above, the first declaration declares the variable ‘A’ is of number data type with total precision 8 and decimal digits 2. The second declaration declares the variable ‘B’ is of number data type with total precision 8 and no decimal digits. The third declaration is the most generic, declares variable ‘C’ is of number data type with no restriction in precision or decimal places. It can take up to a maximum of 38 digits.

PL/SQL BOOLEAN Data Type

This data type stores the logical values. Oracle Boolean Data Type represents either TRUE or FALSE and mainly used in conditional statements. Values need not enclose within quotes while assigning for this data type. Syntax Explanation:

In the above, variable ‘Var1’ is declared as BOOLEAN data type. The output of the code will be either true or false based on the condition set.

PL/SQL DATE Data Type

This data type stores the values in date format, as date, month, and year. Whenever a variable is defined with DATE data type along with the date it can hold time information and by default time information is set to 12:00:00 if not specified. Values need to enclose within quotes while assigning for this data type. The standard Oracle time format for input and output is ‘DD-MON-YY’ and it is again set at NLS_PARAMETERS (NLS_DATE_FORMAT) at the session level. Syntax Explanation:

In the above, variable ‘newyear’ is declared as DATE data type and assigned the value of Jan 1st, 2015 date. The second declaration declares the variable current_date as DATE data type and assigned the value with current system date. Both these variable holds the time information.

PL/SQL LOB Data Type

This data type is mainly used to store and manipulate large blocks of unstructured data’s like images, multimedia files, etc. Oracle prefers LOB instead of the a LONG data type as it is more flexible than the LONG data type. The below are the few main advantage of LOB over LONG data type.

The number of column in a table with LONG data type is limited to 1, whereas a table has no restriction on a number of columns with LOB data type. The data interface tool accepts LOB data type of the table during data replication, but it omits LONG column of the table. These LONG columns need to be replicated manually. The size of the LONG column is 2GB, whereas LOB can store up to 128 TB. Oracle is constantly improving the LOB data type in each of their releases according to the modern requirement, whereas LONG data type is constant and not getting many updates.

So, it is always good to use LOB data type instead of the LONG data type. Following are the different LOB data types. They can store up to the size of 128 terabytes.

BLOB CLOB and NCLOB BFILE

Summary

We have covered the different simple data types that are available in PL/SQL along with their syntax. We will learn about complex data types in further topics.

In the above, variable ‘Binary_data’ is declared as a BLOB.

In the above, variable ‘Charac_data’ is declared as CLOB data type.