Oracle - Collections Kaushik Raghupathi
What is a Collection Is an ordered group of elements of the same datatype Similar to Lists, Arrays  PL/SQL has following collections Index by Tables  vArrays  Nested Tables  Mapping from Other Languages  Hash Tables are like Index by Tables Arrays are like vArrays Sets are like Nested Tables
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; /
Index by Table - Associative Array   TYPE type_name IS TABLE OF [DATATYPE]    INDEX BY [DATATYPE];
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; /
Nested Tables   TYPE type_name IS TABLE OF [DATATYPE];
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;
VARRAY   TYPE type_name IS VARRAY(SIZE) of [DATATYPE];
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;
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
Passing Collections as Parameters DECLARE    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 procedure END;
Using Collections in Tables CREATE 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) /
Using Collections in Tables INSERT INTO department     VALUES(60, 'Security', 750400,       ProjectList('New Badges', 'Track Computers', 'Check Exits'));     Try selecting data from the table
Using Collections in Tables SELECT a.* FROM TABLE(SELECT projects FROM department) a

Oracle Collections

  • 1.
    Oracle - CollectionsKaushik Raghupathi
  • 2.
    What is aCollection Is an ordered group of elements of the same datatype Similar to Lists, Arrays  PL/SQL has following collections Index by Tables vArrays Nested Tables  Mapping from Other Languages Hash Tables are like Index by Tables Arrays are like vArrays Sets are like Nested Tables
  • 3.
    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; /
  • 4.
    Index by Table- Associative Array   TYPE type_name IS TABLE OF [DATATYPE]    INDEX BY [DATATYPE];
  • 5.
    Nested Tables OneColumn 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; /
  • 6.
    Nested Tables  TYPE type_name IS TABLE OF [DATATYPE];
  • 7.
    vArray Ordered groupof 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;
  • 8.
    VARRAY   TYPEtype_name IS VARRAY(SIZE) of [DATATYPE];
  • 9.
    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;
  • 10.
    Key Aspects Youneed 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
  • 11.
    Passing Collections asParameters DECLARE    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 procedure END;
  • 12.
    Using Collections inTables CREATE 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) /
  • 13.
    Using Collections inTables INSERT INTO department     VALUES(60, 'Security', 750400,       ProjectList('New Badges', 'Track Computers', 'Check Exits'));     Try selecting data from the table
  • 14.
    Using Collections inTables SELECT a.* FROM TABLE(SELECT projects FROM department) a