日期:2014-05-16  浏览次数:20476 次

Oracle 集合学习笔记

关联数组:

一维的,无界限的,同种元素的稀疏集合,只在PL/SQL中可用。

在oracle 8i中称为索引表,9i后改为关联数组。

?

嵌套表:

一维的,无界限的,同种元素的稀疏集合;

初始化是紧密的,可能被删除能稀疏的。嵌套表在PL和数据库里使用。

??? 嵌套表是多重表,里面元素没有继承顺序。

?

变长数组:

一维的同种元素集合。有限的,并且不是稀疏的。当定义变长数组时必须指定最大长度。

可以PL/SQL及数据库里使用。变长数组是保留顺序的。

?

?

?选择合适的集合类型。

普遍情况:在都能使用的情况下,优先使用关联数组,因为它能减少coding.不用初始化,不用extend.性能最高(在老的数据库版本中)。如果有储存到数据里的话只能使用嵌套表或是变长数组。

?

Guidelines:

  • 稀疏数组使用关联数组
  • 下标使用负数,使用关联数组。
  • 使用Oracle 10g的话,如果要使用高层集合操作,更倾向使用嵌套表而不是关联数组
  • 在表中储存少量数据使用变长数组
  • 表中储存大量数据使用嵌套表
  • 想要保证数据储存的顺序,并且是小数据量的话使用变长数组。“小”是指能储存到一个block里
  • 在使用变长数组时,不用担心删除中间数据。

?

Table 12-2. Comparing Oracle collection types

Characteristic

Associative array

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)

VARCHAR2 (Oracle9i Database Release 2 and above)

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