Presentation is loading. Please wait.

Presentation is loading. Please wait.

Collections Oracle Database PL/SQL 10g Programming Chapter 6.

Similar presentations


Presentation on theme: "Collections Oracle Database PL/SQL 10g Programming Chapter 6."— Presentation transcript:

1 Collections Oracle Database PL/SQL 10g Programming Chapter 6

2 2006 Oracle Database PL/SQL 10g Programming (Chapter 6)Page 2 Collections Collection Types Collection Types VARRAY Collections VARRAY Collections Nested Table Collections Nested Table Collections Associative Array Collections Associative Array Collections Collection API Collection API

3 2006 Oracle Database PL/SQL 10g Programming (Chapter 6)Page 3 Collections Collection Types: Definition Collections are lists. Collections are lists. Collections are ordered or unordered. Collections are ordered or unordered. Ordered lists are indexed by numbers. Ordered lists are indexed by numbers. Unordered lists are indexed by unique strings. Unordered lists are indexed by unique strings. Collection elements can be: Collection elements can be: Scalar variables Scalar variables Compound variables Compound variables

4 2006 Oracle Database PL/SQL 10g Programming (Chapter 6)Page 4 Collections Collection Types: VARRAY VARRAY collections are densely populated structures, indexed by sequential numbers. VARRAY collections are densely populated structures, indexed by sequential numbers. VARRAY collections have an initial maximum size. VARRAY collections have an initial maximum size. VARRAY collections can be used as column data types. VARRAY collections can be used as column data types.

5 2006 Oracle Database PL/SQL 10g Programming (Chapter 6)Page 5 Collections Collection Types: Nested Table Nested tables are densely populated structures, indexed by sequential numbers. Nested tables are densely populated structures, indexed by sequential numbers. Nested tables are have no maximum size. Nested tables are have no maximum size. Nested tables can be used as column data types. Nested tables can be used as column data types.

6 2006 Oracle Database PL/SQL 10g Programming (Chapter 6)Page 6 Collections Collection Types: Associative Arrays Associative arrays are sparsely populated structures, indexed by unique numbers or strings. Associative arrays are sparsely populated structures, indexed by unique numbers or strings. Associative arrays have no maximum size. Associative arrays have no maximum size. Associative arrays cannot be used as column data types. Associative arrays cannot be used as column data types.

7 2006 Oracle Database PL/SQL 10g Programming (Chapter 6)Page 7 Collections Collection Types: Multiset Operators MULTISET EXCEPT This operator removes one set from another, like the SQL MINUS operator. MULTISET EXCEPT This operator removes one set from another, like the SQL MINUS operator. MULTISET INTERSECT This operator takes two sets and merges them into a new set that contains one copy of elements found in both original sets, like the SQL INTERSECT operator. MULTISET INTERSECT This operator takes two sets and merges them into a new set that contains one copy of elements found in both original sets, like the SQL INTERSECT operator. MULTISET UNION This operator takes two sets and merges them into a new set without eliminating duplicate values, like the UNION ALL operator. MULTISET UNION This operator takes two sets and merges them into a new set without eliminating duplicate values, like the UNION ALL operator. SET This operator removes duplicates from a set and acts like the DISTINCT operator in a SQL statement. SET This operator removes duplicates from a set and acts like the DISTINCT operator in a SQL statement.

8 2006 Oracle Database PL/SQL 10g Programming (Chapter 6)Page 8 Collections VARRAY Collection Types: Rules VARRAY data types can be defined: VARRAY data types can be defined: As PL/SQL user-defined types. As PL/SQL user-defined types. As SQL collection data types of scalar variables. As SQL collection data types of scalar variables. As SQL collection data types of compound, object type, variables. As SQL collection data types of compound, object type, variables. VARRAY data types require explicit construction. VARRAY data types require explicit construction. VARRAY data types allocate space at construction or by calling the Oracle Collection API EXTEND methods to allocate space for an element or set of elements. VARRAY data types allocate space at construction or by calling the Oracle Collection API EXTEND methods to allocate space for an element or set of elements.

9 2006 Oracle Database PL/SQL 10g Programming (Chapter 6)Page 9 Collections VARRAY Collection Types: SQL Declaration -- Declare VARRAY SQL data type. CREATE [OR REPLACE] TYPE number_list AS VARRAY(3) OF NUMBER;

