Upload
mario-araujo
View
216
Download
2
Embed Size (px)
Citation preview
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
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
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 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;
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)/
Using Collections in TablesINSERT INTO department VALUES(60, 'Security', 750400, ProjectList('New Badges', 'Track Computers', 'Check Exits')); Try selecting data from the table
Using Collections in TablesSELECT a.* FROMTABLE(SELECT projects FROM department) a