嵌套表,索引组织表(IOT)

来源:互联网 发布:新浪软件下载中心 编辑:程序博客网 时间:2024/05/02 00:26

索引组织表(IOT)是将数据和索引放在一起存储.而嵌套表, 理解为使用表类型来组织数据.

嵌套表是集合数据类型的一种,oracle提供嵌套表这样的数据结构,可能是弥补了数据库原理上的关系数据模型的不足。嵌套表可以被存储在数据库中。

定义嵌套表:

CREATE type  student as object
( vno VARCHAR2(10),
  vname VARCHAR2(10),
  vsex VARCHAR2(2),
  vaddress VARCHAR2(40)
  );
  /
CREATE TYPE studentlist AS TABLE OF student;
/
CREATE TABLE nestedtable_student
( vid VARCHAR2(10),
  vbj VARCHAR2(14),
  students studentlist
 )
 nested table students store as students_tab;
使用嵌套表(赋值):
SET SERVEROUTPUT ON;
DECLARE
--声明并初始化一个studentlist类型的嵌套表
v_student studentlist:=studentlist(student('200701','赵二麻子','男','广州大街'),student('200702','王三胖子','女','沈阳大街'));
BEGIN
  --DBMS_OUTPUT.PUT_LINE(v_student.vname);
 /*用嵌套表变量v_student给数据库表赋值*/
 INSERT INTO nestedtable_student(vid,vbj,students)  VALUES('01','计算机软件',v_student);
 /*也可以这样赋值*/ 
 INSERT INTO nestedtable_student(vid,vbj,students)
  VALUES('02','网络工程',studentlist(student('200703','张三子','女','北京大街'),student('200704','李小子','男','潮阳大街')));
 COMMIT;
 END;
显示带有嵌套表的数据库表内容:
SET SERVEROUTPUT ON;
DECLARE
v_vid nestedtable_student.vid%type;
v_vbj nestedtable_student.vbj%type;
v_student nestedtable_student.students%type;
CURSOR mycursor IS SELECT vid,vbj,students FROM nestedtable_student;
BEGIN
OPEN mycursor;
LOOP
  FETCH mycursor INTO v_vid,v_vbj,v_student;
EXIT WHEN mycursor%NOTFOUND;
  DBMS_OUTPUT.PUT_LINE(v_vid||v_vbj);
  FOR key_num IN 1..v_student.COUNT LOOP
   DBMS_OUTPUT.PUT_LINE(v_student(key_num).vno||v_student(key_num).vname||v_student(key_num).vsex||v_student(key_num).vaddress);
  END LOOP;
END LOOP;
CLOSE MYCURSOR;
END;
如果想仅仅显示数据库表中的嵌套表的记录可以用运算符the:
select *
from the(select students from nestedtable_student where vid='01')
where vno='200101'

 

 

 

 

举例说明嵌套表的使用:
  假设有一个关于动物饲养员的表,希望其中具有他们饲养的动物的信息。用一个嵌套表,就可以在同一个表中存储饲养员和其饲养的全部动物的信息。
1、创建类型animal_ty:此类型中,对于每个动物都包含有一个记录,记载了其品种、名称和出生日期信息。

CREATE TYPE animal_ty AS OBJECT
( breed varchar2(25),
  name varchar2(25),
  birthdate date);

2、创建animals_nt:此类型将用作一个嵌套表的基础类型。

 CREATE TYPE animals_nt as table of animal_ty;

3、创建表breeder:饲养员的信息表

create table breeder
( breedername varchar2(25),
  animals animal_nt)
 nested table animals store as animals_nt_tab;

4、向嵌套表中插入记录
insert into breeder values
( 'mary',
  animal_nt(animal_ty('dog','butch','31-MAR-97'),
            animal_ty('dog','rover','31-MAR-97'),
            animal_ty('dog','julio','31-MAR-97'))
);

insert into breeder values
( 'jane',
  animal_nt(animal_ty('cat','an','31-MAR-97'),
            animal_ty('cat','jame','31-MAR-97'),
            animal_ty('cat','killer','31-MAR-97'))
);

commit;

5、查询嵌套表
select name,birthdate
  from table(select animals from breeder);

select name,birthdate
  from table(select animals from breeder where breedername=’mary’)
 where name=’dog’;


嵌套表的特点:
1、对象复用:如果编写面向对象的代码,就提高了重用以前编写的代码模块的机会。同样,如果创建面向对象的数据库对象,也就提高了数据库对象能够被重用的机会。

2、标准支持:如果创建标准的对象,那么它们被重用的机会就会提高。如果有多个应用或多个表使用同一数据库对象集合,那么它就是既成事实的数据库对象标准。

3、定义访问路径:对于每一个对象,用户可定义在其上运行的过程和函数,从而可以使数据和访问此数据的方法联合起来。有了用这种方式定义的访问路径,就可以标准化数据访问的方法并提高对象的可复用性。


嵌套表范例:
--------------------
CREATE OR REPLACE TYPE CourseList AS TABLE OF VARCHAR2(64);
/

desc courselist

SELECT type, text
FROM user_source
WHERE name = 'COURSELIST';

CREATE TABLE department (
name     VARCHAR2(20),
director VARCHAR2(20),
office   VARCHAR2(20),
courses  CourseList)
NESTED TABLE courses STORE AS courses_tab;

desc department

desc courses_tab

SELECT table_name, nested
FROM user_tables;

set linesize 121
col table_name format a20
col data_type format a30
col table_type_name format a15
col parent_table_column format a10

SELECT column_name, data_type, data_length
FROM user_tab_columns
WHERE table_name = 'DEPARTMENT';

SELECT table_name, table_type_owner, table_type_name,
parent_table_column
FROM user_nested_tables;

--------------------------------
嵌套表插入值范例

INSERT INTO department
(name, director, office, courses)
VALUES
('English', 'Lynn Saunders', 'Breakstone Hall 205', CourseList(
'Expository Writing',
'Film and Literature',
'Modern Science Fiction',
'Discursive Writing',
'Modern English Grammar',
'Introduction to Shakespeare',
'Modern Drama',
'The Short Story',
'The American Novel'));

SELECT * FROM department;
--------------------------------
更新嵌套表范例

DECLARE
   new_courses CourseList :=
   CourseList('Expository Writing',
   'Film and Literature',
   'Discursive Writing',
   'Modern English Grammar',
   'Realism and Naturalism',
   'Introduction to Shakespeare',
   'Modern Drama',
   'The Short Story',
   'The American Novel',
   '20th-Century Poetry',
   'Advanced Workshop in Poetry');
BEGIN
   UPDATE department
   SET courses = new_courses
   WHERE name = 'English';
END;
/

SELECT * FROM department;
-----------------------------------
DROP嵌套表范例

SELECT table_name
FROM user_tables;

DROP TABLE courses_tab;

You cannot directly drop the storage table of a nested table. Instead, you must drop the nested table column using the ALTER TABLE ... DROP COLUMN clause.

desc department

ALTER TABLE department
DROP COLUMN courses;
------------------------------- 

原创粉丝点击