10 2006 Oracle Database PL/SQL 10g Programming (Chapter 6)Page 10 Collections VARRAY Collection Types: PL/SQL Declaration DECLARE -- Declare VARRAY PL/SQL data type. -- Declare VARRAY PL/SQL data type. TYPE number_list IS VARRAY(3) OF NUMBER; TYPE number_list IS VARRAY(3) OF NUMBER; -- Create a list without any allocated space. -- Create a list without any allocated space. empty_scalar_list NUMBER_LIST := NUMBER_LIST(); empty_scalar_list NUMBER_LIST := NUMBER_LIST(); -- Create a list of NULL values with two allocated space. -- Create a list of NULL values with two allocated space. null_scalar_list NUMBER_LIST := NUMBER_LIST(NULL,NULL); null_scalar_list NUMBER_LIST := NUMBER_LIST(NULL,NULL); -- Create a list of values with three allocated space. -- Create a list of values with three allocated space. value_scalar_list NUMBER_LIST := NUMBER_LIST(1,2,3); value_scalar_list NUMBER_LIST := NUMBER_LIST(1,2,3);BEGIN … next_slide … … next_slide …END;/

11 2006 Oracle Database PL/SQL 10g Programming (Chapter 6)Page 11 Collections VARRAY Collection Types: PL/SQL Assignment DECLARE … prior_slide … … prior_slide …BEGIN empty_scalar_list.EXTEND; empty_scalar_list.EXTEND; empty_scalar_list(1) := 1829; empty_scalar_list(1) := 1829;END;/

12 2006 Oracle Database PL/SQL 10g Programming (Chapter 6)Page 12 Collections Nested Table Collection Types: Rules Nested table data types can be defined: Nested table data types can be defined: As PL/SQL user-defined types. As PL/SQL user-defined types. As SQL collection data types of scalar variables. As SQL collection data types of scalar variables. As SQL collection data types of compound, object type, variables. As SQL collection data types of compound, object type, variables. Nested table data types require explicit construction. Nested table data types require explicit construction. Nested table data types allocate space at construction or by calling the Oracle Collection API EXTEND methods to allocate space for an element or set of elements. Nested table data types allocate space at construction or by calling the Oracle Collection API EXTEND methods to allocate space for an element or set of elements.

13 2006 Oracle Database PL/SQL 10g Programming (Chapter 6)Page 13 Collections Nested Table Collection Types: SQL Declaration -- Declare VARRAY SQL data type. CREATE [OR REPLACE] TYPE number_list AS TABLE OF NUMBER;

14 2006 Oracle Database PL/SQL 10g Programming (Chapter 6)Page 14 Collections Nested Table Collection Types: PL/SQL Declaration DECLARE -- Declare VARRAY PL/SQL data type. -- Declare VARRAY PL/SQL data type. TYPE number_list IS TABLE OF NUMBER; TYPE number_list IS TABLE OF NUMBER; -- Create a list without any allocated space. -- Create a list without any allocated space. empty_scalar_list NUMBER_LIST := NUMBER_LIST(); empty_scalar_list NUMBER_LIST := NUMBER_LIST(); -- Create a list of NULL values with two allocated space. -- Create a list of NULL values with two allocated space. null_scalar_list NUMBER_LIST := NUMBER_LIST(NULL,NULL); null_scalar_list NUMBER_LIST := NUMBER_LIST(NULL,NULL); -- Create a list of values with three allocated space. -- Create a list of values with three allocated space. value_scalar_list NUMBER_LIST := NUMBER_LIST(1,2,3); value_scalar_list NUMBER_LIST := NUMBER_LIST(1,2,3);BEGIN … next_slide … … next_slide …END;/

15 2006 Oracle Database PL/SQL 10g Programming (Chapter 6)Page 15 Collections Associative Array Collection Types: Rules Associative Array data types can be defined as PL/SQL user-defined types. Associative Array data types can be defined as PL/SQL user-defined types. Associative Array data types cannot be defined as SQL collection data types. Associative Array data types cannot be defined as SQL collection data types. Associative Array data types do not require explicit construction. Associative Array data types do not require explicit construction. Associative Array data types require element by element assignment, or bulk assignments. Associative Array data types require element by element assignment, or bulk assignments. Associative Array data types do not require explicit space allocation. Associative Array data types do not require explicit space allocation. Associative Array data types can use the Oracle Collection API. Associative Array data types can use the Oracle Collection API.

16 2006 Oracle Database PL/SQL 10g Programming (Chapter 6)Page 16 Collections Nested Table Collection Types: PL/SQL Usage DECLARE -- Declare VARRAY PL/SQL data type. -- Declare VARRAY PL/SQL data type. TYPE number_list IS TABLE OF NUMBER TYPE number_list IS TABLE OF NUMBER INDEX BY BINARY_INTEGER; INDEX BY BINARY_INTEGER; -- Declare a variable. -- Declare a variable. empty_scalar_list NUMBER_LIST; empty_scalar_list NUMBER_LIST;BEGIN empty_scalar_list(1) := 1829; empty_scalar_list(1) := 1829;END;/

