Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Programming PL SQL.doc
Скачиваний:
3
Добавлен:
01.07.2025
Размер:
5 Мб
Скачать
☆

11.4.5 Collections as Attributes of an Object Type

In this example, we are modeling automobile specifications. Each Auto_spec_ t object will include a list of manufacturer's colors in which you can purchase the vehicle.

CREATE TYPE Auto_spec_t AS OBJECT (

make VARCHAR2(30),

model VARCHAR2(30),

available_colors Color_tab_t

);

Because there is no data storage required for the object type, it is not necessary to designate a name for the companion table at the time we issue the CREATE TYPE . . . AS OBJECT statement.

When the time comes to implement the type as, say, an object table, you could do this:

CREATE TABLE auto_specs OF Auto_spec_t

NESTED TABLE available_colors STORE AS available_colors_st;

This statement requires a bit of explanation. When you create a "table of objects," Oracle looks at the object type definition to determine what columns you want. When it discovers that one of the object type's attributes, available_colors, is in fact a nested table, Oracle treats this table as it did in earlier examples; in other words, it wants to know what to name the store table. So the phrase:

...NESTED TABLE available_colors STORE AS available_colors_st

says that you want the available_colors column to have a store table named available_colors_st.

See Chapter 21 for more information about Oracle object types.

11.5 Collection Built-Ins (Methods)

PL/SQL offers a number of built-infunctions and procedures, known as collection methods, that let you obtain information about and modify the contents of collections. Here is the complete list of these programs:

Method (function or procedure)

Description

COUNT function

Returns the current number of elements in a collection.

DELETE procedure

Removes one or more elements from the "middle" of a nested table. Reduces COUNT if the element is not already DELETEd. Can be used for VARRAYS, but only to delete the entire contents of the collection.

EXISTS function

Returns TRUE or FALSE to indicate whether the specified element exists.

EXTEND procedure

Increases the number of elements in a collection. Increases COUNT. Does not apply to associative arrays.

FIRST, LASTfunctions

Return the smallest (FIRST) and largest (LAST) subscripts in use.

LIMIT function

Returns the maximum number of allowed elements in a VARRAY.

PRIOR, NEXTfunctions

Return the subscript immediately before (PRIOR) or after (NEXT) a specified subscript. You should always use PRIOR and NEXT to traverse a collection, especially if you are working with sparse (or potentially sparse) collections.

TRIM procedure

Removes collection elements at the "end" of the collection. Reduces COUNT if elements are not DELETEd.

These programs are referred to as "methods" because the syntax for using the collection built-ins is different from the normal syntax used to call procedures and functions. Collection methods employ a "member method" syntax that's common in object-oriented languages such as C++.

To give you a feel for member-method syntax, consider the LAST function. It returns the greatest index value in use in the associative array. Using standard function syntax, you might expect to call LAST as follows:

IF LAST (company_table) > 10 THEN ... /* Invalid syntax */

In other words, you would pass the associative array as an argument. In contrast, by using the member-method syntax, the LAST function is a method that "belongs to" the object—in this case, the associative array. So the correct syntax for using LAST is:

IF company_table.LAST > 10 THEN ... /* Correct syntax */

The general syntax for calling these associative array built-ins is either of the following:

  • An operation that takes no arguments:

table_name.operation

  • An operation that takes a row index for an argument:

table_name.operation(index_number [, index_number])

The following statement, for example, returns TRUE if the 15th row of the company_tab associative array is defined:

company_tab.EXISTS(15)

The collection methods are not available from within SQL; they can be used only in PL/SQL programs.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]