/** 
 * 本脚本用于创建表和插入相关数据，不能重复执行
 * 
 **/

-- 创建emp表
prompt 
prompt 创建emp表
declare
    num number;
begin 
    select count(1) into num from all_tables where table_name = 'EMP' and owner = 'EODA';
    if num = 1 then  
        execute immediate 'drop table eoda.emp';
    end if;
    execute immediate '
        create table eoda.emp 
        as
        select 
            object_id      empno,
            object_name    ename,
            created        hiredate
        from 
            all_objects
    ';
end;
/

prompt 
prompt emp 表添加索引
declare 
    num number;
begin 
    select count(1) into num from all_tables where table_name = 'EMP' and owner = 'EODA';
    if num = 1 then 
        execute immediate 'alter table emp add constraint emp_pk primary key (empno)';
    end if;
end;
/

prompt 
prompt 统计信息
begin 
    dbms_stats.gather_table_stats (user,'EMP',cascade=>true);
end;
/

-- 创建表heap_addresses
prompt 
prompt 创建表heap_addresses
declare 
    num number;
begin 
    select count(1) into num from all_tables where table_name = 'HEAP_ADDRESSES' and owner = 'EODA';
    if num = 1 then 
        execute immediate 'drop table eoda.heap_addresses';
    end if;
    execute immediate '
        create table heap_addresses 
        (
            empno      references  emp(empno) on delete cascade,
            addr_type  varchar2(10),
            street     varchar2(20),
            city       varchar2(20),
            state      varchar2(2),
            zip        number,
            primary key (empno,addr_type)
        )
    ';
end;
/

-- 创建表iot_addresses
prompt 
prompt 创建表iot_addresses
declare 
    num number;
begin 
    select count(1) into num from all_tables where table_name = 'IOT_ADDRESSES' and owner = 'EODA';
    if num = 1 then 
        execute immediate 'drop table eoda.iot_addresses';
    end if;
    execute immediate '
        create table iot_addresses
        (
            empno        references   emp(empno) on delete cascade,
            addr_type    varchar2(10),
            street       varchar2(20),
            city         varchar2(20),
            state        varchar2(2),
            zip          number,
            primary key (empno,addr_type)
        ) ORGANIZATION INDEX
    ';
end;
/

-- 向表中插入数据
prompt 
prompt 向表中插入数据
declare
    num_iot    number;
    num_heap   number;
begin 
    select count(1) into num_iot from all_tables where table_name = 'IOT_ADDRESSES' and owner = 'EODA';
    select count(1) into num_heap from all_tables where table_name = 'HEAP_ADDRESSES' and owner = 'EODA';
    if num_heap = 1 and num_iot = 1 then 
        insert into heap_addresses select empno,'WORK','123 main street','Washington','DC',20123 from scott.emp;
        insert into iot_addresses select empno,'WORK','123 main street','Washington','DC',20123 from scott.emp;
        commit;
    end if;
end;
/

-- 收集统计信息
prompt 
prompt 收集统计信息
declare
    num_iot    number;
    num_heap   number;
begin 
    select count(1) into num_iot from all_tables where table_name = 'IOT_ADDRESSES' and owner = 'EODA';
    select count(1) into num_heap from all_tables where table_name = 'HEAP_ADDRESSES' and owner = 'EODA';
    if num_heap = 1 and num_iot = 1 then 
        dbms_stats.gather_table_stats( user,'HEAP_ADDRESSES' );
        dbms_stats.gather_table_stats( user,'IOT_ADDRESSES' );
    end if;
end;
/
