-- 创建tablespace p1和p2，

CREATE TABLESPACE p1 DATAFILE '/opt/oracle/oradata/orcl/p1.dbf' SIZE 1m autoextend ON NEXT 1m;


CREATE TABLESPACE p2 DATAFILE '/opt/oracle/oradata/orcl/p2.dbf' SIZE 1m autoextend ON NEXT 1m;

-- oracle11g 执行下面两句,存在差异的主要原因是在docker中安装时，oracle路径等造成的
-- create tablespace p1 datafile '/u01/app/oracle/oradata/XE/p1.dbf'  size 1m autoextend on next 1m;
-- create tablespace p2 datafile '/u01/app/oracle/oradata/XE/p2.dbf' size 1m autoextend on next 1m;
-- 创建表emp，并根据字段emp将数据分散到两个表空间中
-- 分区是oracle收费部分，标准版没有，所以下面语句在标准版中会报错

CREATE TABLE emp (empno int, ename varchar2(20)) PARTITION BY hash(empno) (PARTITION part_1 TABLESPACE p1,
                                                                                                       PARTITION part_2 TABLESPACE p2);

-- select * from scott.emp;

INSERT INTO emp
SELECT empno,
       ename
FROM scott.emp;


SELECT *
FROM emp partition(part_1);


SELECT *
FROM emp partition(part_2);

-- 创建tablespace big1、big2  注意：由于下面sql语句的size比较大，创建起来比较费时间

CREATE TABLESPACE big1 DATAFILE '/opt/oracle/oradata/orcl/big1.dbf' SIZE 1200m;


CREATE TABLESPACE big2 DATAFILE '/opt/oracle/oradata/orcl/big2.dbf' SIZE 1200m;


CREATE OR REPLACE PROCEDURE hash_proc (p_nhash IN number, p_cursor OUT sys_refcursor) authid CURRENT_USER AS l_next long;

ROW_COUNT number;

l_template long := 'select $PRS$ oc,''p$PROS$'' pname,count(*) cnt' || ' from t partition ($PNAME$) union all ';

table_or_view_not_exits
EXCEPTION;

pragma exception_init(table_or_view_not_exits,-942);

BEGIN BEGIN
SELECT count(*) INTO ROW_COUNT
FROM user_tables
WHERE TABLE_NAME = upper('t');

IF ROW_COUNT = 1 THEN EXECUTE IMMEDIATE 'drop table t';

END IF;


EXCEPTION WHEN table_or_view_not_exits THEN NULL;

END;

EXECUTE IMMEDIATE '
create table t (id)
partition by hash(id)
partitions ' || p_nhash || '
as
select rownum
from all_objects
';


FOR x IN
  (SELECT partition_name pname,
          partition_position pos
   FROM user_tab_partitions
   WHERE TABLE_NAME = 'T'
   ORDER BY partition_position) LOOP l_next := l_next ||
REPLACE (replace(l_template,'$POS$',x.pos),
         '$PNAME$',
         x.pname);

END LOOP;

OPEN p_cursor
FOR 'select pname,cnt,
	substr( rpad(''*'',30*round( cnt/max(cnt)over(),2 ),''*''),1,30 ) hg
	from ('|| substr(l_next,1,length(l_next) - 11) ||')
	order by oc';

END;

-- 引用分区
-- 出错：THEN 误写成then

CREATE TABLE orders (order# number PRIMARY KEY,
                                           order_date date, DATA varchar2(30)) ENABLE ROW movement PARTITION BY range (order_date) (PARTITION part_2014
                                                                                                                                    VALUES LESS THAN(to_date('01-01-2015','dd-mm-yyyy')),
                                                                                                                                                PARTITION part_2015
                                                                                                                                    VALUES LESS THAN(to_date('01-01-2016','dd-mm-yyyy')));


INSERT INTO orders
VALUES (1,
        to_date('01-jun-2014','dd-mm-yyyy'),
        'xxx');


INSERT INTO orders
VALUES (2,
        to_date('01-jun-2015','dd-mm-yyyy'),
        'xxx');

-- 虚拟列分区

CREATE TABLE res(reservation_code varchar2(30));


INSERT INTO res(reservation_code)
SELECT chr(64+round(dbms_random.value(1,4))) || LEVEL
FROM dual CONNECT BY LEVEL <= 100000;


SELECT *
FROM res;


DROP TABLE res;


CREATE TABLE res (reservation_code varchar2(30),
                                   region AS (decode(substr(reservation_code,1,1), 'A','NE','C','NE','B','SW','D','NW'))) PARTITION BY list(region) (PARTITION p1
                                                                                                                                                     VALUES('NE'), PARTITION p2
                                                                                                                                                     VALUES('SW'), PARTITION p3
                                                                                                                                                     VALUES('NW'));

-- 行移动

CREATE TABLE range_example ( range_key_column date, DATA varchar2(20)) PARTITION BY range (range_key_column) ( PARTITION part_1
                                                                                                              VALUES LESS THAN (to_date('01/01/2014','dd/mm/yyyy')), PARTITION part_2
                                                                                                              VALUES LESS THAN (to_date('01/01/2015','dd/mm/yyyy')));


insert into range_example (range_key_column,data) values (to_date('15-dec-2013 00:00:00','dd-mon-yyyy hh24:mi:ss'),'application data...');
insert into range_example (range_key_column,data) values (to_date('01-jan-2013 00:00:00','dd-mon-yyyy hh24:mi:ss')-1/24/60/60,'application data...');
select * from range_example partition(part_1);
update range_example set range_key_column = trunc(range_key_column) where range_key_column = to_date('31-dec-2013 23:59:59','dd-mon-yyyy hh24:mi:ss');
