EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

PL/SQL Collections

By Yashi GoyalYashi Goyal

Home » Data Science » Data Science Tutorials » Database Management Tutorial » PL/SQL Collections

PL and SQL Collections

Introduction to PL/SQL Collections

First of all, like other modern programming languages, PL/SQL also provides the facility to programmers to use PL/SQL collections. So, in general terms, the collection is a data structure having an ordered group of elements of the same data type and these elements are accessed through indexes according to the specific requirements. Commonly used collections in most of the programming languages are Arrays, Set, List, and Queue, etc.

The PL/SQL collection provides a lot of advantages to both the programmer and the user while storing and accessing the data of similar data types in an application. When there is a need to process the data all at once or handling a large amount of data, PL/SQL collections are very useful.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

It also reduces the size and processing speed while storing the data and accessing the elements. The elements in collections can be accessed easily through loops and indexes. Unlike arrays in other programming languages, PL/SQL supports only one-dimensional collections and the data in the collections are identified by the subscripts (also called index in other languages). Each element is accessed and processed through this unique subscript.

Types of PL/SQL Collections

The PL/SQL collections are of 3 types which are given below with syntax:

Types of PL/SQL Collections

1. Nested tables

A nested table is a PL/SQL collection in which the size is not fixed. So, it is like a one-dimensional array except for the fact that the size is fixed in arrays and it can be dense and sparse both. The programmer needs to extend the memory every time before using it using EXTEND Subscript in the Nested table starts with an integer ‘1’.

Nested tables come under Persistent PL/SQL collections which means that they can be reused further as stored in the database. The programmer can also delete an array element and make the table sparse, which means that Nested tables can be both dense and sparse. The Nested tables can be declared either inside the PL/SQL block or at the schema level.

As there is no limit on the upper size in Nested tables, they come under the category of Unbounded PL/SQL collections.

Syntax:

TYPE typ_name IS table of element_data_type;
Table_name typ_name;

2. Variable Sized Arrays or VARRAYs 

VARRAY is the PL/SQL collection in which the size of the collection is fixed as defined in its definition so VARRAYs are called bounded Elements in arrays are populated sequentially starting from the subscript 1. Unlike Nested tables, varrays provides less flexibility as they are dense only. So the programmer cannot delete any element from between, either whole varray is deleted or it can be trimmed from the end. Varrays has to be accessed and stored sequentially. It can be defined either inside the PL/SQL block or the schema level. Like nested tables, varrays comes under the category of persistent collections so they can be reused and stored at the database.

Syntax:

TYPE typ_name IS VARRAY <size> OF element_data_type;

3. Associative Arrays

As the name suggests, associative arrays are used to hold the values in key-value pairs. The key used can be either a string or integer type. Associative arrays are also called an Index-by table. This collection can be either dense or sparse. In associative arrays, the array size is not fixed in the starting and the programmer needs not to initialize them before using it. An associative array comes under the category of non-persistent collections which means that they are not stored in the database and can not be reused so they are defined inside the PL/SQL block and are used in that particular session only. These arrays are under the category of unbounded collections.

Syntax:

TYPE typ_name IS TABLE OF element_data_type;

Collection Methods

PL/SQL provides some predefined methods to make it easy to work with collections. Some of the methods are given below:

S.No. Method Name Description
1. COUNT It returns the number of elements present in the collection
2. FIRST It returns the smallest (first) index number in the collection for integer subscripts
3. LAST It returns the largest (last) index number in the collection for integer subscripts.
4. EXISTS(n) It is used to check whether a particular element is present in the collection or not. It returns TRUE if the nth elements are present in the collection, FALSE if not.
5. PRIOR(n) It returns the index number that is the predecessor of the index(n) given by the user in the collection.
6. NEXT(n) It returns the index number that is the successor of the index(n) given by the user in the collection.
7. TRIM It is used to remove elements from the collection. TRIM removes the last element from the collection and TRIM(n) removes the last n element from the end of the collection.
8. DELETE It is used to remove all the elements from the given collection. It sets the collection count to 0 after removing all elements
9. DELETE(m,n) It is used in the case of associative arrays and indexed tables to remove all the elements in the range from m to n. It returns null if m is greater than n.
10. LIMIT It is used to check the maximum size of the collection.

