Oracle数据库中的OOP概念(1)(4)
对象表:每行都代表一个对象,行对象。
创建对象:
create or replace
type address as object(
id number,
street varchar2(100),
state varchar2(2),
zipcode varchar2(11)
)
/
创建对象表:
create table address_table of address
/
desc address_table
插入数据:
可以像关系表一样插入
insert into address_table values(1,’Oracle way’,’CA’,’90001’)
/也可以用默认构造函数插入对象:
insert into address_table
values(address(2,’Oracle way2’,’CA’,’90011’)
select * from address_ table
/VALUE()
以对象表别名作为参数,返回对象实例:
select value(a) from address_table a
/
REF数据类型:
在关系表中关联对象
create table employee_location(
empno number,
loc_ref ref address scope is address_table)
/
loc_ref是个列,类型是指向address对象类型的ref, 即引用,或者指向address实例的指针。
scope is address_table 是可选的,表示ref指向的对象实例的位置,即只能指向address_table对象表中的address对象实例。
REF():
REF()函数可以建立指向对象表中对象实例的REF对象数据类型,以对象表的别名作为参数
插入数据:
insert into employee_location
select 12345, ref(a)
from address_table a
where id=1
/
insert into employee_location
select 45678, ref(a)
from address_table a
where id=2/
OID:
对象表中每一行对象都对应一个唯一的OID,对象标示符
Select * from employee_location
DEREF()
解析REF数据类型,返回真正指向的实例。以REF数据类型作为参数
select empno, deref(loc_ref)
from employee_location
悬空REF:
REF指向的对象实例被删掉了,此时称REF悬空(dangling),说明ref指向不存在的实例
Delete from address_table where id=1;
Select * from employee_location;
Select empno, deref(rec_loc) from employee_location;
悬空的ref会返回null,使用is dangling 确定哪些ref悬空:
select empno from employee_location
where loc_ref is dangling;
清除悬空的ref, 将ref更新为null:
update employee_location
set loc_ref =null
where loc_ref is dangling;
Select * from employee_location;
对象视图:
在已有的关系型表上,建立对象模型:
关系表:
create table item
( itemcode varchar2(10),
item_on_hand number(10),
item_sode number(10)
);建立对象,使用相同的列:
create or replace type item_type as object
( itemcode varchar2(10),
item_on_hand number(10),
item_sode number(10)
);
建立对象视图:
create view item_view of item_type
with object oid (itemcode)
as
select * from item
/
of item_type 说明基于对象
with object oid (itemcode) 明确生成OID
通过视图操作数据:
insert into item_view values(item_type(‘i102’,15,50));
MAKE_REF()
关系主表1:
create table itmefile(
itemcode varchar2(5) primary key,
itemdesc varchar2(20),
p_category varchar2(20),
qty_hand number(5),
re_level number(5),
max_level number(5),
itemrate number(9,2));
关系从表2:
create table order_detail(
orderno varchar2(5),
itemcode varchar2(5) foreign key references itemfile(itemcode)
qty_ord number(5),
qty_deld number(5)
)
- 上一篇:磁盘排序对Oracle数据库性能的影响(1)
- 下一篇:ASM数据库自动存储管理浅析



