Oracle Collections

1,153 views

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,153
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
57
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Oracle Collections

  1. 1. Oracle - Collections Kaushik Raghupathi
  2. 2. What is a Collection <ul><ul><li>Is an ordered group of elements of the same datatype </li></ul></ul><ul><ul><li>Similar to Lists, Arrays  </li></ul></ul><ul><ul><li>PL/SQL has following collections </li></ul></ul><ul><ul><ul><li>Index by Tables </li></ul></ul></ul><ul><ul><ul><li>vArrays </li></ul></ul></ul><ul><ul><ul><li>Nested Tables  </li></ul></ul></ul><ul><ul><li>Mapping from Other Languages </li></ul></ul><ul><ul><ul><li>Hash Tables are like Index by Tables </li></ul></ul></ul><ul><ul><ul><li>Arrays are like vArrays </li></ul></ul></ul><ul><ul><ul><li>Sets are like Nested Tables </li></ul></ul></ul>
  3. 3. Index by Table - Associative Array <ul><ul><li>Associative arrays are sets of key-value pairs </li></ul></ul><ul><ul><li>Each key can uniquely identify a value in the array </li></ul></ul><ul><li>  </li></ul>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. 4. Index by Table - Associative Array <ul><li>  </li></ul><ul><li>TYPE type_name IS TABLE OF [DATATYPE]    INDEX BY [DATATYPE]; </li></ul>
  5. 5. Nested Tables <ul><ul><li>One Column Database Table </li></ul></ul><ul><ul><li>No particular Order of storage </li></ul></ul><ul><ul><li>Can have items deleted and sparse collections </li></ul></ul><ul><ul><li>Typically unbounded </li></ul></ul><ul><li>  </li></ul>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. 6. Nested Tables <ul><li>  TYPE type_name IS TABLE OF [DATATYPE]; </li></ul>
  7. 7. vArray <ul><ul><li>Ordered group of Items </li></ul></ul><ul><ul><li>Maximum size needs to be specified </li></ul></ul><ul><ul><li>Small or uniform sized collections </li></ul></ul><ul><li>  </li></ul>DECLARE    TYPE CourseList IS VARRAY(50) OF VARCHAR2(16);    my_courses CourseList; BEGIN    my_courses :=       CourseList('Econ 2010', 'Acct 3401', 'Mgmt 3100'); END;
  8. 8. VARRAY <ul><li>  TYPE type_name IS VARRAY(SIZE) of [DATATYPE]; </li></ul>
  9. 9. Initializing, Extend, Count, Delete <ul><li>  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; </li></ul>
  10. 10. Key Aspects <ul><ul><li>You need to initialize all vArray/Nested Table before assigning values </li></ul></ul><ul><ul><li>You need to extend them to allocate their length </li></ul></ul><ul><ul><li>You can use COUNT to check if the length </li></ul></ul><ul><ul><li>For a vARRAY you can DELETE the whole array </li></ul></ul><ul><ul><li>For a NESTED Table you can delete a specific element </li></ul></ul><ul><ul><li>EXISTS(n) check if an nth element exists </li></ul></ul><ul><ul><li>LIMIT return the maximum length of a VARRAY </li></ul></ul><ul><ul><li>COUNT returns the count of elements of a collection  </li></ul></ul><ul><ul><li>Associative Arrays normally for Small Lookups in memory </li></ul></ul>
  11. 11. 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;
  12. 12. 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) /
  13. 13. Using Collections in Tables INSERT INTO department     VALUES(60, 'Security', 750400,       ProjectList('New Badges', 'Track Computers', 'Check Exits'));     Try selecting data from the table
  14. 14. Using Collections in Tables SELECT a.* FROM TABLE(SELECT projects FROM department) a

×