17 2006 Oracle Database PL/SQL 10g Programming (Chapter 6)Page 17 Collections COLLECTION API: Methods COUNT a method that returns the number of elements in a collection. COUNT a method that returns the number of elements in a collection. DELETE(n) a method that takes a single formal parameter that is an index value, and it removes the element pointed to by the equivalent index value. DELETE(n) a method that takes a single formal parameter that is an index value, and it removes the element pointed to by the equivalent index value. DELETE(n,m) a method that takes two formal parameter index values, and it removes a range of elements pointed to by the equivalent index value. DELETE(n,m) a method that takes two formal parameter index values, and it removes a range of elements pointed to by the equivalent index value. EXISTS(n) a method that takes one formal parameter index value, and returns TRUE if found in the collection and FALSE if not. If the collection is a null element structure, the method also returns FALSE. EXISTS(n) a method that takes one formal parameter index value, and returns TRUE if found in the collection and FALSE if not. If the collection is a null element structure, the method also returns FALSE.

18 2006 Oracle Database PL/SQL 10g Programming (Chapter 6)Page 18 Collections COLLECTION API: Methods EXTEND a method that takes no formal parameter and extends space for one new element. EXTEND a method that takes no formal parameter and extends space for one new element. EXTEND(n) a method that takes one formal parameter, which designates how many spaces to extend the collection. EXTEND(n) a method that takes one formal parameter, which designates how many spaces to extend the collection. EXTEND(n,m) a method that takes two formal parameters; the first designates how many spaces to extend, and the second identifies an index to copy into the newly indexed spaces. EXTEND(n,m) a method that takes two formal parameters; the first designates how many spaces to extend, and the second identifies an index to copy into the newly indexed spaces. FIRST a method that takes no formal parameter and returns the first index value, this is the lowest number for numeric indexes and lowest value string for string indexes. FIRST a method that takes no formal parameter and returns the first index value, this is the lowest number for numeric indexes and lowest value string for string indexes. LAST a method that takes no formal parameter and returns the last index value by using opposite rules to the FIRST method. LAST a method that takes no formal parameter and returns the last index value by using opposite rules to the FIRST method.

19 2006 Oracle Database PL/SQL 10g Programming (Chapter 6)Page 19 Collections COLLECTION API: Methods LIMIT a method that returns the highest allowed element number in a VARRAY collection. LIMIT a method that returns the highest allowed element number in a VARRAY collection. NEXT(n) a method that takes a single formal parameter that is an index value, and it returns the next value in the collection. NEXT(n) a method that takes a single formal parameter that is an index value, and it returns the next value in the collection. PRIOR(n) a method that takes a single formal parameter that is an index value, and returns the prior indexed value in the collection. PRIOR(n) a method that takes a single formal parameter that is an index value, and returns the prior indexed value in the collection. TRIM a method that takes no formal parameter, and removes the highest subscripted value from a collection. TRIM a method that takes no formal parameter, and removes the highest subscripted value from a collection. TRIM(n) a method that takes one formal parameter, which is an INTEGER ; and it removes that number of subscripted values from the end of a collection. TRIM(n) a method that takes one formal parameter, which is an INTEGER ; and it removes that number of subscripted values from the end of a collection.

20 2006 Oracle Database PL/SQL 10g Programming (Chapter 6)Page 20 Collections COLLECTION API: Exceptions COLLECTION_IS_NULL COLLECTION_IS_NULL Raised when attempting to access a null collection. Raised when attempting to access a null collection. NO_DATA_FOUND NO_DATA_FOUND Raised when attempting to access values that are not present in an initialized collection. Raised when attempting to access values that are not present in an initialized collection. SUBSCRIPT_BEYOND_COUNT SUBSCRIPT_BEYOND_COUNT Raised when attempting to access beyond the highest subscripted value. Raised when attempting to access beyond the highest subscripted value. SUBSCRIPT_OUTSIDE_LIMIT SUBSCRIPT_OUTSIDE_LIMIT Raised when attempting to access beyond a VARRAY index value limit. Raised when attempting to access beyond a VARRAY index value limit. VALUE_ERROR VALUE_ERROR Raised when attempting to cast an incorrect data type to the index type of the subscript. Raised when attempting to cast an incorrect data type to the index type of the subscript.

21 2006 Oracle Database PL/SQL 10g Programming (Chapter 6)Page 21 Summary Collection Types Collection Types VARRAY Collections VARRAY Collections Nested Table Collections Nested Table Collections Associative Array Collections Associative Array Collections Collection API Collection API


Download ppt "Collections Oracle Database PL/SQL 10g Programming Chapter 6."

Similar presentations


Ads by Google