标签:dep sql spl 其他 declare 对象类型 ctf for 复合
嵌套表
嵌套表是一种类似于索引表的结构,也可以用于保存多个数据,而且也可以保存复合类型的数据
嵌套表指的是一个数据表定义事同时加入了其他内部表的定义,这一概念是在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 ; 从回收站删除
标签:dep sql spl 其他 declare 对象类型 ctf for 复合
原文地址:https://www.cnblogs.com/kawashibara/p/9094052.html