Collections Oracle Database PL/SQL 10g Programming Chapter 6.
-
Upload
audra-mason -
Category
Documents
-
view
225 -
download
4
Transcript of Collections Oracle Database PL/SQL 10g Programming Chapter 6.
CollectionsCollectionsOracle Database PL/SQL 10g Oracle Database PL/SQL 10g
ProgrammingProgramming
Chapter 6Chapter 6
20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming
(Chapter 6)(Chapter 6) Page Page 22
CollectionsCollections
Collection TypesCollection Types VARRAYVARRAY Collections Collections Nested Table Collections Nested Table Collections Associative Array CollectionsAssociative Array Collections Collection APICollection API
20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming
(Chapter 6)(Chapter 6) Page Page 33
CollectionsCollections
Collection Types: DefinitionCollection 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 Unordered lists are indexed by unique
strings.strings. Collection elements can be:Collection elements can be:
Scalar variablesScalar variables Compound variablesCompound variables
20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming
(Chapter 6)(Chapter 6) Page Page 44
CollectionsCollections
Collection Types: Collection Types: VARRAYVARRAY
VARRAYVARRAY collections are densely collections are densely populated structures, indexed by populated structures, indexed by sequential numbers.sequential numbers.
VARRAYVARRAY collections have an initial collections have an initial maximum size.maximum size.
VARRAYVARRAY collections can be used as collections can be used as column data types.column data types.
20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming
(Chapter 6)(Chapter 6) Page Page 55
CollectionsCollections
Collection Types: Nested Collection Types: Nested TableTable
Nested tables are densely populated Nested tables are densely populated structures, indexed by sequential structures, indexed by sequential numbers.numbers.
Nested tables are have no maximum Nested tables are have no maximum size.size.
Nested tables can be used as column Nested tables can be used as column data types.data types.
20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming
(Chapter 6)(Chapter 6) Page Page 66
CollectionsCollections
Collection Types: Associative Collection Types: Associative ArraysArrays
Associative arrays are sparsely Associative arrays are sparsely populated structures, indexed by populated structures, indexed by unique numbers or strings.unique numbers or strings.
Associative arrays have no maximum Associative arrays have no maximum size.size.
Associative arrays cannot be used as Associative arrays cannot be used as column data types.column data types.
20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming
(Chapter 6)(Chapter 6) Page Page 77
CollectionsCollections
Collection Types: Multiset Collection Types: Multiset OperatorsOperators
MULTISET EXCEPTMULTISET EXCEPT This operator removes one set This operator removes one set fromfrom another, like the SQL another, like the SQL MINUSMINUS operator.operator.
MULTISET INTERSECTMULTISET INTERSECT This operator takes two sets and This operator takes two sets and merges merges them into a new set that contains them into a new set that contains one one copy of elements found in both copy of elements found in both original sets, like the SQL original sets, like the SQL INTERSECTINTERSECT operator.operator.
MULTISET UNIONMULTISET UNION This operator takes two sets and This operator takes two sets and merges merges them into a new set without them into a new set without eliminating eliminating duplicate values, like the duplicate values, like the UNION UNION ALLALL operator.operator.
SETSET This operator removes duplicates This operator removes duplicates from a from a set and acts like the set and acts like the DISTINCTDISTINCT operator operator in a SQL statement.in a SQL statement.
20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming
(Chapter 6)(Chapter 6) Page Page 88
CollectionsCollections
VARRAY VARRAY Collection Types: RulesCollection Types: Rules
VARRAYVARRAY data types can be defined: 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 As SQL collection data types of compound, object
type, variables.type, variables. VARRAYVARRAY data types require explicit data types require explicit
construction.construction. VARRAYVARRAY data types allocate space at data types allocate space at
construction or by calling the Oracle Collection construction or by calling the Oracle Collection API API EXTENDEXTEND methods to allocate space for an methods to allocate space for an element or set of elements.element or set of elements.
20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming
(Chapter 6)(Chapter 6) Page Page 99
CollectionsCollectionsVARRAY VARRAY Collection Types: SQL Collection Types: SQL
DeclarationDeclaration-- Declare VARRAY SQL data type.-- Declare VARRAY SQL data type.CREATE [OR REPLACE]CREATE [OR REPLACE] TYPETYPE number_listnumber_listAS VARRAY(3) OF NUMBER;AS VARRAY(3) OF NUMBER;
20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming
(Chapter 6)(Chapter 6) Page Page 1010
CollectionsCollectionsVARRAY VARRAY Collection Types: PL/SQL Collection Types: PL/SQL
DeclarationDeclaration
DECLAREDECLARE -- Declare VARRAY PL/SQL data type.-- Declare VARRAY PL/SQL data type. TYPETYPE number_listnumber_list IS VARRAY(3) OF NUMBER;IS VARRAY(3) OF NUMBER;
-- Create a list without any allocated space.-- Create a list without any allocated space. empty_scalar_list empty_scalar_list NUMBER_LISTNUMBER_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 null_scalar_list NUMBER_LISTNUMBER_LIST := := NUMBER_LIST(NULL,NULL);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 value_scalar_list NUMBER_LISTNUMBER_LIST := := NUMBER_LIST(1,2,3);NUMBER_LIST(1,2,3);BEGINBEGIN … … next_slidenext_slide … …END;END;//
20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming
(Chapter 6)(Chapter 6) Page Page 1111
CollectionsCollectionsVARRAY VARRAY Collection Types: PL/SQL Collection Types: PL/SQL
AssignmentAssignment
DECLAREDECLARE
… … prior_slide …prior_slide …
BEGINBEGIN
empty_scalar_list.EXTEND;empty_scalar_list.EXTEND;
empty_scalar_list(1) := 1829;empty_scalar_list(1) := 1829;
END;END;
//
20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming
(Chapter 6)(Chapter 6) Page Page 1212
CollectionsCollectionsNested Table Collection Types: Nested Table Collection Types:
RulesRules 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, As SQL collection data types of compound,
object type, variables.object type, variables. Nested table data types require explicit Nested table data types require explicit
construction.construction. Nested table data types allocate space at Nested table data types allocate space at
construction or by calling the Oracle construction or by calling the Oracle Collection API Collection API EXTENDEXTEND methods to allocate methods to allocate space for an element or set of elements.space for an element or set of elements.
20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming
(Chapter 6)(Chapter 6) Page Page 1313
CollectionsCollectionsNested Table Collection Types: SQL Nested Table Collection Types: SQL
DeclarationDeclaration
-- Declare VARRAY SQL data type.-- Declare VARRAY SQL data type.CREATE [OR REPLACE]CREATE [OR REPLACE] TYPETYPE number_listnumber_listAS TABLE OF NUMBER;AS TABLE OF NUMBER;
20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming
(Chapter 6)(Chapter 6) Page Page 1414
CollectionsCollectionsNested Table Collection Types: PL/SQL Nested Table Collection Types: PL/SQL
DeclarationDeclaration
DECLAREDECLARE -- Declare VARRAY PL/SQL data type.-- Declare VARRAY PL/SQL data type. TYPETYPE number_listnumber_list IS TABLE OF NUMBER;IS TABLE OF NUMBER;
-- Create a list without any allocated space.-- Create a list without any allocated space. empty_scalar_list empty_scalar_list NUMBER_LISTNUMBER_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 null_scalar_list NUMBER_LISTNUMBER_LIST := := NUMBER_LIST(NULL,NULL);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 value_scalar_list NUMBER_LISTNUMBER_LIST := := NUMBER_LIST(1,2,3);NUMBER_LIST(1,2,3);BEGINBEGIN … … next_slidenext_slide … …END;END;//
20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming
(Chapter 6)(Chapter 6) Page Page 1515
CollectionsCollectionsAssociative Array Collection Types: Associative Array Collection Types:
RulesRules Associative Array data types can be defined as Associative Array data types can be defined as
PL/SQL user-defined types.PL/SQL user-defined types. Associative Array data types cannot be defined as Associative Array data types cannot be defined as
SQL collection data types.SQL collection data types. Associative Array data types do not require Associative Array data types do not require
explicit construction.explicit construction. Associative Array data types require element by Associative Array data types require element by
element assignment, or bulk assignments.element assignment, or bulk assignments. Associative Array data types do not require Associative Array data types do not require
explicit space allocation.explicit space allocation. Associative Array data types can use the Oracle Associative Array data types can use the Oracle
Collection API.Collection API.
20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming
(Chapter 6)(Chapter 6) Page Page 1616
CollectionsCollectionsNested Table Collection Types: PL/SQL Nested Table Collection Types: PL/SQL
UsageUsage
DECLAREDECLARE
-- Declare VARRAY PL/SQL data type.-- Declare VARRAY PL/SQL data type.
TYPETYPE number_listnumber_list IS TABLE OF NUMBERIS TABLE OF NUMBER
INDEX BY BINARY_INTEGER;INDEX BY BINARY_INTEGER;
-- Declare a variable.-- Declare a variable.
empty_scalar_list empty_scalar_list NUMBER_LIST;NUMBER_LIST;
BEGINBEGIN
empty_scalar_list(1) := 1829;empty_scalar_list(1) := 1829;
END;END;
//
20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming
(Chapter 6)(Chapter 6) Page Page 1717
CollectionsCollectionsCOLLECTION API: MethodsCOLLECTION API: Methods
COUNTCOUNT a method that returns the number ofa method that returns the number ofelements in a collection.elements in a collection.
DELETE(n)DELETE(n) a method that takes a single formal a method that takes a single formal parameter that is an index value, and parameter that is an index value, and
it it removes the element pointed to by removes the element pointed to by the the equivalent index value.equivalent index value.
DELETE(n,m)DELETE(n,m) a method that takes two formal a method that takes two formal parameter parameter index values, and it removes a range index values, and it removes a range of of elements pointed to by the equivalentelements pointed to by the equivalent
index value.index value. EXISTS(n)EXISTS(n) a method that takes one formal a method that takes one formal
parameter parameter index value, and returns TRUE if found index value, and returns TRUE if found in the in the collection and FALSE if not. If the collection and FALSE if not. If the collection iscollection is a null element structure, the method a null element structure, the method alsoalso returns FALSE.returns FALSE.
20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming
(Chapter 6)(Chapter 6) Page Page 1818
CollectionsCollectionsCOLLECTION API: MethodsCOLLECTION API: Methods
EXTENDEXTEND a method that takes no formal parameter a method that takes no formal parameter and and extends space for one new element.extends space for one new element.
EXTEND(n)EXTEND(n) a method that takes one formal a method that takes one formal parameter, which parameter, which designates how many spaces to extend designates how many spaces to extend the the collection.collection.
EXTEND(n,m)EXTEND(n,m) a method that takes two formal a method that takes two formal parameters; parameters; thethe first designates how many spaces first designates how many spaces to extend, andto extend, and the second identifies an index to copy into the second identifies an index to copy into thethe newly indexed spaces.newly indexed spaces.
FIRSTFIRST a method that takes no formal parameter a method that takes no formal parameter and and returns the first index value, this is the returns the first index value, this is the lowest lowest number for numeric indexes and lowest number for numeric indexes and lowest value string value string for string indexes.for string indexes.
LASTLAST a method that takes no formal parameter a method that takes no formal parameter and and returns the last index value by using returns the last index value by using opposite rules opposite rules to the FIRST method.to the FIRST method.
20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming
(Chapter 6)(Chapter 6) Page Page 1919
CollectionsCollectionsCOLLECTION API: MethodsCOLLECTION API: Methods
LIMITLIMIT a method that returns the highest a method that returns the highest allowed allowed element number in a element number in a VARRAYVARRAY collection.collection.
NEXT(n)NEXT(n) a method that takes a single formal a method that takes a single formal parameter that is an index value, and parameter that is an index value, and
it it returns the next value in the collection.returns the next value in the collection. PRIOR(n)PRIOR(n) a method that takes a single formal a method that takes a single formal
parameter parameter that is an index value, and returns the that is an index value, and returns the prior prior indexed value in the collection.indexed value in the collection.
TRIMTRIM a method that takes no formal a method that takes no formal parameter, and parameter, and removes the highest subscripted value removes the highest subscripted value from a from a collection.collection.
TRIM(n)TRIM(n) a method that takes one formal a method that takes one formal parameter, parameter, which is an which is an INTEGERINTEGER; and it removes ; and it removes that that number of subscripted values from the number of subscripted values from the end of end of a collection.a collection.
20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming
(Chapter 6)(Chapter 6) Page Page 2020
CollectionsCollectionsCOLLECTION API: ExceptionsCOLLECTION API: Exceptions
COLLECTION_IS_NULLCOLLECTION_IS_NULL Raised when attempting to access a null collection.Raised when attempting to access a null collection.
NO_DATA_FOUNDNO_DATA_FOUND Raised when attempting to access values that are not present Raised when attempting to access values that are not present
in an initialized collection.in an initialized collection. SUBSCRIPT_BEYOND_COUNTSUBSCRIPT_BEYOND_COUNT
Raised when attempting to access beyond the highest Raised when attempting to access beyond the highest subscripted value.subscripted value.
SUBSCRIPT_OUTSIDE_LIMITSUBSCRIPT_OUTSIDE_LIMIT Raised when attempting to access beyond a Raised when attempting to access beyond a VARRAYVARRAY index index
value limit.value limit. VALUE_ERRORVALUE_ERROR
Raised when attempting to cast an incorrect data type to the Raised when attempting to cast an incorrect data type to the index type of the subscript.index type of the subscript.
20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming
(Chapter 6)(Chapter 6) Page Page 2121
SummarySummary
Collection TypesCollection Types VARRAYVARRAY Collections Collections Nested Table Collections Nested Table Collections Associative Array CollectionsAssociative Array Collections Collection APICollection API