
/*
 *  
 *  
 * 
 */

-- begin 用关系模式实现嵌套表的模型
prompt
prompt 创建dept表
declare
    num       number;
    num_emp   number;
begin 
    select count(1) into num from all_tables where table_name = 'DEPT_NEST' and owner = 'EODA';
    select count(1) into num_emp from all_tables where table_name = 'EMP_NEST' and owner = 'EODA';
    if num_emp = 1 then 
        execute immediate 'drop table eoda.emp_nest';
    end if;
    if num = 1 then 
        execute immediate 'drop table eoda.dept_nest';
    end if;
    execute immediate '
        create table eoda.dept_nest
        (
            deptno    number(2)  primary key,
            dname     varchar2(14),
            loc       varchar2(13)
        )
    ';
end;
/

prompt
prompt 创建表emp_nest
declare 
    num number;
begin 
    select count(1) into num from all_tables where table_name = 'EMP_NEST' and owner = 'EODA';
    if num = 1 then 
        execute immediate 'drop table eoda.emp_nest';
    end if;
    execute immediate '
        create table eoda.emp_nest
        (
            empno    number(4)  primary key,
            ename    varchar2(10),
            job      varchar2(9),
            mgr      number(4)  references emp_nest,
            hiredate date,
            sal      number(7,2),
            comm     number(7,2),
            deptno   number(2) references dept_nest
        )
    ';
end;
/
-- end   用关系模式实现嵌套表的模型

prompt
prompt 创建type eoda.emp_type
declare
    num number;
begin 
    select count(1) into num from all_types where type_name = 'EMP_TYPE' and owner = 'EODA';
    if num = 1 then 
        execute immediate 'drop type eoda.emp_type force';
    end if;
    execute immediate '
        create or replace type eoda.emp_type
        as object
        (
            empno    number(4),
            ename    varchar2(10),
            job      varchar2(9),
            mgr      number(4),
            hiredate date,
            sal      number(7,2),
            comm     number(7,2)
        )
    ';
end;
/

prompt
prompt 创建type eoda.emp_tab_type
create or replace type emp_tab_type
as table of emp_type
/

prompt
prompt 创建表eoda.dept_and_emp
declare 
    num number;
begin 
    select count(1) into num from all_tables where table_name = 'DEPT_AND_EMP' and owner = 'EODA';
    if num = 1 then 
        execute immediate 'drop table eoda.dept_and_emp';
    end if;
    -- 在创建eoda.dept_and_emp表的时候也会创建表eoda.emps_nt
    execute immediate '
        create table eoda.dept_and_emp
        (
            deptno    number(2) primary key,
            dname     varchar2(14),
            loc       varchar2(13),
            emps      emp_tab_type 
        )
        nested table emps store as emps_nt
    ';
end;
/

prompt
prompt 添加唯一索引
declare 
    num number;
begin 
    select count(1) into num from all_tables where table_name = 'DEPT_AND_EMP' and owner = 'EODA';
    if num = 1 then 
        execute immediate 'alter table emps_nt add constraint emps_empno_unique unique(empno)';
    end if;
end;
/

prompt
prompt 插入数据
begin 
    execute immediate 'truncate table eoda.dept_and_emp';
    insert into eoda.dept_and_emp 
    select dept.*, 
           cast( multiset( select emp.empno,emp.ename,emp.job,emp.mgr,emp.hiredate,emp.sal,emp.comm 
                             from scott.emp emp 
                            where emp.deptno = dept.deptno ) as emp_tab_type )
     from scott.dept dept; 
end;
/


select deptno,dname,loc,d.emps as employees
 from eoda.dept_and_emp d 
where d.deptno = 10; 

select d.deptno,d.dname,emp.* 
from eoda.dept_and_emp d, table(d.emps) emp;

prompt 
prompt 跟新表数据
begin 
    update table( select emps from eoda.dept_and_emp where deptno = 10 ) set comm = 100;
end;
/

select d.deptno,d.dname,emp.* 
  from eoda.dept_and_emp d, table(d.emps) emp;

-- 由于数据较多，所以少查几条
select /*+NESTED_TABLE_GET_REFS+*/ nested_table_id,sys_nc_rowinfo$ from eoda.emps_nt where rownum < 5;

select name 
  from sys.col$ 
 where obj# = ( 
    select object_id 
      from dba_objects 
     where object_name = 'DEPT_AND_EMP' and 
                owner = 'EODA' );


select SYS_NC0000400005$
  from eoda.dept_and_emp;

prompt
prompt DBMS_METADATA.GET_DDL转储信息
begin 
    dbms_metadata.set_transform_param( DBMS_METADATA.SESSION_TRANSFORM,'STORAGE',false );
end;
/

-- 第一次运行会比较慢，慢的要死
select dbms_metadata.get_ddl( 'TABLE','DEPT_AND_EMP' ) from dual;
