Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
C3: Protected
Credential
Information:
Version and
Date:
SQL/PPT/0108/1.0
Icons Used
Tools
Handson
Exercise
Coding
Standards
TestYour
Understanding
Reference
TryitOut
AWelcome
Break
Contacts
Questions
Collection
A collection is a ordered group of elements, all of the
same type.
It is a general concept that encompasses list, arrays, and
other familiar data types.
Each element has a unique subscript that determines its
position in the collection.
Collections work like the arrays found in most thirdgeneration programming language.
Collection (Contd.)
A collection is a ordered group of elements, all of the
same type.
It is a general concept that encompasses list, arrays,
and other familiar data types.
Each element has a unique subscript that determines its
position in the collection.
Collections work like the arrays found in most thirdgeneration programming language.
PL/SQL has two collection types: tables and varrays
Tables comes in two flavors:
Index by tables (formerly called PL/SQL tables)
Nested tables
Collection (Contd.)
Index-by tables:
Unconstrained
Collection (Contd.)
Syntax:
TYPE type_name IS TABLE OF
{column_type | variable%TYPE
| table.column%TYPE} [NOT NULL]
| table.%ROWTYPE
[INDEX BY BINARY_INTEGER];
identifier type_name;
Collection (Contd.)
DECLARE:
TYPE population_type IS TABLE OF NUMBER INDEX BY VARCHAR2(64);
country_population population_type;
continent_population population_type;
howmany NUMBER;
which VARCHAR2(64);
BEGIN:
country_population('Greenland') := 100000; -- Creates new entry
country_population('Iceland') := 750000; -- Creates new entry
-- Looks up value associated with a string
howmany := country_population('Greenland');
dbms_output.put_line(howmany);
continent_population('Australia') := 30000000;
continent_population('Antarctica') := 1000; -- Creates new entry
continent_population('Antarctica') := 1001; -- Replaces previous
value
10
Collection (Contd.)
-- Returns 'Antarctica' as that comes first
alphabetically.
which := continent_population.FIRST;
dbms_output.put_line(which);
-- Returns 'Australia' as that comes last
alphabetically.
which := continent_population.LAST;
dbms_output.put_line(which);
-- Returns the value corresponding to the last key, in
this
-- case the population of Australia.
howmany :=
continent_population(continent_population.LAST);
dbms_output.put_line(howmany);
END;
11
Collection (Contd.)
Nested Table:
12
Collection (Contd.)
Example: Store the borrower information and it's
dependent information in the same table:
CREATE OR REPLACE TYPE dependend_ty as object
( dependend_name
VARCHAR2(50),
relationship
VARCHAR2(20),
birth_date
DATE) ;
13
Collection (Contd.)
CREATE TABLE borrower:
(borrower_id
NUMBER,
borrowe_name
VARCHAR2(50),
borrower_adress VARCHAR2(100),
dependends
dependend_nt)
NESTED TABLE dependends STORE AS
dependend_nt_tab;
14
Collection (Contd.)
INSERT INTO borrower
(10001,
'JAMES',
'D2-14 ,CA 9264',
dependend_nt(dependent_ty('JACOB','FATHER','31-MAR-1950'),
dependent_ty('RUBELA','MOTHER','25-MAR-1949')));
SELECT borrower_name,N.dependend_name,N.realtionship,
N.birth_date
FROM borrower,TABLE(borrower.dependends) N;
SET DESCRIBE DEPTH 2
DESC borrower
15
Collection (Contd.)
Difference between array and nested tables:
First, arrays have a fixed upper bound, but nested
tables are unbounded. So, the size of a nested table can
increase dynamically.
Second, arrays must be dense (have consecutive
subscripts). So, individual elements cannot be deleted
from an array. Initially, nested tables are dense, but
they can be sparse (have nonconsecutive subscripts).
16
Collection (Contd.)
Elements from a nested table can be deleted using the builtin procedure DELETE.
That might leave gaps in the index, but the built-in function
NEXT allows iteration over any series of subscripts.
17
Collection (Contd.)
Varrays:
Items of type VARRAY are called varrays.
Allow to associate a single identifier with an entire
collection.
Manipulate the collection as a whole and reference
individual elements easily.
To reference an element, use standard subscripting
syntax.
Example: Grade (3) references the third element in
varray Grades.
18
Collection (Contd.)
CREATE OR REPLACE TYPE addresses_va AS VARRAY(3) of
VARCHAR2 (50);
CREATE TABLE borrower
(borrower_id
NUMBER,
borrowe_name
VARCHAR2(50),
borrower_adress VARCHAR2(100),
addresses
addresses _VA);
19
Collection (Contd.)
INSERT INTO borrower:
(10001,
'JAMES',
'D2-14 ,CA 9264',
addresses_va('address1','address2','address3'));
SELECT addresses FROM borrower: If you want to display
the data from Varray in separates line then use TABLE function.
SELECT b.borrower_name,n.*
FROM borrower b,TABLE(b.addresses) N;
20
Collection (Contd.)
Collection method:
FIRST: Returns the index of the first element in the
collection.
LAST: Returns the index of the last element in the
collection.
PRIOR(n): Returns the index of the element prior to the
specified element.
NEXT(n): Returns the index of the next element after the
specified element.
EXTEND: Appends a single null element to the collection.
EXTEND(n): Appends n null elements to the collection.
21
Collection (Contd.)
22
Q&A
Allow time for questions from participants
23
25
Disclaimer: Parts of the content of this course is based on the materials available from the Web sites and
books listed above. The materials that can be accessed from linked sites are not maintained by
Cognizant Academy and we are not responsible for the contents thereof. All trademarks, service marks,
and trade names in this course are the marks of the respective owner(s).
26