145.Oracle数据库SQL开发之 集合——10g对集合的增强
欢迎转载,转载请标明出处:blog.csdn.net/notbaron/ar…\
10g对集合的增强,包括:
l 支持关联数组
l 更改元素类型的大小或精度的能力
l 增加变长数组中元素数目的能力
l 在临时表中使用变长数组列的能力
l 为嵌套表的存储表使用不同表空间的能力
l 实现嵌套表对ANSI的支持
1. 关联数组
关联数组是一个键值对集合。可以使用键或一个整数获得数组中的值。
CREATE PROCEDURE customers_associative_array AS
-- define anassociative array type named t_assoc_array;
-- the valuestored in each array element is a NUMBER,
-- and theindex key to access each element is a VARCHAR2
TYPEt_assoc_array IS TABLE OF NUMBER INDEX BY VARCHAR2(15);
-- declare anobject named v_customer_array of type t_assoc_array;
--v_customer_array will be used to store the ages of customers
v_customer_array t_assoc_array;
BEGIN
-- assign thevalues to v_customer_array; the VARCHAR2 key is the
-- customername and the NUMBER value is the age of the customer
v_customer_array('Jason') := 32;
v_customer_array('Steve') := 28;
v_customer_array('Fred') := 43;
v_customer_array('Cynthia') := 27;
-- displaythe values stored in v_customer_array
DBMS_OUTPUT.PUT_LINE(
'v_customer_array[''Jason''] = ' || v_customer_array('Jason')
);
DBMS_OUTPUT.PUT_LINE(
'v_customer_array[''Steve''] = ' || v_customer_array('Steve')
);
DBMS_OUTPUT.PUT_LINE(
'v_customer_array[''Fred''] = ' || v_customer_array('Fred')
);
DBMS_OUTPUT.PUT_LINE(
'v_customer_array[''Cynthia''] = ' || v_customer_array('Cynthia')
);
END customers_associative_array;
/
调用customers_associative_array
collection_user3@PDB1> set serveroutput on
collection_user3@PDB1> call customers_associative_array();
v_customer_array['Jason'] = 32
v_customer_array['Steve'] = 28
v_customer_array['Fred'] = 43
v_customer_array['Cynthia'] = 27
Call completed.
2. 更改元素类型的大小
当元素类型是字符、数字或原始类型其中之一时,可以更改集合中元素类型的大小。
创建变长数组类型如下:
CREATE TYPE t_varray_address AS VARRAY(3) OFVARCHAR2(50);
/
修改VARCHAR2元素大小更改为60:
collection_user3@PDB1> alter type t_varray_addressmodify element type varchar2(60) cascade;
Type altered.
CASCADE选项把更改传播到数据库中的任何依赖对象。
3. 增加变长数组的元素数目
可以增加变长数组的元素数目:
collection_user3@PDB1> alter type t_varray_addressmodify limit 5 cascade;
Type altered.
4. 在临时表中使用变长数组
可以在临时表中使用变长数组。如下:
CREATEGLOBAL TEMPORARY TABLE cust_with_varray_temp_table (
id INTEGER PRIMARY KEY,
first_name VARCHAR2(10),
last_name VARCHAR2(10),
addresses t_varray_address
);
5. 为嵌套表的存储表使用不同的表空间
默认情况下,嵌套表的存储表创建在与嵌套表的父表相同的表空间中。
10g 中,可以为嵌套表的存储表指定一个不同的表空间。
CREATE TABLE cust_with_nested_table (
id INTEGER PRIMARY KEY,
first_nameVARCHAR2(10),
last_name VARCHAR2(10),
addresses t_nested_table_address
)
NESTED TABLE
addresses
STORE AS
nested_addresses2 TABLESPACE users;
6. 嵌套表对ANSI的支持
美国国家标准协会ANSI规范包括许多可用于嵌套表的操作符。
SUBMULTISET操作符用于检查一个嵌套表的元素是否是另一个嵌套表的子集。
MULTISET操作符用于获得一个嵌套表
CARDINALITY函数返回集合中的元素数目
MEMBER OF操作符用于检查嵌套表的一个元素是否存在
SET函数首先将嵌套表转换为一个集合,然后删除集合中重复的元素,最后将集合作为嵌套表返回。
IS A SET操作符用于检查嵌套表中的元素是否是各不相同的。
IS EMPTY 操作符用于检查嵌套表是否不包含任何元素。
COLLECT函数从一个元素集返回一个嵌套表
POWERMULTISET函数返回给定嵌套表中元素的所有组合。
POWERMULTISET_BY_CARDINALITY函数返回给定嵌套表中元素的某些组合,这些组合都具有特定的元素数量。