Presentation is loading. Please wait.

Presentation is loading. Please wait.

Oracle 9i Collections. Composite types Several variables as single unit ◦ Records  Different datatype variables are combined here  Ex: All fields of.

Similar presentations


Presentation on theme: "Oracle 9i Collections. Composite types Several variables as single unit ◦ Records  Different datatype variables are combined here  Ex: All fields of."— Presentation transcript:

1 Oracle 9i Collections

2 Composite types Several variables as single unit ◦ Records  Different datatype variables are combined here  Ex: All fields of Students Table ◦ Collections  Combines variables of same type.  Ex. C or Java arrays.  Types of collections 1.Index – by Table (Oracle 7 onwards) 2.Nested Tables (Oracle 8 ) 3.Varray ( Oracle 8)

3 Index-By Tables Syntax: TYPE IS TABLE OF type INDEX BY BINARY_INTEGER; ◦ Set of Key-Value pairs ◦ indexed using BINARY_INTEGER values which do not need to be consecutive. ◦ No upper bounds ◦ Index starts with 1. ◦ In Oracle 9i Release 2 these have been renamed to Associative Arrays and can be indexed by BINARY INTEGER or VARCHAR2.Associative Arrays DECLARE TYPE table_type IS TABLE OF NUMBER(10) INDEX BY BINARY_INTEGER; v_tab table_type; BEGIN v_tab(7) := 100; DECLARE TYPE table_type IS TABLE OF NUMBER(10) INDEX BY BINARY_INTEGER; v_tab table_type; BEGIN v_tab(7) := 100;

4 Nested Table Hold an arbitrary number of elements. use sequential numbers (non negative)as subscripts You can define equivalent SQL types, allowing nested tables to be stored in database tables and manipulated through SQL but index-by cannot be stored in DB. During creation the collection must be dense, having consecutive subscripts for the elements. DELETE method to make the collection sparse. The NEXT method overcomes the problems of traversing sparse collections.

5

6 DECLARE ◦ TYPE table_type IS TABLE OF NUMBER(10); ◦ v_tab table_type; ◦ v_idx NUMBER; BEGIN ◦ v_tab := table_type(1, 2); ◦ FOR i IN 3.. 5 LOOP  v_tab.extend;  v_tab(v_tab.last) := i; ◦ END LOOP; ◦ v_tab.DELETE(3); ◦ v_idx := v_tab.FIRST; ◦ WHILE v_idx IS NOT NULL ◦ LOOP DBMS_OUTPUT.PUT_LINE('The number ' || v_tab(v_idx)); v_idx := v_tab.NEXT(v_idx); ◦ END LOOP ; END; ◦ / The number 1 The number 2 The number 4 The number 5 PL/SQL procedure successfully completed.

7 Varray A VARRAY is similar to a nested table except you must specifiy an upper bound in the declaration. Like nested tables they can be stored in the database, but unlike nested tables individual elements cannot be deleted so they remain dense. Syntax: ◦ TYPE type_name {VARRAY | VARYING ARRAY } (max_size) OF element_type [NOT NULL]; ◦ Ex: ◦ TYPE NUMlist VARRAY(10) OF students.Roll% TYPE NOT NULL;  V_num NUMlist;  V_num NUMlist := NUMlist(1.2) ; //constructor initialization

8

9 Index- BYNested TablesVarrays Cannot be stored in DBCan be Keys can be positive or negative Positive onlypositive only No explicit max Size Have explicit max Size Can be sparse with non- sequential keys Always sequential and have space allocated for each value Cannot be automatically NULL Can be NO_DATA _FOUND is raised if nonexistant data is referred SUBSCRIPT_BEYOND_COU NT Can be declared in PL/SQL block only Can be declared in PL/SQL block or outside using CREATE TYPE Elements are assigned directly without initialization The Table must be initialized and extended before elements can be assigned The Varray must be initialized before elements can be assigned

10 Collection Methods A variety of methods exist for collections, but not all are relevant for every collection type. EXISTS(n) - Returns TRUE if the specified element exists. COUNT - Returns the number of elements. LIMIT - Returns max num of elements for a VARRAY, or NULL for nested tables. 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. 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.

11 Multilevel Collections


Download ppt "Oracle 9i Collections. Composite types Several variables as single unit ◦ Records  Different datatype variables are combined here  Ex: All fields of."

Similar presentations


Ads by Google