Collection Exceptions

Given below are some of the exceptions most likely to arise while working with collections.

Popular Course in this category
Sale
PL SQL Training (4 Courses, 2+ Projects)4 Online Courses | 2 Hands-on Projects | 17+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (8,933 ratings)
Course Price

View Course

Related Courses
SQL Training Program (7 Courses, 8+ Projects)Oracle Training (14 Courses, 8+ Projects)
Exception Name A scenario in which Exception arise
NO_DATA_FOUND An exception arises when a subscript referring an element that is deleted and does not exist anymore.
VALUE_ERROR The exception occurs when the value of columns trying to access is non-convertible to the key type or when a subscript is null
COLLECTION_IS_NULL Exception arise on working on an automatically null collection
SUBSCRIPT_BEYOND_CO UNT An exception arises when a subscript exceeds the maximum count of a number of elements in the collection.
SUBSCRIPT_OUTSIDE_LIM IT The exception arises when trying to reference using the index number which is outside the legal range.
TOO_MANY_ROWS An exception arises when a SELECT into statement returns more than 1 row.

Advantages of Collections in PL/SQL

 Some of the advantages of PL/SQL collections are given below:

  • Besides, one of the biggest advantages of collections is that it improves the system performance by caching the static data which needs to be accessed regularly.
  • Most importantly, collections are helpful while working with the large dataset having the same data type on which the user needs to perform multiple DML operations.
  • By using the one collection variable we can reduce the multiple numbers of variables used for storing different values and hence saving the memory.
  • Performing different operations like storing and processing data becomes easy through PL/SQL collection methods already provided.

Conclusion

Through the above description, you may get an idea of what the PL/SQL collection is and the methods that can be used in the PL/SQL collections. Before using any PL/SQL collection type in the program, the developer needs to think deeply about the scenario first before choosing any type. Though working in PL/SQL collections is not difficult but certain exceptions may arise at different points which the programmer must be aware of and knows how to deal with them.

Recommended Articles

This is a guide to PL/SQL Collections. Here we discuss the syntax, types, methods, and exceptions of PL/SQL collections along with the advantages. You may also look at the following articles to learn more –

  1. Cursors in PL/SQL
  2. CASE statement in PL/SQL
  3. Oracle PL/SQL Interview Questions
  4. PL/SQL Commands

PL SQL Training (4 Courses, 2+ Projects)

4 Online Courses

2 Hands-on Projects

