Sei sulla pagina 1di 27

Oracle 10g PL/SQL

Targeted at: Entry level trainee

Session 12: PL/SQL Collection, Record and Varrays

2007, Cognizant Technology Solutions. All Rights Reserved.


The information contained herein is subject to change without notice.

C3: Protected

About the Author


Created By:

Alok Chandra Mishra (146167)

Credential
Information:

Technical Skills : BO/DI/Oracle


Experience : 4.5 Years

Version and
Date:

SQL/PPT/0108/1.0

Icons Used

Tools

Handson
Exercise

Coding
Standards

TestYour
Understanding

Reference

TryitOut

AWelcome
Break

Contacts

Questions

Oracle 10g PL/SQL Session 12:


Overview
Introduction:
Collections and records are composite types that
have internal components that can be manipulated
individually, such as the elements of an array,
record, or table.

Oracle 10g PL/SQL Session 12:


Objective
Objective:
After completing this session, you will be able to:
Define collections
List the types of collection
Declare collection
Explain the use of different collection methods

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:

Also known as associative arrays. It lets you to look up elements


using arbitrary numbers and strings for subscript values.

Are similar to one-dimensional arrays and are referenced like


arrays of records.

Since index-by tables can be passed as parameters, they can be


used to move columns of data into and out of database tables or
between client-side applications and stored subprograms.

Are composed of two components:


Primary key of data type BINARY_INTEGER
Column of scalar or record data type

Can increase in size dynamically because they are

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:

Nested tables hold an arbitrary number of elements.

They use sequential numbers as subscripts.

Within the database, nested tables can be considered one-column


database tables.

Oracle does not stores the rows of a nested table in any


particular order.

When the nested table is retrieve into a PL/SQL variable, the


rows are given consecutive subscripts starting from one. That
gives array-like access to individual rows.

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) ;

CREATE TYPE dependend_nt AS TABLE OF


dependend_ty;

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.)

EXTEND(n1, n2): Appends n1 copies of the n2th element to


the collection.

TRIM: Removes a single element from the end of the


collection.

TRIM(n): Removes n elements from the end of the collection.

DELETE: Removes all elements from the collection.

DELETE(n): Removes element n from the collection.

DELETE(n1,n2): Removes all elements from n1 to n2 from the


collection.

22

Q&A
Allow time for questions from participants

23

Test Your Understanding


1. What is a Collection?
2. What are different type of Collection?
3. What is the difference between nested table
and Indexed table?
4. What is a Varray?
5. How do you describe a nested table?
6. List down all the methods that can be used
with collection.
24

Oracle 10g PL/SQL Session 12:


Summary
A Collection is a ordered group of elements, all of the
same type.
Each element has a unique subscript that determines its
position in the collection.
PL/SQL has two collection types: Tables and Varrays.
Tables comes in two flavors index-by tables (formerly
called PL/SQL tables) and nested tables.

25

Oracle 10g PL/SQL Session 12:


Source
Oracle Database 10g: The Complete Reference

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

You have completed the


Session 12 of
Oracle 10g PL/SQL.

2007, Cognizant Technology Solutions. All Rights Reserved.


The information contained herein is subject to change without notice.

Potrebbero piacerti anche