嵌套表用法詳解(PLSQL)
嵌套表
嵌套表是一種類似於索引表的結構,也可以用於保存多個數據,而且也可以保存復合類型的數據
嵌套表指的是一個數據表定義事同時加入了其他內部表的定義,這一概念是在oracle 8中引入的,它們可以使用SQL進行訪問,也可以進行動態擴展。
創建表指定嵌套表存儲空間名稱
Create table 表名稱(
字段名稱 類型
……
嵌套表字段名稱 嵌套表類型
)NESTED TABLE 嵌套表字段名稱 STORE AS 存儲空間名稱;
定義部門表
DROP TABLE department
Create table department(
Did number,
Deptname varchar(30) not null,
Projects project_nested,
Constraint pk_did primary key(did)
)NESTED TABLE projects STORE AS projects_nested_table;
創建新的對象類型
Create type 類型名稱 AS OBJECT(
列名稱 數據類型,
列名稱 數據類型,
……
列名稱 數據類型
);
/
範例:創建一個表示項目類型的對象
Create or replace type kingsql_type as object(
Projectid number,
Projectname varchar(50),
Projectfunds number,
Pubdate date
);
/
CREATE OR REPLACE TYPE kingsql_type AS OBJECT
projectid NUMBER ,
projectname VARCHAR(50),
projectfunds NUMBER ,
pubdate DATE
) ;
定義嵌套表類型——project_nested
CREATE OR REPLACE TYPE kingsql_nested AS TABLE OF kingsql_type NOT NULL ;
CREATE TABLE department_01 (
did NUMBER ,
deptname VARCHAR(50) NOT NULL ,
qt_column kingsql_nested
) NESTED TABLE qt_column STORE AS kingsql_nested_t1
projectid NOT NULL ,
projectname NOT NULL ,
projectfunds NOT NULL ,
pubdate NOT NULL)) ;
嵌套表插入數據
insert into department_01 values(
1,--第一列
‘hehe1‘,--第二列
kingsql_nested(kingsql_type(1,‘hehe1‘,1,sysdate),kingsql_type(11,‘hehe11‘,11,sysdate))); 第三列第一行/第三列第二行
插入數據
declare
v1 kingsql_nested:=kingsql_nested(kingsql_type(2,‘haha2‘,2,sysdate));
begin
insert into department_01
values(1,‘hehe1‘,kingsql_nested(kingsql_type(1,‘hehe1‘,1,sysdate),kingsql_type(11,‘hehe11‘,11,sysdate)));
insert into department_01 values(2,‘haha2‘,v1);
end;
/
查詢嵌套表數據
select * from the(select department_01.qt_column from department_01 where did=1);
PROJECTID PROJECTNAME PROJECTFUNDS PUBDATE
---------- -------------------------------------------------- ------------ -------------------
1 hehe1 1 2018-05-21 14:35:32
11 hehe11 11 2018-05-21 14:35:32
select * from the(select department_01.qt_column from department_01 where did=2);
PROJECTID PROJECTNAME PROJECTFUNDS PUBDATE
---------- -------------------------------------------------- ------------ -------------------
2 haha2 2 2018-05-21 14:35:32
select * from the(select department_01.qt_column from department_01 where did=1) where projectid=1;
PROJECTID PROJECTNAME PROJECTFUNDS PUBDATE
---------- -------------------------------------------------- ------------ -------------------
1 hehe1 1 2018-05-21 14:35:32
直接插入嵌套表數據
insert into the(select department_01.qt_column from department_01 where did=1) values(111,‘hehe111‘,111,sysdate);
Select * from the(select department_01.qt_column from department_01 where did=1);
循環插入一百行
declare
x number:=3;
begin
for x in 3..103 loop
insert into the(select department_01.qt_column from department_01 where did=2) values(x,‘hehe‘,111,sysdate);
end loop;
commit;
end;
直接更新嵌套表數據
update the(select department_01.qt_column from department_01 where did=1) set projectid=1111 where projectid=1;
commit;
select * from the(select department_01.qt_column from department_01 where did=1)
直接刪除嵌套表數據
delete the(select department_01.qt_column from department_01 where did=1) where projectid=1111;
commit;
select * from the(select department_01.qt_column from department_01 where did=1);
DROP TABLE department_01 PURGE ; 從回收站刪除
嵌套表用法詳解(PLSQL)