nested table

 

General Information
Note: A nested table is a table stored within the structure of another table.
Data Dictionary Objects
collection$ tab$ type$
dba_nested_tablesall_nested_tablesuser_nested_tables
dba_nested_table_colsall_nested_table_colsuser_nested_table_cols
dba_sourceall_sourceuser_source
dba_tables all_tables user_tables
dba_tab_cols all_tab_cols user_tab_cols
dba_types all_typesuser_types
System Privileges
CREATE ANY TABLECREATE TABLEDROP ANY TABLE
CREATE ANY TYPECREATE TYPEDROP ANY TYPE
 

Nested Table Example

CREATE OR REPLACE TYPE CourseList AS TABLE OF VARCHAR2(64);
/

desc courselist

col text format a50

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;

--courses_tab是在数据库表user_nested_tables内标识department表中该索引表的名。

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_cols
WHERE table_name = 'DEPARTMENT';



SELECT table_name, table_type_owner, table_type_name,
parent_table_column
FROM user_nested_tables;

 

table_nametable_type_ownertable_type_nameparent_table_column
COURSES_TABAPPSCOURSELISTCOURSES

 

 


Insert into Nested Table
SELECT cardinality(courses)
FROM department;

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;

SELECT cardinality(courses)
FROM department;

Update a nested table
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;

SELECT cardinality(courses)
FROM department;

Drop nested table
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;
 
Table Unnesting

Collection Unnesting Demo
To select data from a nested table column you use the TABLE function to treat the nested table as columns of a table. This process is called "collection unnesting".

SELECT t1.department_id, t2.*
FROM hr_info t1, TABLE(t1.people) t2
WHERE t2.department_id = t1.department_id;

SELECT t1.name, t2.*
FROM department t1, TABLE(t1.courses) t2;
 
Nested Table Demos

Constructor for a Nested Table
In the following example, you pass multiple elements to the constructor CourseList(), which returns a nested table  containing those elements:

DECLARE

TYPE CourseList IS TABLE OF VARCHAR2(16);
my_courses CourseList;

BEGIN
my_courses := CourseList('Econ 2010','Acct 3401','Mgmt 3100');
END;
/

Because a PL/SQL table does not have a declared maximum size, you can put as many elements in the constructor as necessary.

DECLARE

TYPE CourseList IS TABLE OF VARCHAR2(16);
my_courses CourseList;

BEGIN
  my_courses := CourseList('Econ 2010','Acct 3401','Mgmt 3100');
  my_courses := CourseList('Math 2022','Acct 3431','Mgmt 3100');
  my_courses := CourseList('Phys 2299','Chem 9876');
  my_courses := CourseList('Food 9999');
  my_courses := CourseList('Orcl 3456','Math 3434','Hist 1040');
END;
/
Defining a Type as a database objectCREATE OR REPLACE TYPE CourseList AS TABLE OF VARCHAR2(64);
/
Defining Types in a package headerCREATE OR REPLACE PACKAGE xyz IS
 TYPE CourseList IS TABLE OF VARCHAR2(64);

 TYPE PartNum IS TABLE OF parent.part_num%TYPE
 INDEX BY BINARY_INTEGER;
END xyz;
/

 

 

   

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值