COLLECTIONVARRAY/NEST TABLE/INDEX BY TABLE

COLLECTIONS官方参考文档:
(http://docs.oracle.com/cd/A87860_01/doc/appdev.817/a77069/04_colls.htm#19834)
(http://www.java2s.com/Tutorial/Oracle/0520__Collections/0020__Introduction.htm)
There are three types of collections:
Varrays,Nested tables,Associative arrays (formerly known as index-by tables)
    A varray is similar to an array in Java.
You can use a varray to store an ordered set of elements having an index associated with it.
The elements in a varray are of the same type.
    A varray has one dimension.
    A varray has a maximum size that you set when creating it.
Elements in a varray can only be modified as a whole, not individually.
You can change the size of a varray later.
The elements stored in a varray are stored with the table when the size of the varray is 4KB or less, otherwise the varray is stored outside of the table.
When a varray is stored with the table, accessing its elements is faster than accessing elements in a nested table.
     A nested table is a table that is embedded within another table.
You can insert, update, and delete individual elements in a nested table.
Because you can modify individual elements in a nested table, this makes them more flexible than a varray.
    A nested table doesn't have a maximum size, and you can store an arbitrary number of elements in a nested table.
The elements for nested tables are stored in separate tables.
Associative arrays is formerly known as index-by tables.
An associative array is a set of key and value pairs.
You can get the value from the array using the key (which may be a string) or an integer.
An associative array is similar to a hash table.
You create a collection type using the SQL DDL CREATE TYPE statement.
Then you can use these types to define columns in a table.
    An associative array is a PL/SQL construct, not a SQL construct.
    An associative array cannot be stored persistently in a table.
You might be asking yourself why you would want to use collections in the first place.
After all, using two tables with a foreign key already allows you to model relationships between data.
The answer is that the data stored in the collection may be accessed more rapidly by the database than if you were to use two tables instead.
Typically, you'll want to use a collection if you have data that is only used by one table.
A collection type whose elements are also a collection type is known as a multilevel collection type.
----------------------------------------------------------------
The following list shows the valid multilevel collection types:
A nested table containing a nested table type
A nested table containing a varray type
A varray containing a varray type
A varray containing a nested table type
A varray or nested table of an object type that has an attribute that is a varray or nested table type
----------------------------------------------------------------
A series of collection methods can be used to determine the size, and the rows populated, in any collection datatype: index-by tables, VARRAYs, and nested tables. The following is a list of the collection methods and their purposes:

EXISTS(row) returns TRUE if the row specified exists.
COUNT returns the number of rows.
FIRST returns the row number of the first populated row.
LAST returns the row number of the last populated row.
PRIOR(row) returns the row number of the last row populated before the row specified.
NEXT(row) returns the row number of the next row populated after the row specified.
DELETE removes all rows.
DELETE(row) removes the specified row.
DELETE(start_row,end_row) removes all rows between and including the start_row and end_row.
TRIM removes the last row.
TRIM(n) removes the last n rows.
EXTEND adds one row.
EXTEND(n) adds n rows.
EXTEND(n,m) adds n copies of row m.

                                                         三种COLLECTION的比较
                                              http://docstore.mik.ua/orelly/oracle/prog2/ch19_01.htm

Table 19.2: Comparing Oracle Collection Types

Characteristic

Index-By Table

Nested Table

VARRAY

Dimensionality

Single

Single

Single

Usable in SQL?

No

Yes

Yes

Usable as column datatype in a table?

No

Yes; data stored "out of line" (in separate table)

Yes; data stored "in line" (in same table)

Uninitialized state

Empty (cannot be null); elements undefined

Atomically null; illegal to reference elements

Atomically null; illegal to reference elements

Initialization

Automatic, when declared

Via constructor, fetch, assignment

Via constructor, fetch, assignment

In PL/SQL, elements referenced via

BINARY_INTEGER

(-2,147,483,647 .. 2,147,483,647)

Positive integer between 1 and 2,147,483,647

Positive integer between 1 and 2,147,483,647

Sparse?

Yes

Initially, no; after deletions, yes

No

Bounded?

No

Can be extended

Yes

Can assign value to any element at any time?

Yes

No; may need to EXTEND first

No; may need to EXTEND first, and cannot EXTEND past upper bound

Means of extending

Assign value to element with a new subscript

Use built-in EXTEND procedure (or TRIM to condense), with no predefined maximum

EXTEND (or TRIM), but only up to declared maximum size

Can be compared for equality?

No

No

No

Retains ordering and subscripts when stored in and retrieved from database?

N/A

No

Yes

The inevitable question is: Which construct should I use? This chapter reviews some examples of the new collections and offers some suggestions in this area. The short answer:

  • Nested tables are more flexible than VARRAYs for table columns.

  • VARRAYs are best when you need bounded arrays that preserve element order.

  • Index-by tables are the only option that allows initial sparseness.

  • If your code must run in both Oracle7 and Oracle8, you can use only index-by tables.

I believe that we are who we choose to be. Nobody‘s going to come and save you, you‘ve got to save yourself. 我相信我们成为怎样的人是我们自己的选择。没有人会来拯救你,你必须要自己拯救自己。
原文地址:https://www.cnblogs.com/caroline/p/COLLECTION.html