PL/SQL (Procedural Language/Structured Query Language) is a procedural extension language for SQL used specifically for the Oracle database to ease the management of data and the flow of operations. A core feature of PL/SQL is its diverse set of data types, designed to handle everything from simple numbers and strings to large, unstructured data such as images or text files.
In this article, We will learn about subtypes of data types of PL/SQL, namely scalar types, numeric types, character types, Boolean types, date and time types, large objects, and a type by user-defined subtypes.
Introduction to PL/SQL Data Types
PL/SQL uses many data types corresponding to different types of data including scalar and composite data types and reference data types along with large objects (LOBs).
These data types are used to declare the sharable variables and constants in the packages, parameters for subroutines, and return values of function and procedure subroutines in PL/SQL programs. PL/SQL data types are very much similar to SQL data types and have extra characteristics attached to PL/SQL.
Scalar Data Types in PL/SQL
Scalar data types are basic types that store only one value at a time.like numbers, characters, or even logical values each representing an individual value. The scalar data types are categorized into:
- Numeric Types: It Stores any integer value along with a fractional entity whatever large it is or as per the requirement of the program.
- Character Types: Nodes represent strings of text and These act as structures of text whereby an individual string is represented by a node.
- Boolean Types: It Contains ‘true’ or ‘false’ values.
- Datetime Types: It is Used to represent date and time values which are of typical usage in computer systems.
Subtypes are defined based on the base scalar types and are formed by placing additional constraints upon values that can be assigned.
Numeric Data Types and Subtypes in PL/SQL
Numeric data types store numbers, both integers and real numbers, and allow developers to perform arithmetic operations. The main numeric types include:
- NUMBER: A highly flexible type that can store fixed-point or floating-point numbers. It has precision and scale parameters. For example, NUMBER(5, 2) can store up to 5 digits, with 2 of them after the decimal point.
- BINARY_INTEGER/PLS_INTEGER: These types are used for signed integers and are often faster than the generic NUMBER type because they use machine-dependent formats.
- FLOAT: It is a subtype of NUMBER designed for storing floating-point numbers. You can specify an optional precision such as FLOAT(10) which allows for storing a number with up to 10 digits of precision
Subtypes of Numeric Data Types
- NATURAL: A subtype of BINARY_INTEGER representing non-negative integers (0 or greater).
- NATURALN: It is Similar to NATURAL but cannot be null.
- POSITIVE: A subtype of BINARY_INTEGER representing positive integers (greater than 0).
- POSITIVEN: It is Similar to POSITIVE but cannot be null.
Character Data Types and Subtypes in PL/SQL
Character data types are designed to store any text, numbers or symbols in the form of alphanumeric. They are used specifically for string manipulation and are a vital part of PL/SQL.
- CHAR: It Handles variable-length binary data and fixed-length character strings. If the string is less than the defined length, then the rest is filled up with spaces. For instance, CHAR(10) would store string as CHAR data type that has a length of 10 characters regardless of the actual string length.
- VARCHAR2: To store character strings of varying lengths. VARCHAR2 is slightly different because it only allocates the required amount of space required to store the string. For instance, VARCHAR2(10) data type can accommodate a string with as many as 10 characters.
- LONG: It Can store variable-length character strings of up to 2 gigabytes. However, it is deprecated and it should be replaced with CLOB to 2 GB. However, it is deprecated and should be avoided in favor of CLOB.
Subtypes of Character Data Types
- STRING: A subtype of VARCHAR2, used to represent variable-length strings.
- LONG VARCHAR: A deprecated subtype of VARCHAR2, previously used to store large strings.
PL/SQL Boolean Data Types
The Boolean data type is unique to PL/SQL, allowing you to store logical values and use them in conditional expressions.
BOOLEAN: This type can have three possible values: TRUE, FALSE, or NULL. It is used in conditional statements and logical comparisons. Notably, the BOOLEAN data type is unique to PL/SQL and cannot be used in SQL statements directly.
PL/SQL Datetime and Interval Types
Datetime data types contain date and time and interval types contain the difference between two datetime values. PL/SQL provides the following datetime and interval types:
- DATE: It stores date and time values. These are the year, month, day, hour, minute, and second as a part of the Date type.
- TIMESTAMP: It is an extension of the DATE data type with the added feature of fractional seconds.
- TIMESTAMP WITH TIME ZONE: Saves a TIMESTAMP, but with information about the time zone in which it has been set.
- TIMESTAMP WITH LOCAL TIME ZONE: Standalone function that converts the TIMESTAMP to the time zone of the current database session.
- INTERVAL YEAR TO MONTH: Saves the amount of time measured in years and months.
- INTERVAL DAY TO SECOND: Saves as the time duration in terms of days, hours, minutes, and seconds.
PL/SQL Large Object (LOB) Data Types
LOB data types store large amounts of unstructured data, such as text, images, videos, and audio. PL/SQL provides several LOB types:
- BLOB(Binary Large Object): Stores binary large objects, such as images or multimedia files.
- CLOB(Character Large Object): Stores large character data.
- NCLOB(National Character Large Object): Stores large character data using the national character set.
- BFILE(Binary File): Stores a reference to a binary file stored outside of the database.
LOB types can store up to 4 gigabytes of data, making them ideal for handling large and complex datasets.
Important Points for Using Data Types in PL/SQL
- Choosing the correct data type (e.g., CHAR for fixed text, VARCHAR2 for variable text) optimizes storage and improves readability.
- By using subtypes like NATURAL or POSITIVE, you can enforce constraints (such as no negative values) at the data type level, improving code reliability.
- Use LOB types (like CLOB and BLOB) for large datasets to efficiently handle multimedia, documents, or other large data objects without performance degradation.
- When working with timestamps, consider TIMESTAMP WITH TIME ZONE or TIMESTAMP WITH LOCAL TIME ZONE to avoid time-related issues, especially if your application spans multiple time zones.
Similar Reads
SQL Data Types
SQL Data Types are very important in relational databases. It ensures that data is stored efficiently and accurately. Data types define the type of value a column can hold, such as numbers, text, or dates. Understanding SQL Data Types is critical for database administrators, developers, and data ana
6 min read
SQLite Data Types
In SQLite, understanding data types is important for efficient database design and query execution. SQLite provides five primary data types such as NULL, INTEGER, REAL, TEXT, and BLOB each of them is used for distinct purposes. In this article, We will learn about SQLite Data Types with the help of
4 min read
PostgreSQL - Data Types
PostgreSQL is a robust open-source relational database management system that supports a wide variety of data types. These data types are essential for defining the nature of the data stored in a database column, affecting storage, access, and manipulation. In this article, We will learn about the P
5 min read
R Data Types
R Data types are used to specify the kind of data that can be stored in a variable. For effective memory consumption and computation, the right data type must be selected. Each R data type has its own set of regulations and restrictions. Variables are not needed to be declare with a data type in R,
7 min read
Numpy data Types
NumPy is a powerful Python library that can manage different types of data. Here we will explore the Datatypes in NumPy and How we can check and create datatypes of the NumPy array. DataTypes in NumPyA data type in NumPy is used to specify the type of data stored in a variable. Here is the list of c
3 min read
MATLAB - Data Types
MATLAB is a platform which provides millions of Engineers and Scientists to analyze data using programming and numerical computing algorithm and also help in creating models. Data types are particular types of data items defined by the values they can store in them, generally, in programming languag
4 min read
SQL Server INT Data Type
In SQL Server, while creating a table column we give different data types to represent the type of values stored in the column. For numerical whole numbers, we give INT data type for a column in a table. In this article let us discuss the INT data type, how it is used, and the different INT data typ
3 min read
SQL Server BIT Data Type
The BIT data type is used to store boolean values like 0, 1, or NULL. The SQL server doesn't have the data Boolean instead it has the data type BIT which has which stores the boolean value. The BIT data type is advantageous in terms of space optimization since each BIT data type takes on only 1 bit
3 min read
Types of NoSQL Databases
A database is a collection of structured data or information that is stored in a computer system and can be accessed easily. A database is usually managed by a Database Management System (DBMS). NoSQL databases are a category of non-relational databases designed to handle large-scale, unstructured,
5 min read
SAP ABAP | Data Types
Before Understanding the Data type first understand the Data object. Data objects are variables that we declare in the program. It occupies some memory where you can store the data from external sources. Data can be of different types, so data types are responsible for defining the type of data of t
6 min read