You are on page 1of 13

SANJIVANI K. B. P.

POLYTECHNIC, KOPARGAON
With NBA ACCREDIATED programs , Approved by AICTE, New Delhi,
Recognized by Govt. of Maharashtra, Affiliated to Maharashtra State Board of Technical Education,
Mumbai, ISO 9001:2015 Certified Institute

Department:- Computer Technology Class:- CM3I - A

Name of Subject:- Database Management System MSBTE Subject Code:- 22319


 The Objectives of today’s lecture are:
To Study
• PL/SQL Data Types
• Variables and Constants

 The Outcomes of today’s lecture are:


• You will be able to understand PL/SQL Data Types, Variables and
Constants.

Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology M. V. Khasne 2


4.1 Introduction to PL/SQL(Contd…)

Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology M. V. Khasne 3


PL/SQL Data Types:
 PL/SQL variables, constants, and parameters must have a valid data type
which specifies a storage format, constraints, and valid range of values.
 PL/SQL Data Types and Subtypes come under the following categories:

Data Type Description


Numeric Numeric values, on which arithmetic operations are performed.
Alphanumeric values that represent single characters or strings of
Character
characters.
Boolean Logical values, on which logical operations are performed.
Datetime Dates and times.

Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology M. V. Khasne 4


PL/SQL Numeric Data Types and Subtypes :
 Following is the detail of PL/SQL pre-defined Numeric data types and their sub-
types:
Data Type Description
PLS_INTEGER Signed integer in range -2,147,483,648 through 2,147,483,647, represented in 32 bits
BINARY_INTEGER Signed integer in range -2,147,483,648 through 2,147,483,647, represented in 32 bits
BINARY_FLOAT Single-precision IEEE 754-format floating-point number
BINARY_DOUBLE Double-precision IEEE 754-format floating-point number
Fixed-point or floating-point number with absolute value in range 1E-130 to (but not including)
NUMBER(prec, scale)
1.0E126. A NUMBER variable can also represent 0.
DEC(prec, scale) ANSI specific fixed-point type with maximum precision of 38 decimal digits.
DECIMAL(prec, scale) IBM specific fixed-point type with maximum precision of 38 decimal digits.
NUMERIC(pre, scale) Floating type with maximum precision of 38 decimal digits.
ANSI specific floating-point type with maximum precision of 126 binary digits (approximately
DOUBLE PRECISION
38 decimal digits)
FLOAT ANSI and IBM specific floating-point type with maximum precision of 126 binary digits
INT ANSI specific integer type with maximum precision of 38 decimal digits
INTEGER ANSI and IBM specific integer type with maximum precision of 38 decimal digits
SMALLINT ANSI and IBM specific integer type with maximum precision of 38 decimal digits
REAL Floating-point type with maximum precision of 63 binary digits (approx. 18 decimal digits)

Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology M. V. Khasne 5


PL/SQL Character Data Types and Subtypes :
 Following is the detail of PL/SQL pre-defined Character data types and their
sub-types:

Data Type Description


CHAR Fixed-length character string with maximum size of 32,767 bytes
VARCHAR2 Variable-length character string with maximum size of 32,767 bytes
Variable-length binary or byte string with maximum size of 32,767 bytes, not
RAW
interpreted by PL/SQL
NCHAR Fixed-length national character string with maximum size of 32,767 bytes
NVARCHAR2 Variable-length national character string with maximum size of 32,767 bytes
LONG Variable-length character string with maximum size of 32,760 bytes
Variable-length binary or byte string with maximum size of 32,760 bytes, not
LONG RAW
interpreted by PL/SQL
ROWID Physical row identifier, the address of a row in an ordinary table

Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology M. V. Khasne 6


PL/SQL Boolean Data Types :
 The BOOLEAN data type stores logical values that are used in logical
operations. The logical values are the Boolean values TRUE and FALSE and the
value NULL.
 However, SQL has no data type equivalent to BOOLEAN. Therefore Boolean
