變長數組是集合數據類型的一種,其使用方法與嵌套表大同小異, 變長數組是一個存儲有序元素的集合,每個元素都有一個索引,該索引相對應元素在數組中的位置。變長數組存在大小的限制,但是可以動態進行更改。
二 變長數組語法
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);;
創 建變長數組類型在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;
修該變長數組長度範圍在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;
修改變長數組元素的數據長度在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';語句來來查詢。
查詢結果 爲: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;
刪除變長數組類型在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);
創建表在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);
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';
更改表數據在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';
刪除表數據在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')
DEPARTEMENT ----EMPLOYEES------------------------------
2 T_TAB1_EMP('123', '456', '789', '101', '120', '114', '45')