| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1858 人关注过本帖
标题:Oracle学习笔记---(五)
只看楼主 加入收藏
ant3000
Rank: 1
等 级:新手上路
帖 子:188
专家分:0
注 册:2004-6-7
收藏
 问题点数:0 回复次数:6 
Oracle学习笔记---(五)

一,定义抽象数据类型 /* create or replace type animal_ty as object (breed varchar2(25), --动物种类 name varchar2(25), --名字 birthdate date, --出生日期 member function AGE(birthdate in date) return number --根据出生日期计算年龄 ); --带有方法的抽象数据类型 */ create or replace type animal_ty as object (Bread varchar2(25), --动物种类 name varchar2(25), --名字 hobby varchar2(10) --爱好 );

desc animal_ty; 查看 user_types; user_type_attrs;

二,抽象数据类型的使用

1)创建关系表 drop table animal; create table animal (id number primary key , anima animal_ty ); set desc depth 3 desc animal; 2) 插入数据(使用构造方法) insert into animal values(1, animal_ty('MULE','FRANCES','PLAY')); insert into animal values(2, animal_ty('DOG','BENJI','EAT')); insert into animal values(3, animal_ty('CROCODILE','LYLE','SWIM'));

3)操作 查看:select f.anima.name from animal f; 更新: update animal f set f.anima.hobby='PLAY1' where id=1; 删除:delete from animal a where a.anima.hobby='PLAY1';

2,删除对象类型 drop type animal_ty force;

3,查询相关性 select name,type from user_dependencies where referenced_name='ANIMAL_TY';

4,在抽象数据类型上创建索引 create index Idx on animal(anima.name);

5,final 和 not final 修饰类型 instantiable 和not instantiable 可修饰类型和方法

默认情况下,抽象数据类型是不能被继承的,同时类型提供构造函数,类型中的方法可以被实

现 声明类型加 not final 该类型可被继承 修饰方法: not instantiable 类型不提供方法实现 修饰类型: not instantiable 类型没有构造函数.不能实例化

二,可变数组

1)创建VARRAY类型 create or replace type tools_va as varray(5) of varchar2(25); 2)创建关系表,该表的字段类型是varray类型 create table borrower(name varchar2(25) primary key, tools tools_va); 字典 select typecode,attributes from user_types where type_name='TOOLS_VA'; select coll_type,elem_type_owner,elem_type_name,upper_bound, length from user_coll_types; 3)插入数据 insert into borrower values ('JED HOPKINS', Tools_va('HAMMER','SLEDGE','AX')); insert into borrower values('PK', Tools_va('PEN1','PEN2','PEN3')); 4)查看 select * from table(select t.tools from borrower t where t.name='PK'); 三,嵌套表

drop type emp_ty force; 1, create or replace type emp_ty as object ( empno number, ename char(10), sal number); 2,create or replace type emps_nt as table of emp_ty;

3, create table ntdept ( deptno number, dname varchar2(20), loc varchar2(20), dno_det emps_nt) nested table dno_det store as emp_nt_tab;

--插入数据

insert into ntdept values (10,'市场部','海珠北路',emps_nt(emp_ty(100,'MARRY',1000), emp_ty(101,'Lili',1500), emp_ty(102,'KHK',3000)) );

insert into ntdept values(20,'教务部','海珠南路',emps_nt(emp_ty(103,'JAKE',2200), emp_ty(104,'ROSE',1000), emp_ty(105,'LUSU',2000) ));

--查询 可以使用the或table select deptno,dname,loc,dno_det from ntdept; select dno_det from ntdept where deptno=10; select nt.* from table(select dno_det from ntdept where deptno=10) nt; select nt.* from table(select dno_det from ntdept where deptno=20) nt where

nt.empno=103 ; select nt.empno,nt.ename from table(select deptno from ntdept where deptno=10)

nt where nt.empno=101; --插入 insert into table(select dno_det from ntdept where deptno=10)

values(106,'HANG',3000); 或从嵌套表中选择数据插入 insert into ntdept values(40,'NEWd','NEWloc', cast(multiset(select * from table(select dno_det from ntdept where deptno=10)

nt where nt.empno=100 ) as emps_nt))