values cannot be used in:
• SQL statements
• Built-in SQL functions (such as TO_CHAR)
• PL/SQL functions invoked from SQL statements

Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology M. V. Khasne 7


PL/SQL Datetime and Interval Types:
 The DATE data type to store fixed-length datetimes, which include the time of
day in seconds since midnight. Valid dates range from January 1, 4712 BC to
December 31, 9999 AD. The following table shows the valid values for each
field:
Field Name Valid Datetime Values Valid Interval Values
YEAR -4712 to 9999 (excluding year 0) Any nonzero integer
MONTH 01 to 12 0 to 11
DAY 01 to 31 Any nonzero integer
HOUR 00 to 23 0 to 23
MINUTE 00 to 59 0 to 59
00 to 59.9(n), where 9(n) is the precision of
SECOND 0 to 59.9(n)
time fractional seconds

Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology M. V. Khasne 8


PL/SQL Large Object (LOB) Data Types :
 Large object (LOB) data types refer large to data items such as text, graphic
images, video clips, and sound waveforms. LOB data types allow efficient,
random, piecewise access to this data. Following are the predefined PL/SQL
LOB data types:

Data Type Description Size


Used to store value in pointer variable, to read only System-dependent.
BFILE binary data stored as an external file outside the Cannot exceed 4
database. GB.
Used to store Binary character like songs, video,
BLOB 4 GB
image, constructed data type.
Used to store large blocks of character data in the
CLOB 4 GB
database.
Used to store large blocks of NCHAR data in the
NCLOB 4 GB
database.

Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology M. V. Khasne 9


PL/SQL Variables :
 Variables are nothing but a name given to a storage area that our program can
manipulate. Information is transmitted between a PL/SQL program and the
database through variables. The variables gets overwrite if new value is
assigned to it.
 The general rules for declaring the variables are :
1) The name of a PL/SQL variable consists of a letter optionally followed by
more letters, numerals, dollar signs, underscores, and number signs and
should not exceed 30 characters.
2) You cannot use a reserved PL/SQL keywords as a variable name.
3) Variables should be declared before they are used. Data types like DATE,
VARCHAR, NUMBER, BOOLEAN, CHAR, LONG etc are of scalar type.
Example : Variable declaration could be as follows:
commission number(4) ;
 The initialization of variables can be done at the time of declaration only.
Example :
commission number(4) :=0 ;
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology M. V. Khasne 10
General rules for declaring the variables continued….
4) BOOLEAN data type variables accept only TRUE or FALSE values.
5) BOOLEAN values can be used with AND, OR, NOT operators.
6) If a user wish to create variable that is of type of the column present in some
table, then %TYPE attribute is used to declare that variable.
Example : If user wishes to use a variable salary in the program that is of type sal
column in emp table, then salary variable can be declared as follows :
salary emp.sal%type ;
 The general Syntax for declaring the variable that is of type of some column in
the table is as follows:
<variable-name> <table-name.column-name>%type ;
 The general Syntax to declare a variable is :
<variable-name> <data type> [NOT NULL := value] ;
Where , Variable-name is the name of the variable. Data type is a valid PL/SQL data
type. NOT NULL is an optional specification on the variable. value or DEFAULT
value is also an optional specification, where you can initialize a variable.

Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology M. V. Khasne 11


PL/SQL Constants :
 As the name implies a value used in PL/SQL block that remains unchanged
throughout the program is called as Constant. We can declare a constant and
use it instead of actual value.
 Syntax :
<constant-name> CONSTANT <data type> := value ;
 Where constant-name is the name of the constant i.e. similar to a variable
name. The word “CONSTANT” is the reserved word and ensures that the value
does not change. Value is the actual value which must be assigned to a
constant when it is declared.
 Example :
PI CONSTANT NUMBER := 3.141592654 ;

Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology M. V. Khasne 12

You might also like