14
Oracle - Collections Kaushik Raghupathi

9455053-110928055257-phpapp01

Embed Size (px)

Citation preview

Page 1: 9455053-110928055257-phpapp01

Oracle - Collections

Kaushik Raghupathi

Page 2: 9455053-110928055257-phpapp01

What is a Collection• Is an ordered group of elements of the same

datatype• Similar to Lists, Arrays • PL/SQL has following collections

o Index by Tables o vArrays o Nested Tables 

• Mapping from Other Languages o Hash Tables are like Index by Tableso Arrays are like vArrayso Sets are like Nested Tables

Page 3: 9455053-110928055257-phpapp01

Index by Table - Associative Array• Associative arrays are sets of key-value pairs• Each key can uniquely identify a value in the array

 

DECLARE  TYPE population_type IS TABLE OF NUMBER INDEX BY VARCHAR2(64);  country_population population_type;  howmany NUMBER;BEGIN  country_population('Greenland') := 100000;  country_population('Iceland') := 750000;  howmany := country_population('Greenland');END;/

Page 4: 9455053-110928055257-phpapp01

Index by Table - Associative Array TYPE type_name IS TABLE OF [DATATYPE]   INDEX BY [DATATYPE];

Page 5: 9455053-110928055257-phpapp01

Nested Tables• One Column Database Table• No particular Order of storage• Can have items deleted and sparse collections• Typically unbounded

 DECLARE   TYPE CourseList IS TABLE OF VARCHAR2(16);   my_courses CourseList;BEGIN   my_courses :=      CourseList('Econ 2010', 'Acct 3401', 'Mgmt 3100');     dbms_output.put_line(my_courses(1));END;/

Page 6: 9455053-110928055257-phpapp01

Nested Tables TYPE type_name IS TABLE OF [DATATYPE];

Page 7: 9455053-110928055257-phpapp01

vArray• Ordered group of Items• Maximum size needs to be specified• Small or uniform sized collections

 

DECLARE   TYPE CourseList IS VARRAY(50) OF VARCHAR2(16);   my_courses CourseList;BEGIN   my_courses :=      CourseList('Econ 2010', 'Acct 3401', 'Mgmt 3100');END;

Page 8: 9455053-110928055257-phpapp01

VARRAY TYPE type_name IS VARRAY(SIZE) of [DATATYPE];

Page 9: 9455053-110928055257-phpapp01

Initializing, Extend, Count, Delete DECLARE   TYPE CourseList IS VARRAY(50) OF VARCHAR2(16);   my_courses CourseList;BEGIN  my_courses := CourseList();  my_courses.EXTEND(2);  dbms_output.put_line(my_courses.COUNT);  select 'string' into my_courses(2) from dual;  dbms_output.put_line(my_courses( 2));  my_courses.DELETE;END;

Page 10: 9455053-110928055257-phpapp01

Key Aspects• You need to initialize all vArray/Nested Table before

assigning values• You need to extend them to allocate their length• You can use COUNT to check if the length• For a vARRAY you can DELETE the whole array• For a NESTED Table you can delete a specific

element• EXISTS(n) check if an nth element exists • LIMIT return the maximum length of a VARRAY• COUNT returns the count of elements of a collection • Associative Arrays normally for Small Lookups in

memory

Page 11: 9455053-110928055257-phpapp01

Passing Collections as ParametersDECLARE   TYPE Roster IS TABLE OF VARCHAR2(15);   names Roster := Roster('J Hamil', 'D Piro', 'R Singh');   i BINARY_INTEGER := 2;BEGIN   verify_name(names(i));  -- call procedureEND;

Page 12: 9455053-110928055257-phpapp01

Using Collections in TablesCREATE TYPE ProjectList AS VARRAY(50) OF VARCHAR2(16);/ CREATE TABLE department (  -- create database table   dept_id  NUMBER(2),   name     VARCHAR2(15),   budget   NUMBER(11,2),-- Each department can have up to 50 projects.   projects ProjectList)/

Page 13: 9455053-110928055257-phpapp01

Using Collections in TablesINSERT INTO department    VALUES(60, 'Security', 750400,      ProjectList('New Badges', 'Track Computers', 'Check Exits'));  Try selecting data from the table

Page 14: 9455053-110928055257-phpapp01

Using Collections in TablesSELECT a.* FROMTABLE(SELECT projects FROM department) a