cast:将查询的结果转换成嵌套表的形式 multiset:允许cast查询中包含多条记录

--更新 update table(select dno_det from ntdept where deptno=10) nt set nt.ename='LLLL'

where nt.empno=101; --删除 delete from the(select dno_det from ntdept where deptno=10) nt where

nt.empno=101

四,对象表

1,建立对象类型 drop type animal_ty force; create type animal_ty as object (name varchar2(15), hobby varchar2(20) ) / 2,建立对象表 create table animal of animal_ty(name constraint pk primary key); 3,插入数据 insert into animal values(animal_ty('SNAKE','PLAY')); insert into animal values(animal_ty('DOG','SWIM')); insert into animal values(animal_ty('CAT','SLEEP'));

4,查看oid select ref(f) from animal f; 5,查看对象 select value(r) from animal r; 使用deref

drop table keeper; create table keeper ( keepername varchar2(10), animalkept ref animal_ty ) 插入数据

insert into keeper select 'JEK',ref(f) from animal f where name='DOG'; insert into keeper select 'BLK',ref(f) from animal f where name='CAT'; 查看数据 在关系表中看对象 select deref(animalkept) from keeper; 假设删掉一个对象 delete from animal where name='DOG';

select * from keeper; 还在引用该对象 update keeper set animalkept=null where animalkept is dangling;--去掉引用

五,对象视图

将dept关系表做为对象表访问

1,建立对象类型

create or replace type dept_type as object (dno number(2), dname varchar2(15), local varchar2(18) ); 2,建立对象视图 create view dept_view of dept_type with object oid (dno) as select * from dept ;

3,通过对象视图操纵关系表数据 insert into dept_view values(dept_type(60,'aaa','bbb')); delete from dept_view where deptno=60; 结果会反映到关系表中,当改变关系表数据时,也会反映到对象视图中

对象视图每行都有一个oid 4,当关系表之间存在引用关系时,也可以用对象视图实现 dept 和 emp 是主、外键表,emp表的deptno引用了dept表的deptno 并且已为关系表dept生成了对象视图,此时关系表的每一个行都具有oid dept表的行可以作为行对象通过dept_view视图进行访问,只要为关系表创建 了对象视图,即可将任何关系表作为行对象进行访问 --make_ref只对所提供的参数应用一个Oracle内部的算法,即可得到引用,它并没有真

正读取视图。 select make_ref(dept_view,deptno) from dept; select make_ref(dept_view,deptno) from emp; 5,通过对象视图建立引用关系 create view emp_ov as select make_ref(dept_view,deptno) dep,empno,ename,sal

from emp; 该视图第一列指向了对象视图dept_view的行 select deref(a.dep) from emp_ov a;

本文引用通告地址: http://www.donews.net/ant3000/services/trackbacks/384949.aspx
搜索更多相关主题的帖子: Oracle 笔记 学习 
2005-06-01 13:57
六月飞雪
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2005-9-1
收藏
得分:0 
顶 !!!!!!! 楼主 继续啊

很想到一个陌生的地方,开始学会遗忘,开始学会成为自己
2005-09-03 22:22
duolanshizhe
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2005-9-10
收藏
得分:0 
好,学习中!
2005-09-10 15:39
xibeilang
Rank: 3Rank: 3
等 级:论坛游侠
威 望:5
帖 子:283
专家分:172
注 册:2005-12-17
收藏
得分:0 
谢楼主啊!

现在不经常码代码了,偶尔过过手瘾。
2005-12-17 20:13
xibeilang
Rank: 3Rank: 3
等 级:论坛游侠
威 望:5
帖 子:283
专家分:172
注 册:2005-12-17
收藏
得分:0 
学习中...

现在不经常码代码了,偶尔过过手瘾。
2005-12-17 20:14
faxudo1980
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2006-3-14
收藏
得分:0 
楼主好样的.不知道能否交个朋友!因为我也想学习ORACLE!我的QQ是67315147.希望能向你请教.
2006-03-14 12:33
tpy9527
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2006-5-2
收藏
得分:0 

我建了2张表,然后有几个属性,
但是我不知道在哪里添加数据?

2006-05-02 21:40
快速回复:Oracle学习笔记---(五)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016016 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved