关系数据库在不断地发展时,许多数据库引入了面向对象的思想,这其中以Oracle为代表,Oracle9i就号称面向对象数据库。
4、嵌套表
嵌套表是表中之表,一个嵌套表是某些行的集合,它在主表中表示为其中的一列。对主表中的每一条记录,嵌套表可以包含多个行。
5、对象视图
可变数组,是一种集合。一个可变数组是对象的一个集合,其中每个对象都具有相同的数据类型。可变数组的大小由创建时决定。在表中建立可变数组后,可变数组在主表中作为一个列对待。
1
创建对象类型
2 create or replace type ADDRESS as object
3 (
4 PROVINCE varchar ( 10 ),
5 CITY varchar ( 20 ),
6 );
7
8 创建ADDRESS可变数组
9 create or replace type ADDRESS_LIST as varray( 3 ) of ADDRESS;
10
11 创建表并使用可变数组ADDRESS_LIST
12 create table STUDENT
13 (
14 STUNAME varchar ( 20 ),
15 STUADDS ADDRESS_LIST
16 );
17
18 向表中插入数据
19 insert into STUDENT values ( ' improviser ' ,ADDRESS_LIST(ADDRESS( ' 广东省 ' , ' 广州市 ' , ' 江海大道 ' ),
ADDRESS( ' 广东省 ' , ' 潮州市 ' , ' 潮枫路 ' )));
20
21 查询(使用table函数将数据表格形式输出)
22 select * from table ( select s.STUADDS from STUDENT where STUNAME = ' improviser ' );
2 create or replace type ADDRESS as object
3 (
4 PROVINCE varchar ( 10 ),
5 CITY varchar ( 20 ),
6 );
7
8 创建ADDRESS可变数组
9 create or replace type ADDRESS_LIST as varray( 3 ) of ADDRESS;
10
11 创建表并使用可变数组ADDRESS_LIST
12 create table STUDENT
13 (
14 STUNAME varchar ( 20 ),
15 STUADDS ADDRESS_LIST
16 );
17
18 向表中插入数据
19 insert into STUDENT values ( ' improviser ' ,ADDRESS_LIST(ADDRESS( ' 广东省 ' , ' 广州市 ' , ' 江海大道 ' ),
ADDRESS( ' 广东省 ' , ' 潮州市 ' , ' 潮枫路 ' )));
20
21 查询(使用table函数将数据表格形式输出)
22 select * from table ( select s.STUADDS from STUDENT where STUNAME = ' improviser ' );
4、嵌套表
嵌套表是表中之表,一个嵌套表是某些行的集合,它在主表中表示为其中的一列。对主表中的每一条记录,嵌套表可以包含多个行。
1
定义嵌套表方法:
2 create or replace type ADDRESS_TABLE as table of ADDRESS;
3
4 基本对象类型、创建表、查询和插入数据都跟上面可变数组一样
5
6 嵌套表更新 ( table 转化为表再利用表别名操作列进行更新 )
7 update table ( select S.STUADDS from STUDENT S where
8 S.STUNAME = ' improviser ' ) AD set AD.PROVINCE = ' 北京 ' where city = ' 广州 ' ;
9
10 删除嵌套表记录
11 delete from table ( select S.STUADDS from STUDENT S
12 where S.STUNAME = ' improviser ' ) AD where AD.city = ' 潮州 市' ;
13
2 create or replace type ADDRESS_TABLE as table of ADDRESS;
3
4 基本对象类型、创建表、查询和插入数据都跟上面可变数组一样
5
6 嵌套表更新 ( table 转化为表再利用表别名操作列进行更新 )
7 update table ( select S.STUADDS from STUDENT S where
8 S.STUNAME = ' improviser ' ) AD set AD.PROVINCE = ' 北京 ' where city = ' 广州 ' ;
9
10 删除嵌套表记录
11 delete from table ( select S.STUADDS from STUDENT S
12 where S.STUNAME = ' improviser ' ) AD where AD.city = ' 潮州 市' ;
13
比较:可变数组,查询速度快,但是更新困难得整体更新,适用与数据不修改情况,如医生处方。使用嵌套表可以对表中的表内容进行修改而无需进行整体更新操作。
4、对象表
1
首先创建对象类型
2 create or replace type OFFICETYPE as object
3 (
4 id varchar ( 10 ),
5 typename varchar ( 20 )
6 );
7
8 将对象类型转化为对象表
9 create table office of officetype;
10
11 创建对象关系表(使用ref ,指示OID进行对象表关联)
12 create table worker
13 (
14 workerid varchar ( 10 ) primary key ,
15 workername varchar ( 20 ),
16 workeroffice ref officetpey scope is office,
17 phone varchar ( 20 )
18 );
19
20 使用deref 取得关联对象表相关内容
21 select workerid,workername,deref(w.workeroffice),phone from worker w
where workerid = ' C001 ' ;
22 结果为
23 C001 张小明 OFFICETYPE( ' 0001 ' , ' 财务科 ' ) 010 - 12345
24
25 使用VALUE(别名)查询对象内容
26 select value(o) from office o;
27
2 create or replace type OFFICETYPE as object
3 (
4 id varchar ( 10 ),
5 typename varchar ( 20 )
6 );
7
8 将对象类型转化为对象表
9 create table office of officetype;
10
11 创建对象关系表(使用ref ,指示OID进行对象表关联)
12 create table worker
13 (
14 workerid varchar ( 10 ) primary key ,
15 workername varchar ( 20 ),
16 workeroffice ref officetpey scope is office,
17 phone varchar ( 20 )
18 );
19
20 使用deref 取得关联对象表相关内容
21 select workerid,workername,deref(w.workeroffice),phone from worker w
where workerid = ' C001 ' ;
22 结果为
23 C001 张小明 OFFICETYPE( ' 0001 ' , ' 财务科 ' ) 010 - 12345
24
25 使用VALUE(别名)查询对象内容
26 select value(o) from office o;
27
5、对象视图
1
创建对象视图(通过OID连接dept)
2 create view deptview of deptype with object oid(deptno) as select * from dept;
3 create view emp_view as select make_ref(deptview,deptno) deptoid,empno,ename
4 from emp;
2 create view deptview of deptype with object oid(deptno) as select * from dept;
3 create view emp_view as select make_ref(deptview,deptno) deptoid,empno,ename
4 from emp;