0% found this document useful (0 votes)
8 views

PL - SQL Data Type

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

PL - SQL Data Type

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

What is PL/SQL Datatypes?

Data Types in PL/SQL are used to define how the data will be stored,
handled, and treated by Oracle during the data storage and
processing. Data types are associated with the specific storage format
and range constraints. In Oracle, each value or constant is assigned
with a data type.
The main difference between PL/SQL and SQL data types is, SQL
data type are limited to table column while the PL/SQL data types are
used in the PL/SQL blocks.

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

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

Data Type Description Syntax

CHAR This data type stores the grade CHAR;


string value, and the size of manager CHAR
the string is fixed at the time (10):= 'guru99';
of declaring the variable.
Syntax Explanation:
● Oracle would be blank-
padded the variable if ● The first declaration
the variable didn’t statement declared
occupy the entire size the variable ‘grade’
that has been declared of CHAR data type
for it, Hence Oracle will with the maximum
allocate the memory for size of 1 byte
declared size even if (default value).
the variable didn’t ● The second
occupy it fully. declaration
● The size restriction for statement declared
this data type is 1-2000 the variable
bytes. ‘manager’ of CHAR
● CHAR data type is data type with the
more appropriate to use maximum size of
where ever fixed the 10 and assigned
size of data will be the value ‘guru99’
handled. which is of 6 bytes.
Oracle will allocate
the memory of 10
bytes rather than 6
bytes in this case.
VARCHA This data type stores the manager
R2 string, but the length of the VARCHAR2(10) :=
string is not fixed.
‘guru99';
● The size restriction for Syntax Explanation:
this data type is 1-4000
bytes for table column ● The above
size and 1-32767 bytes declaration
for variables. statement declared
● The size is defined for the variable
each variable at the ‘manager’ of
time of variable VARCHAR2 data
declaration. type with the
● But Oracle will allocate maximum size of
memory only after the 10 and assigned
variable is defined, i.e., the value ‘guru99’
Oracle will consider which is of 6 bytes.
only the actual length of Oracle will allocate
the string that is stored memory of only 6
in a variable for bytes in this case.
memory allocation
rather than the size that
has been given for a
variable in the
declaration part.
● It is always good to use
VARCHAR2 instead of
CHAR data type to
optimize the memory
usage.
VARCHA This is synonymous with the manager
R VARCHAR2 data type. VARCHAR(10) :=
‘guru99';
● It is always a good
practice to use Syntax Explanation:
VARCHAR2 instead of
VARCHAR to avoid ● The above
behavioral changes. 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)
NCHAR This data type is same as native NCHAR(10);
CHAR data type, but the Syntax Explanation:
character set will of the
national character set. ● The above
declaration
● This character set can statement declares
be defined for the the variable ‘native’
session using of NCHAR data
NLS_PARAMETERS. type with the
● The character set can maximum size of
be either UTF16 or 10.
UTF8. ● The length of this
● The size restriction is 1- variable depends
2000 bytes. upon the (number
of lengths) per byte
as defined in the
character set.
NVARCH This data type is same as Native var
AR2 VARCHAR2 data type, but NVARCHAR2(10):='gu
the character set will be of
the national character set. ru99';
Syntax Explanation:
● This character set can
be defined for the ● The above
session using declaration
NLS_PARAMETERS. statement declares
● The character set can the variable
be either UTF16 or ‘Native_var’ of
UTF8. NVARCHAR2 data
● The size restriction is 1- type with the
4000 bytes. maximum size of
10.
LONG This data type is used to Large_text LONG;
and store large text or raw data Large_raw LONG
LONGRA up to the maximum size of RAW;
W 2GB.
Syntax Explanation:
● These are mainly used ● The above
in the data dictionary. declaration
● LONG data type is used statement declares
to store character set the variable
data, while LONG RAW
‘Large_text’ of
is used to store data in
binary format. LONG data type
● LONG RAW data type and ‘Large_raw’ of
accepts media objects, LONG RAW data
images, etc. whereas type.
LONG works only on Note: Using LONG data
data that can be stored type is not recommended
using character set. by Oracle. Instead, LOB
data type should be
preferred.

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.
A NUMBER(8,2);
B NUMBER(8);
C NUMBER;
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.
Var1 BOOLEAN;
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.
newyear DATE:='01-JAN-2015';
current_date DATE:=SYSDATE;
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.

You might also like