一,定义抽象数据类型
/*
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;

posted on 2007-06-05 16:17  思净  阅读(370)  评论(1编辑  收藏  举报