17+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
Database Management Tutorial
  • PL/SQL
    • What is PL/SQL?
    • Careers in PL/SQL
    • PLSQL procedure
    • PL/SQL Exception
    • PL/SQL LIKE
    • PL/SQL Raise Exception
    • PLSQL rowtype
    • PLSQL? bind variables
    • PL/SQL Record
    • PL/SQL WITH
    • PL/SQL bulk collect
    • PL/SQL Block Structure
    • PL/SQL else if
    • PL/SQL nvl2
    • PL/SQL Package
    • PL/SQL exists
    • PL/SQL instr
    • PL/SQL listagg
    • PL/ SQL Formatter
    • PLSQLlength
    • PL/SQL Commands
    • PL/SQL Data Types
    • CASE statement in PL/SQL
    • PL/SQL IF Statement
    • Loops in PL/SQL
    • PL/SQL Add Column
    • For Loop in PLSQL
    • PL/SQL Cursor Loop
    • PLSQL Array
    • Cursors in PL/SQL
    • PL/SQL FOR Loop Cursor
    • PL/SQL Queries
    • PL/SQL SELECT INTO
    • PL/SQL TO_CHAR
    • PL/SQL UNION
    • PL/SQL NOT EQUAL
    • PL/SQL varray
    • PL/SQL Concatenate
    • PL/SQL UPDATE
    • PL/SQL TRIM
    • PL/SQL GROUP BY
    • PL/SQL GOTO
    • PL/SQL Date Functions
    • PL/ SQL having
    • PL/SQL to_DATE
    • PL/SQL NVL
    • PLSQL format date
    • PLSQL mod
    • PLSQL round
    • PL/SQL Boolean
    • PL/SQL exit
    • PL/SQL DECODE
    • PL/SQL ROWNUM
    • PLSQL?pivot
    • PLSQL string functions
    • PL/SQL Block
    • PL/SQL Function
    • PL/SQL Unwrapper
    • PL/SQL Table
    • PL/SQL ALTER TABLE
    • PLSQL execute immediate
    • Triggers in PL/SQL
    • PL/SQL Collections
    • PL/SQL stored procedure
    • PL/SQL Anonymous Block
    • PLSQL Interview Questions
  • DataBase Management
    • Text Data Mining
    • Roles of Database Management System in Industry
    • SQL Server Database Management Tools
    • Database administrator skills
    • Database Management Systems Advantages
    • Database Testing Interview Questions
    • Data Administrator
    • Database Administrator
    • Database Management Software
    • DataStage
    • Types of Database Models
    • Types of Database
    • Hierarchical Database Model
    • Relational Database
    • Relational Database Advantages
    • Operational Database
    • What is RDBMS?
    • What is DB2?
    • Data Masking Tools
    • Database Security
    • Data Replication
    • Bitmap Indexing
    • Second Normal Form
    • Third Normal Form
    • Fourth Normal Form
    • Data Definition Language
    • Data Manipulation Language
    • Data Control Language
    • Transaction Control Language
    • Conceptual Data Model
    • Entity-Relationship Model
    • Relational Database Model
    • Sequential File Organization
    • Checkpoint in DBMS
    • Teradata Create Table
    • Centralized Database
    • Data Storage in Database
    • Thomas write Rule
    • DBA Interview Questions
    • What is JDBC?
    • jdbc hive
    • Apriori Algorithm
    • JDBC Architecture
    • JDBC Interview Questions
    • Wildcard Characters
    • Distributed Database System
    • Multidimensional Database
  • TSQL Basic
    • TSQL
    • What is T-SQL
    • T-SQL Commands
    • T-SQL String Functions
    • TSQL Interview Questions
  • MariaDB
    • MariaDB Versions
    • MariaDB?list users
    • MariaDB Commands
    • MariaDB odbc
    • MariaDB Workbench
    • MariaDB for windows
    • MariaDB Server
    • MariaDB? Data Types
    • MariaDB?boolean
    • MariaDB phpMyAdmin
    • MariaDB Mysqldump
    • MariaDB Java Connector
    • MariaDB insert
    • MariaDB UPDATE
    • MariaDB? rename column
    • MariaDB AUTO_INCREMENT
    • MariaDB Timezone
    • MariaDB GROUP_CONCAT
    • MariaDB wait_timeout
    • MariaDB MaxScale
    • MariaDB? with
    • MariaDB? create?table
    • MariaDB? SHOW TABLES
    • MariaDB alter table
    • MariaDB List Tables
    • MariaDB JSON Functions
    • MariaDB Foreign Key
    • MariaDB? trigger
    • MariaDB Grant All Privileges
    • MariaDB Select Database
    • MariaDB? create database
    • MariaDB Delete Database
    • MariaDB List Databases
    • MariaDB Functions
    • MariaDB? TIMESTAMP
    • MariaDB create user
    • MariaDB add user
    • MariaDB show users
    • MariaDB Delete User
    • MariaDB? change user password
    • MariaDB? change root password
    • MariaDB reset root password
    • MariaDB IF
    • MariaDB bind-address
    • MariaDB Transaction
    • MariaDB Cluster
    • MariaDB Logs
    • MariaDB Encryption
    • MariaDB? backup
    • MariaDB Replication
    • MariaDB max_allowed_packet
    • MariaDB? performance tuning
    • MariaDB export database
    • MariaDB? import SQL
  • SQLite
    • What is SQLite
    • SQLite Commands
    • SQLite Data Types
    • SQLite COUNT
    • SQLite Boolean
    • SQLite autoincrement
    • SQLite select
    • SQLite? Bulk Insert
    • SQLite? add column
    • SQLite? concat
    • SQLite BETWEEN
    • SQLite group by
    • SQLite CASE
    • SQLite group_concat
    • SQLite array
    • SQLite? enum
    • SQLite sum
    • SQLite create table
    • SQLite Alter Table
    • SQLite Create Database
    • SQLite Delete
    • SQLite connection string
    • SQLite Database
    • SQLite Describe Table
    • SQLite Show Tables
    • SQLite exit
    • SQLite create index
    • SQLite foreign key
    • SQLite Stored Procedures
    • SQLite Extension
  • DB2
    • DB2? current date
    • DB2 purescale
    • DB2 backup
    • DB2 restore
    • DB2 C Express
    • DB2 Version
    • DB2? Architecture
    • DB2? Data Types
    • DB2? load
    • DB2? order by
    • DB2 date
    • DB2 NVL
    • DB2? update
    • DB2 warehouse
    • DB2 grant
    • DB2 database
    • DB2 VARCHAR
    • DB2? INSERT
    • DB2 LISTAGG
    • DB2 LIKE
    • DB2 TRUNCATE TABLE
    • DB2 LIST TABLES
    • DB2 between
    • DB2? current timestamp
    • DB2? length
    • DB2? bind
    • DB2 limit rows
    • DB2? export
    • DB2 with
    • DB2 Create Table
    • DB2 case statement
    • DB2 CAST
    • DB2 Functions
    • DB2 Date Functions
    • DB2? row_number
    • DB2 trim
    • DB2? Translate
    • DB2 UNION
    • DB2 timestamp
    • DB2? TIMESTAMPDIFF
    • DB2? replace
    • DB2 merge
    • DB2 COALESCE
    • DB2 ISNULL
    • DB2? explain
    • DB2 Join
    • DB2 alter column
    • DB2 rename column
    • DB2? Describe Table
    • DB2? rename table
    • DB2 List Databases
    • DB2 LUW
    • DB2 Query
    • DB2 GROUP BY
    • DB2 TO_DATE
    • View Serializability in DBMS
    • MariaDB Join
    • MariaDB JSON
    • MariaDB? show databases
    • Dataset Normalization
    • MariaDB Max Connections
    • jdbc connection
    • MariaDB GUI
  • DBMS
    • Introduction To DBMS
    • DBMS ER Diagram
    • What is DBMS?
    • DBMS join
    • DBMS Functions
    • Data Administrator in DBMS
    • DBMS Canonical Cover
    • DBMS Log-Based Recovery
    • DBMS Multivalued Dependency
    • Netezza Database
    • DBMS Concepts
    • DBMS Constraints
    • DBMS_Scheduler
    • B+ Tree in DBMS
    • DBMS_LOB
    • dbms entity
    • DBMS Foreign Key
    • DBMS Users
    • DBMS_Metadata.get_ddl
    • Relational Algebra in DBMS
    • DBMS Components
    • DBMS Features
    • DBMS Models
    • DBMS Relational Model
    • Hashing in DBMS
    • DBMS network model
    • Relationship in DBMS
    • ER Model in DBMS
    • Data Models in DBMS
    • Static Hashing in DBMS
    • Advantages of DBMS
    • dbms_output.put_line
    • DBMS Data Dictionary
    • dbms_xplan.display_cursor
    • Normal Forms in DBMS
    • DBMS helps achieve
    • DBMS 3 tier Architecture
    • Relational Calculus in DBMS
    • Serializability in DBMS
    • File Organization in DBMS
    • DBMS Transaction Processing
    • States of Transaction in DBMS
    • Functional Dependency in DBMS
    • Generalization in DBMS
    • Data Independence in DBMS
    • Lock Based Protocols in DBMS
    • Deadlock in DBMS
    • Integrity Constraints in DBMS
    • Concurrency Control in DBMS
    • Validation Based Protocol in DBMS
    • DBMS Locks
    • Normalization in DBMS
    • Transaction Property in DBMS
    • Specialization in DBMS
    • Aggregation in DBMS
    • Types of DBMS

Related Courses

SQL Certification Course

PL/SQL Certification Course

Oracle Certification Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Special Offer - PL SQL Training (4 Courses, 2+ Projects) Learn More