ant3000 发表于 2005-6-1 13:57

Oracle学习笔记---(五)

<DIV class=postTitle><a href="http://www.donews.net/ant3000/archive/2005/05/17/384949.aspx" target="_blank" >Oracle学习笔记---(五)</A> </DIV>
<DIV class=postText>
<P align=center>五</P>
<P>一,定义抽象数据类型
/*
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)     --爱好
);</P>
<P>    desc  animal_ty;
    查看  user_types;
          user_type_attrs;</P>
<P>二,抽象数据类型的使用</P>
<P>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'));</P>
<P>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';</P>
<P>2,删除对象类型
   drop type animal_ty force;</P>
<P>3,查询相关性
   select name,type from user_dependencies where referenced_name='ANIMAL_TY';  </P>
<P>4,在抽象数据类型上创建索引
  create index  Idx  on animal(anima.name);</P>
<P>5,final 和 not final   修饰类型
  instantiable 和not instantiable  可修饰类型和方法</P>
<P>默认情况下,抽象数据类型是不能被继承的,同时类型提供构造函数,类型中的方法可以被实</P>
<P>现

  声明类型加 not final 该类型可被继承     
  修饰方法: not instantiable 类型不提供方法实现
  修饰类型: not instantiable 类型没有构造函数.不能实例化</P>
<P>二,可变数组</P>
<P> 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');
   
三,嵌套表</P>
<P> 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;</P>
<P>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;</P>
<P>--插入数据</P>
<P> insert into ntdept values
(10,'市场部','海珠北路',emps_nt(emp_ty(100,'MARRY',1000),
                                 emp_ty(101,'Lili',1500),
                                 emp_ty(102,'KHK',3000))
);</P>
<P>
insert into ntdept values(20,'教务部','海珠南路',emps_nt(emp_ty(103,'JAKE',2200),
                                                          emp_ty(104,'ROSE',1000),
                                                          emp_ty(105,'LUSU',2000)
));</P>
<P>--查询
  可以使用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 </P>
<P>nt.empno=103  ;
  select nt.empno,nt.ename from table(select deptno from ntdept where  deptno=10) </P>
<P>nt    where   nt.empno=101;
--插入
  insert into table(select dno_det from ntdept where deptno=10)  </P>
<P>values(106,'HANG',3000);
  或从嵌套表中选择数据插入
  insert into ntdept  values(40,'NEWd','NEWloc',
  cast(multiset(select * from table(select dno_det from ntdept where   deptno=10) </P>
<P>nt where nt.empno=100 )  as emps_nt))</P>
<P>  cast:将查询的结果转换成嵌套表的形式
  multiset:允许cast查询中包含多条记录</P>
<P>--更新
  update table(select dno_det from ntdept where deptno=10) nt set nt.ename='LLLL'  </P>
<P> where nt.empno=101;
--删除
  delete from the(select dno_det from ntdept where deptno=10) nt where </P>
<P>nt.empno=101</P>
<P>四,对象表</P>
<P>   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'));</P>
<P>   4,查看oid
    select ref(f) from animal f;
   
   5,查看对象
    select value(r) from animal r;
   
   
使用deref </P>
<P>    drop table keeper;
    create table keeper
    (
     keepername  varchar2(10),
     animalkept  ref animal_ty
    )
     
    插入数据</P>
<P>    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';</P>
<P>    select * from keeper;  还在引用该对象
    update keeper set animalkept=null where animalkept is dangling;--去掉引用
    </P>
<P>五,对象视图</P>
<P>   将dept关系表做为对象表访问</P>
<P>   1,建立对象类型</P>
<P>    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 ;</P>
<P>   3,通过对象视图操纵关系表数据
     insert into dept_view  values(dept_type(60,'aaa','bbb'));
     delete from dept_view where deptno=60;
     结果会反映到关系表中,当改变关系表数据时,也会反映到对象视图中     </P>
<P>     对象视图每行都有一个oid
   
   4,当关系表之间存在引用关系时,也可以用对象视图实现  
     dept 和 emp 是主、外键表,emp表的deptno引用了dept表的deptno
     并且已为关系表dept生成了对象视图,此时关系表的每一个行都具有oid         
   
     dept表的行可以作为行对象通过dept_view视图进行访问,只要为关系表创建
     了对象视图,即可将任何关系表作为行对象进行访问
      
      --make_ref只对所提供的参数应用一个Oracle内部的算法,即可得到引用,它并没有真</P>
<P>正读取视图。
      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 </P>
<P>from emp;   
     该视图第一列指向了对象视图dept_view的行
     
     select deref(a.dep)  from  emp_ov  a;</P>

本文引用通告地址: http://www.donews.net/ant3000/services/trackbacks/384949.aspx
</DIV>

六月飞雪 发表于 2005-9-3 22:22

顶 !!!!!!! 楼主 继续啊

duolanshizhe 发表于 2005-9-10 15:39

好,学习中![em02]

xibeilang 发表于 2005-12-17 20:13

谢楼主啊!

xibeilang 发表于 2005-12-17 20:14

学习中...

faxudo1980 发表于 2006-3-14 12:33

楼主好样的.不知道能否交个朋友!因为我也想学习ORACLE!我的QQ是67315147.希望能向你请教.

tpy9527 发表于 2006-5-2 21:40

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

页: [1]

编程论坛