SlideShare a Scribd company logo
Collection
A composite data type stores values that have internal components. You can pass
entire composite variables to subprograms as parameters, and you can access
internal components of composite variables individually. Internal components can
be either scalar or composite. You can use scalar components wherever you can use
scalar variables. PL/SQL lets you define two kinds of composite data types,
collection and record. You can use composite components wherever you can use
composite variables of the same type.
In a collection, the internal components always have the same data type, and are
called elements. You can access each element of a collection variable by its unique
subscript, with this syntax: variable_name(subscript). To create a collection
variable, you either define a collection type and then create a variable of that type or
use %TYPE.
In a record, the internal components can have different data types, and are called
fields. You can access each field of a record variable by its name, with this syntax:
variable_name.field_name. To create a record variable, you either define a
RECORD type and then create a variable of that type or use %ROWTYPE or
%TYPE.
Introduction
An associative array (also called an index-by table) is a set of key-value pairs.
Each key is unique, and is used to locate the corresponding value. The key can be
either an integer or a string.
Using a key-value pair for the first time adds that pair to the associative array.
Using the same key with a different value changes the value.
Like a database table, an associative array holds a data set of arbitrary size, and
you can access its elements without knowing their positions in the array. An
associative array does not need the disk space or network operations of a database
table, but an associative array cannot be manipulated by SQL statements (such as
INSERT and DELETE).
An associative array is intended for temporary data storage.
Understanding Associative Arrays (Index-By Tables)
 DECLARE
 -- Associative array indexed by string:

 TYPE population IS TABLE OF
NUMBER -- Associative array type
 INDEX BY VARCHAR2(64);

 city_population population; --
Associative array variable
 i VARCHAR2(64);

 BEGIN
 -- Add new elements to associative array:

 city_population('Smallville') := 2000;
 city_population('Midland') := 750000;
 city_population('Megalopolis') := 1000000;


Understanding Associative Arrays (Index-By Tables)
-- Change value associated with key
'Smallville':
city_population('Smallville') :=
2001;
-- Print associative array:
i := city_population.FIRST;
WHILE i IS NOT NULL LOOP
DBMS_Output.PUT_LINE
('Population of ' || i || ' is ' ||
TO_CHAR(city_population(i)));
i := city_population.NEXT(i);
END LOOP;
END;
/
Population of Megalopolis is 1000000
Population of Midland is 750000
Population of Smallville is 2001
Conceptually, a nested table is like a one-dimensional array with an arbitrary
number of elements.
Within the database, a nested table is a column type that holds a set of values. The
database stores the rows of a nested table in no particular order. When you retrieve a
nested table from the database into a PL/SQL variable, the rows are given
consecutive subscripts starting at 1. These subscripts give you array-like access to
individual rows.
A nested table differs from an array in these important ways:
 An array has a declared number of elements, but a nested table does not. The
size of a nested table can increase dynamically (however, a maximum limit is
imposed—see Referencing Collection Elements).
 An array is always dense (that is, it always has consecutive subcripts). A nested
array is dense initially, but it can become sparse, because you can delete elements
from it.
Understanding Nested Tables
Understanding Nested Tables
Figure Array and Nested Table
A variable-size array (varray) is an item of the data type VARRAY. A varray has a
maximum size, which you specify in its type definition. A varray can contain a
varying number of elements, from zero (when empty) to the maximum size. A
varray index has a fixed lower bound of 1 and an extensible upper bound. To access
an element of a varray, you use standard subscripting syntax.
Figure shows a varray named Grades, which has maximum size 10 and contains
seven elements. The current upper bound for Grades is 7, but you can increase it to
the maximum of 10. Grades(n) references the nth element of Grades.
Understanding Variable-Size Arrays (Varrays)
Varray of Size 10
Example Declaring Nested Tables, Varrays, and Associative Arrays
DECLARE
TYPE nested_type IS TABLE OF
VARCHAR2(30);
TYPE varray_type IS VARRAY(5) OF
INTEGER;
TYPE assoc_array_num_type
IS TABLE OF NUMBER INDEX BY
PLS_INTEGER;
TYPE assoc_array_str_type
IS TABLE OF VARCHAR2(32) INDEX BY
PLS_INTEGER;
TYPE assoc_array_str_type2
IS TABLE OF VARCHAR2(32) INDEX BY
VARCHAR2(64);
v1 nested_type;
v2 varray_type;
v3 assoc_array_num_type;
v4 assoc_array_str_type;
v5 assoc_array_str_type2;
BEGIN
-- an arbitrary number of strings can be
inserted v1
v1 :=
nested_type('Shipping','Sales','Finance','Pay
roll');
v2 := varray_type(1, 2, 3, 4, 5); -- Up to 5
integers
v3(99) := 10; -- Just start assigning to
elements
v3(7) := 100; -- Subscripts can be any
integer values
v4(42) := 'Smith'; -- Just start assigning to
elements
v4(54) := 'Jones'; -- Subscripts can be any
integer values
v5('Canada') := 'North America';
-- Just start assigning to elements
v5('Greece') := 'Europe';
-- Subscripts can be string values
END;
/
Summary
• Introduction
• Associative array (or index-by table)
• Nested table
• Variable-size array (varray)

More Related Content

PPTX
Presentation1
Jay Patel
 
PPTX
Anchor data type,cursor data type,array data type
dhruv patel
 
DOCX
Exploring collections with example
pranav kumar verma
 
PPT
Lecture 2a arrays
Victor Palmar
 
PPTX
Data Structure and Algorithms by Sabeen Memon03.pptx
msoomar8611
 
PPTX
DS_PPT.pptx
MeghaKulkarni27
 
PPTX
Relational data model
Dr. SURBHI SAROHA
 
PPT
DATA STRUCTURES A BRIEF OVERVIEW OF DATA
LearnItAllAcademy
 
Presentation1
Jay Patel
 
Anchor data type,cursor data type,array data type
dhruv patel
 
Exploring collections with example
pranav kumar verma
 
Lecture 2a arrays
Victor Palmar
 
Data Structure and Algorithms by Sabeen Memon03.pptx
msoomar8611
 
DS_PPT.pptx
MeghaKulkarni27
 
Relational data model
Dr. SURBHI SAROHA
 
DATA STRUCTURES A BRIEF OVERVIEW OF DATA
LearnItAllAcademy
 

Similar to Collection (20)

PPT
data structure programing language in c.ppt
LavkushGupta12
 
PPT
Introduction of data structure in short.ppt
mba29007
 
PDF
Unit 2
Sowri Rajan
 
PPT
DATA STRUCTURES IN INFORMATION TECHNOLOGY
DanilynSukkie
 
PPT
Data Structures and Algorithm for Engineers.ppt
AdharshKumarSingh
 
PPT
02-dataStructurePM and algortima for python.ppt
totowahid1
 
PPT
DSA theory all topics (summary) presentation
damru0408
 
PPT
Basic Data Structure and its concepts details
baisakhiparida92
 
PPT
different types of data structures using c.ppt
RaviKumarChavali1
 
PPT
Data structure study material introduction
SwatiShinde79
 
PPT
PM.ppt DATA STRUCTURE USING C WITH EXAMPLE PROGRAMES
NagarathnaRajur2
 
PPT
DS_INTROduction dhjm,asjkfnsflkwefskdmcsdmckds
aayushkumarsinghec22
 
PPT
Chapter 08
Terry Yoast
 
PPTX
data structures module I & II.pptx
rani marri
 
PPT
PMDATA STRUICVIUDGHfjufguigfuigkguidfui.ppt
srahul53094
 
PPTX
Introduction to data structure presentations
jayajadhav7
 
PDF
java.pdf
RAJCHATTERJEE24
 
PPT
PM.ppt
SrinivasanCSE
 
PPTX
advanced mySQL Database tutorials and RDBMS tutorials.pptx
argsstring
 
data structure programing language in c.ppt
LavkushGupta12
 
Introduction of data structure in short.ppt
mba29007
 
Unit 2
Sowri Rajan
 
DATA STRUCTURES IN INFORMATION TECHNOLOGY
DanilynSukkie
 
Data Structures and Algorithm for Engineers.ppt
AdharshKumarSingh
 
02-dataStructurePM and algortima for python.ppt
totowahid1
 
DSA theory all topics (summary) presentation
damru0408
 
Basic Data Structure and its concepts details
baisakhiparida92
 
different types of data structures using c.ppt
RaviKumarChavali1
 
Data structure study material introduction
SwatiShinde79
 
PM.ppt DATA STRUCTURE USING C WITH EXAMPLE PROGRAMES
NagarathnaRajur2
 
DS_INTROduction dhjm,asjkfnsflkwefskdmcsdmckds
aayushkumarsinghec22
 
Chapter 08
Terry Yoast
 
data structures module I & II.pptx
rani marri
 
PMDATA STRUICVIUDGHfjufguigfuigkguidfui.ppt
srahul53094
 
Introduction to data structure presentations
jayajadhav7
 
java.pdf
RAJCHATTERJEE24
 
advanced mySQL Database tutorials and RDBMS tutorials.pptx
argsstring
 
Ad

Recently uploaded (20)

PPTX
INTESTINALPARASITES OR WORM INFESTATIONS.pptx
PRADEEP ABOTHU
 
PDF
RA 12028_ARAL_Orientation_Day-2-Sessions_v2.pdf
Seven De Los Reyes
 
PDF
Virat Kohli- the Pride of Indian cricket
kushpar147
 
PPTX
How to Apply for a Job From Odoo 18 Website
Celine George
 
DOCX
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
PPTX
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
PPTX
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
PDF
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
PPTX
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
PPTX
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
DOCX
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
PPTX
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
PPTX
HEALTH CARE DELIVERY SYSTEM - UNIT 2 - GNM 3RD YEAR.pptx
Priyanshu Anand
 
PPTX
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
PPTX
How to Track Skills & Contracts Using Odoo 18 Employee
Celine George
 
DOCX
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
PPTX
BASICS IN COMPUTER APPLICATIONS - UNIT I
suganthim28
 
PPTX
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
PPTX
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
PPTX
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
INTESTINALPARASITES OR WORM INFESTATIONS.pptx
PRADEEP ABOTHU
 
RA 12028_ARAL_Orientation_Day-2-Sessions_v2.pdf
Seven De Los Reyes
 
Virat Kohli- the Pride of Indian cricket
kushpar147
 
How to Apply for a Job From Odoo 18 Website
Celine George
 
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
HEALTH CARE DELIVERY SYSTEM - UNIT 2 - GNM 3RD YEAR.pptx
Priyanshu Anand
 
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
How to Track Skills & Contracts Using Odoo 18 Employee
Celine George
 
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
BASICS IN COMPUTER APPLICATIONS - UNIT I
suganthim28
 
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
Ad

Collection

  • 2. A composite data type stores values that have internal components. You can pass entire composite variables to subprograms as parameters, and you can access internal components of composite variables individually. Internal components can be either scalar or composite. You can use scalar components wherever you can use scalar variables. PL/SQL lets you define two kinds of composite data types, collection and record. You can use composite components wherever you can use composite variables of the same type. In a collection, the internal components always have the same data type, and are called elements. You can access each element of a collection variable by its unique subscript, with this syntax: variable_name(subscript). To create a collection variable, you either define a collection type and then create a variable of that type or use %TYPE. In a record, the internal components can have different data types, and are called fields. You can access each field of a record variable by its name, with this syntax: variable_name.field_name. To create a record variable, you either define a RECORD type and then create a variable of that type or use %ROWTYPE or %TYPE. Introduction
  • 3. An associative array (also called an index-by table) is a set of key-value pairs. Each key is unique, and is used to locate the corresponding value. The key can be either an integer or a string. Using a key-value pair for the first time adds that pair to the associative array. Using the same key with a different value changes the value. Like a database table, an associative array holds a data set of arbitrary size, and you can access its elements without knowing their positions in the array. An associative array does not need the disk space or network operations of a database table, but an associative array cannot be manipulated by SQL statements (such as INSERT and DELETE). An associative array is intended for temporary data storage. Understanding Associative Arrays (Index-By Tables)
  • 4.  DECLARE  -- Associative array indexed by string:   TYPE population IS TABLE OF NUMBER -- Associative array type  INDEX BY VARCHAR2(64);   city_population population; -- Associative array variable  i VARCHAR2(64);   BEGIN  -- Add new elements to associative array:   city_population('Smallville') := 2000;  city_population('Midland') := 750000;  city_population('Megalopolis') := 1000000;   Understanding Associative Arrays (Index-By Tables) -- Change value associated with key 'Smallville': city_population('Smallville') := 2001; -- Print associative array: i := city_population.FIRST; WHILE i IS NOT NULL LOOP DBMS_Output.PUT_LINE ('Population of ' || i || ' is ' || TO_CHAR(city_population(i))); i := city_population.NEXT(i); END LOOP; END; / Population of Megalopolis is 1000000 Population of Midland is 750000 Population of Smallville is 2001
  • 5. Conceptually, a nested table is like a one-dimensional array with an arbitrary number of elements. Within the database, a nested table is a column type that holds a set of values. The database stores the rows of a nested table in no particular order. When you retrieve a nested table from the database into a PL/SQL variable, the rows are given consecutive subscripts starting at 1. These subscripts give you array-like access to individual rows. A nested table differs from an array in these important ways:  An array has a declared number of elements, but a nested table does not. The size of a nested table can increase dynamically (however, a maximum limit is imposed—see Referencing Collection Elements).  An array is always dense (that is, it always has consecutive subcripts). A nested array is dense initially, but it can become sparse, because you can delete elements from it. Understanding Nested Tables
  • 6. Understanding Nested Tables Figure Array and Nested Table
  • 7. A variable-size array (varray) is an item of the data type VARRAY. A varray has a maximum size, which you specify in its type definition. A varray can contain a varying number of elements, from zero (when empty) to the maximum size. A varray index has a fixed lower bound of 1 and an extensible upper bound. To access an element of a varray, you use standard subscripting syntax. Figure shows a varray named Grades, which has maximum size 10 and contains seven elements. The current upper bound for Grades is 7, but you can increase it to the maximum of 10. Grades(n) references the nth element of Grades. Understanding Variable-Size Arrays (Varrays) Varray of Size 10
  • 8. Example Declaring Nested Tables, Varrays, and Associative Arrays DECLARE TYPE nested_type IS TABLE OF VARCHAR2(30); TYPE varray_type IS VARRAY(5) OF INTEGER; TYPE assoc_array_num_type IS TABLE OF NUMBER INDEX BY PLS_INTEGER; TYPE assoc_array_str_type IS TABLE OF VARCHAR2(32) INDEX BY PLS_INTEGER; TYPE assoc_array_str_type2 IS TABLE OF VARCHAR2(32) INDEX BY VARCHAR2(64); v1 nested_type; v2 varray_type; v3 assoc_array_num_type; v4 assoc_array_str_type; v5 assoc_array_str_type2; BEGIN -- an arbitrary number of strings can be inserted v1 v1 := nested_type('Shipping','Sales','Finance','Pay roll'); v2 := varray_type(1, 2, 3, 4, 5); -- Up to 5 integers v3(99) := 10; -- Just start assigning to elements v3(7) := 100; -- Subscripts can be any integer values v4(42) := 'Smith'; -- Just start assigning to elements v4(54) := 'Jones'; -- Subscripts can be any integer values v5('Canada') := 'North America'; -- Just start assigning to elements v5('Greece') := 'Europe'; -- Subscripts can be string values END; /
  • 9. Summary • Introduction • Associative array (or index-by table) • Nested table • Variable-size array (varray)