一 变长数组概述

       变长数组是集合数据类型的一种,其使用方法与嵌套表大同小异, 变长数组是一个存储有序元素的集合,每个元素都有一个索引,该索引相对应元素在数组中的位置。变长数组存在大小的限制,但是可以动态进行更改。

二 变长数组语法

      TYPE TYPE_NAME IS {VARRAY | VARYING ARRAY} (SIZE_MAX) OF ELEMENT_TYPE[NOT NULL];
 --------TYPE_NAME用于指定变长数组类型名,SIZE_MAX定义变长数组元 素的最大个数,ELEMENT_TYPE用于指定元素的数据类型。
VARRAY_NAME TYPE_NAME; -----VARRAY_NAME用于定义VARRAY变量

三 变长数组特性
  • 变长数组主要的特性是元素的最 大个数具有限制性
  • 变长数组下标固定为1,上限可以扩展
  • 与嵌套表类型,在变长数组声明时自动设置为NULL值,所谓的空值指的是集合本身是空,不是针对它所拥有的
  • 在引用元素前需要对其进行初始化
四 变长数组实例

1)创建变长数组类型


       create type scott.t_tab1_emp as VARRAY(2) of varchar2(30); / ---这个变长数组最多可容纳两个数据,数据的类型为varchar2(50)
       创 建变长数组类型在oracle日志表现为:60  create type scott.t_tab1_emp as VARRAY(2) of varchar2(30);;
    
2)修改变长数组长度范围

        alter type scott.t_tab1_emp modify limit 10 cascade;--修改后变长数组可容纳10个数据
       修该变长数组长度范围在oracle日志表现 为: 53  alter type scott.t_tab1_emp modify limit 10 cascade;

3)修改变长数组元素的数据长度

       alter type scott.t_tab1_emp modify element type varchar2(30) cascade;----修改后变长数组元素的长度为30字节。
       修改变长数组元素的数据长度在oracle日志 表现为:70  alter type scott.t_tab1_emp modify element type varchar2(30) cascade;

4)获取变长数组信息

        desc scott.t_tab1_emp;
        查询结果 为:scott.t_tab1_emp VARRAY(10) OF VARCHAR2(30)
       或者使用select * from all_varrays where TYPE_owner='SCOTT' and type_name ='T_TAB1_EMP';语句来来查询。

5)删除变长数组类型

       drop type scott.t_tab1_emp;
       删除变长数组类型在oracle日志表现 为:28  drop type scott.t_tab1_emp;

五 变长数组表实例
1)使用变长数组创建表 
              
      create table scott.test_coll_varray (departement number,  employees   scott.t_tab1_emp );     ----employees字段类型为变长数组类型,引用使用变长数组名
       创建表在oracle日志表现为:78  create table test_coll_varray (departement number,  employees   t_tab1_emp);

2)插入表数据

       insert into scott.test_coll_varray values(1,scott.t_tab1_emp ('hello','world'));
       insert into scott.test_coll_varray values(2,scott.t_tab1_emp('123','456','789','101','120','114','45'));
       插入行数据在oracle日志表现为:
       97  insert into "SCOTT"."TEST_COLL_VARRAY"("DEPARTEMENT","EMPLOYEES") values ('1',Unsupported Type); ----oracle日志不支持变长数组类型
       97  insert into "SCOTT"."TEST_COLL_VARRAY"("DEPARTEMENT","EMPLOYEES") values ('2',Unsupported Type);

3)更改表数据


       update scott.test_coll_varray   set employees=scott.t_tab1_emp('welcome','to','china') where departement=1;
       更改表数据在oracle日志表现为:164 update "SCOTT"."TEST_COLL_VARRAY" set "EMPLOYEES" = Unsupported Type where "DEPARTEMENT" = '1' and "EMPLOYEES" = Unsupported Type and ROWID = 'AABrn+AAJAAAlRmAAA';

4)删除表数据

       delete from scott.test_coll_varray where departement=1 ;
       删除表数据在oracle日志表现为:134 delete from "SCOTT"."TEST_COLL_VARRAY" where "DEPARTEMENT" = '1' and "EMPLOYEES" = Unsupported Type and ROWID = 'AABrn+AAJAAAlRmAAA';

5)检索表信息

     select * from scott.test_coll_varray;
       DEPARTEMENT                    ----EMPLOYEES------------------------------
             2                        T_TAB1_EMP('123', '456', '789', '101', '120', '114', '45')