分區表創建

創建分區表涉及的表空間

create tablespace PDB1_1901 datafile
'/u01/app/oracle/oradata/MPMON/MPMON_PDB1/PDB1_1901_1.dbf' size 16M AUTOEXTEND ON NEXT 8M MAXSIZE UNLIMITED,
'/u01/app/oracle/oradata/MPMON/MPMON_PDB1/PDB1_1901_2.dbf' size 16M AUTOEXTEND ON NEXT 8M MAXSIZE UNLIMITED
default storage (initial 128K next 2M pctincrease 0);

create tablespace PDB1_1902 datafile
'/u01/app/oracle/oradata/MPMON/MPMON_PDB1/PDB1_1902_1.dbf' size 16M AUTOEXTEND ON NEXT 8M MAXSIZE UNLIMITED,
'/u01/app/oracle/oradata/MPMON/MPMON_PDB1/PDB1_1902_2.dbf' size 16M AUTOEXTEND ON NEXT 8M MAXSIZE UNLIMITED
default storage (initial 128K next 2M pctincrease 0);

create tablespace PDB1_1903 datafile
'/u01/app/oracle/oradata/MPMON/MPMON_PDB1/PDB1_1903_1.dbf' size 16M AUTOEXTEND ON NEXT 8M MAXSIZE UNLIMITED,
'/u01/app/oracle/oradata/MPMON/MPMON_PDB1/PDB1_1903_2.dbf' size 16M AUTOEXTEND ON NEXT 8M MAXSIZE UNLIMITED
default storage (initial 128K next 2M pctincrease 0);


create tablespace I_PDB1_1901 datafile
'/u01/app/oracle/oradata/MPMON/MPMON_PDB1/I_PDB1_1901_1.dbf' size 16M AUTOEXTEND ON NEXT 8M MAXSIZE UNLIMITED,
'/u01/app/oracle/oradata/MPMON/MPMON_PDB1/I_PDB1_1901_2.dbf' size 16M AUTOEXTEND ON NEXT 8M MAXSIZE UNLIMITED
default storage (initial 128K next 2M pctincrease 0);

create tablespace I_PDB1_1902 datafile
'/u01/app/oracle/oradata/MPMON/MPMON_PDB1/I_PDB1_1902_1.dbf' size 16M AUTOEXTEND ON NEXT 8M MAXSIZE UNLIMITED,
'/u01/app/oracle/oradata/MPMON/MPMON_PDB1/I_PDB1_1902_2.dbf' size 16M AUTOEXTEND ON NEXT 8M MAXSIZE UNLIMITED
default storage (initial 128K next 2M pctincrease 0);

create tablespace I_PDB1_1903 datafile
'/u01/app/oracle/oradata/MPMON/MPMON_PDB1/I_PDB1_1903_1.dbf' size 16M AUTOEXTEND ON NEXT 8M MAXSIZE UNLIMITED,
'/u01/app/oracle/oradata/MPMON/MPMON_PDB1/I_PDB1_1903_2.dbf' size 16M AUTOEXTEND ON NEXT 8M MAXSIZE UNLIMITED
default storage (initial 128K next 2M pctincrease 0);
----------------------------------------------------------------------------------------------------

create tablespace PDB1_1901_A datafile
'/u01/app/oracle/oradata/MPMON/MPMON_PDB1/PDB1_1901_A1.dbf' size 16M AUTOEXTEND ON NEXT 8M MAXSIZE UNLIMITED,
'/u01/app/oracle/oradata/MPMON/MPMON_PDB1/PDB1_1901_A2.dbf' size 16M AUTOEXTEND ON NEXT 8M MAXSIZE UNLIMITED
default storage (initial 128K next 2M pctincrease 0);

create tablespace PDB1_1901_B datafile
'/u01/app/oracle/oradata/MPMON/MPMON_PDB1/PDB1_1901_B1.dbf' size 16M AUTOEXTEND ON NEXT 8M MAXSIZE UNLIMITED,
'/u01/app/oracle/oradata/MPMON/MPMON_PDB1/PDB1_1901_B2.dbf' size 16M AUTOEXTEND ON NEXT 8M MAXSIZE UNLIMITED
default storage (initial 128K next 2M pctincrease 0);

create tablespace PDB1_1901_C datafile
'/u01/app/oracle/oradata/MPMON/MPMON_PDB1/PDB1_1901_C1.dbf' size 16M AUTOEXTEND ON NEXT 8M MAXSIZE UNLIMITED,
'/u01/app/oracle/oradata/MPMON/MPMON_PDB1/PDB1_1901_C2.dbf' size 16M AUTOEXTEND ON NEXT 8M MAXSIZE UNLIMITED
default storage (initial 128K next 2M pctincrease 0);

---------------

create tablespace PDB1_1902_A datafile
'/u01/app/oracle/oradata/MPMON/MPMON_PDB1/PDB1_1902_A1.dbf' size 16M AUTOEXTEND ON NEXT 8M MAXSIZE UNLIMITED,
'/u01/app/oracle/oradata/MPMON/MPMON_PDB1/PDB1_1902_A2.dbf' size 16M AUTOEXTEND ON NEXT 8M MAXSIZE UNLIMITED
default storage (initial 128K next 2M pctincrease 0);

create tablespace PDB1_1902_B datafile
'/u01/app/oracle/oradata/MPMON/MPMON_PDB1/PDB1_1902_B1.dbf' size 16M AUTOEXTEND ON NEXT 8M MAXSIZE UNLIMITED,
'/u01/app/oracle/oradata/MPMON/MPMON_PDB1/PDB1_1902_B2.dbf' size 16M AUTOEXTEND ON NEXT 8M MAXSIZE UNLIMITED
default storage (initial 128K next 2M pctincrease 0);

create tablespace PDB1_1902_C datafile
'/u01/app/oracle/oradata/MPMON/MPMON_PDB1/PDB1_1902_C1.dbf' size 16M AUTOEXTEND ON NEXT 8M MAXSIZE UNLIMITED,
'/u01/app/oracle/oradata/MPMON/MPMON_PDB1/PDB1_1902_C2.dbf' size 16M AUTOEXTEND ON NEXT 8M MAXSIZE UNLIMITED
default storage (initial 128K next 2M pctincrease 0);

---------------
create tablespace PDB1_1903_A datafile
'/u01/app/oracle/oradata/MPMON/MPMON_PDB1/PDB1_1903_A01.dbf' size 16M AUTOEXTEND ON NEXT 8M MAXSIZE UNLIMITED,
'/u01/app/oracle/oradata/MPMON/MPMON_PDB1/PDB1_1903_A02.dbf' size 16M AUTOEXTEND ON NEXT 8M MAXSIZE UNLIMITED
default storage (initial 128K next 2M pctincrease 0);

create tablespace PDB1_1903_B datafile
'/u01/app/oracle/oradata/MPMON/MPMON_PDB1/PDB1_1903_B01.dbf' size 16M AUTOEXTEND ON NEXT 8M MAXSIZE UNLIMITED,
'/u01/app/oracle/oradata/MPMON/MPMON_PDB1/PDB1_1903_B02.dbf' size 16M AUTOEXTEND ON NEXT 8M MAXSIZE UNLIMITED
default storage (initial 128K next 2M pctincrease 0);

create tablespace PDB1_1903_C datafile
'/u01/app/oracle/oradata/MPMON/MPMON_PDB1/PDB1_1903_C01.dbf' size 16M AUTOEXTEND ON NEXT 8M MAXSIZE UNLIMITED,
'/u01/app/oracle/oradata/MPMON/MPMON_PDB1/PDB1_1903_C02.dbf' size 16M AUTOEXTEND ON NEXT 8M MAXSIZE UNLIMITED
default storage (initial 128K next 2M pctincrease 0);

---------------
--==================
create tablespace I_PDB1_1901_A datafile
'/u01/app/oracle/oradata/MPMON/MPMON_PDB1/I_PDB1_1901_A1.dbf' size 16M AUTOEXTEND ON NEXT 8M MAXSIZE UNLIMITED,
'/u01/app/oracle/oradata/MPMON/MPMON_PDB1/I_PDB1_1901_A2.dbf' size 16M AUTOEXTEND ON NEXT 8M MAXSIZE UNLIMITED
default storage (initial 128K next 2M pctincrease 0);

create tablespace I_PDB1_1901_B datafile
'/u01/app/oracle/oradata/MPMON/MPMON_PDB1/I_PDB1_1901_B1.dbf' size 16M AUTOEXTEND ON NEXT 8M MAXSIZE UNLIMITED,
'/u01/app/oracle/oradata/MPMON/MPMON_PDB1/I_PDB1_1901_B2.dbf' size 16M AUTOEXTEND ON NEXT 8M MAXSIZE UNLIMITED
default storage (initial 128K next 2M pctincrease 0);

create tablespace I_PDB1_1901_C datafile
'/u01/app/oracle/oradata/MPMON/MPMON_PDB1/I_PDB1_1901_C1.dbf' size 16M AUTOEXTEND ON NEXT 8M MAXSIZE UNLIMITED,
'/u01/app/oracle/oradata/MPMON/MPMON_PDB1/I_PDB1_1901_C2.dbf' size 16M AUTOEXTEND ON NEXT 8M MAXSIZE UNLIMITED
default storage (initial 128K next 2M pctincrease 0);

---------------

create tablespace I_PDB1_1902_A datafile
'/u01/app/oracle/oradata/MPMON/MPMON_PDB1/I_PDB1_1902_A1.dbf' size 16M AUTOEXTEND ON NEXT 8M MAXSIZE UNLIMITED,
'/u01/app/oracle/oradata/MPMON/MPMON_PDB1/I_PDB1_1902_A2.dbf' size 16M AUTOEXTEND ON NEXT 8M MAXSIZE UNLIMITED
default storage (initial 128K next 2M pctincrease 0);

create tablespace I_PDB1_1902_B datafile
'/u01/app/oracle/oradata/MPMON/MPMON_PDB1/I_PDB1_1902_B1.dbf' size 16M AUTOEXTEND ON NEXT 8M MAXSIZE UNLIMITED,
'/u01/app/oracle/oradata/MPMON/MPMON_PDB1/I_PDB1_1902_B2.dbf' size 16M AUTOEXTEND ON NEXT 8M MAXSIZE UNLIMITED
default storage (initial 128K next 2M pctincrease 0);

create tablespace I_PDB1_1902_C datafile
'/u01/app/oracle/oradata/MPMON/MPMON_PDB1/I_PDB1_1902_C1.dbf' size 16M AUTOEXTEND ON NEXT 8M MAXSIZE UNLIMITED,
'/u01/app/oracle/oradata/MPMON/MPMON_PDB1/I_PDB1_1902_C2.dbf' size 16M AUTOEXTEND ON NEXT 8M MAXSIZE UNLIMITED
default storage (initial 128K next 2M pctincrease 0);

---------------
create tablespace I_PDB1_1903_A datafile
'/u01/app/oracle/oradata/MPMON/MPMON_PDB1/I_PDB1_1903_A01.dbf' size 16M AUTOEXTEND ON NEXT 8M MAXSIZE UNLIMITED,
'/u01/app/oracle/oradata/MPMON/MPMON_PDB1/I_PDB1_1903_A02.dbf' size 16M AUTOEXTEND ON NEXT 8M MAXSIZE UNLIMITED
default storage (initial 128K next 2M pctincrease 0);

create tablespace I_PDB1_1903_B datafile
'/u01/app/oracle/oradata/MPMON/MPMON_PDB1/I_PDB1_1903_B01.dbf' size 16M AUTOEXTEND ON NEXT 8M MAXSIZE UNLIMITED,
'/u01/app/oracle/oradata/MPMON/MPMON_PDB1/I_PDB1_1903_B02.dbf' size 16M AUTOEXTEND ON NEXT 8M MAXSIZE UNLIMITED
default storage (initial 128K next 2M pctincrease 0);

create tablespace I_PDB1_1903_C datafile
'/u01/app/oracle/oradata/MPMON/MPMON_PDB1/I_PDB1_1903_C01.dbf' size 16M AUTOEXTEND ON NEXT 8M MAXSIZE UNLIMITED,
'/u01/app/oracle/oradata/MPMON/MPMON_PDB1/I_PDB1_1903_C02.dbf' size 16M AUTOEXTEND ON NEXT 8M MAXSIZE UNLIMITED
default storage (initial 128K next 2M pctincrease 0);

非分區表

CREATE TABLE test_tab
(
    range_tab_seq  number not null,
    range_tab_no   varchar2(20),
    range_tab_name varchar2(20),
    range_tab_type varchar2(1),    -- A,B,C 
    range_tab_date date            --範圍分區列
) tablespace UD;

alter table test_tab
add constraint pk_test_tab primary key (range_tab_seq)
using index tablespace I_UD;

create index inx_test_tab_1 on test_tab(range_tab_no)
tablespace I_UD;

範圍分區表

CREATE TABLE test_range_tab
(
    range_tab_seq  number not null,
    range_tab_no   varchar2(20),
    range_tab_name varchar2(20),
    range_tab_type varchar2(1),    -- A,B,C 
    range_tab_date date            --範圍分區列
)partition by range (range_tab_date)
(
  partition P1901 values less than (TO_DATE('2019-02-01', 'YYYY-MM-DD')) tablespace PDB1_1901,
  partition P1902 values less than (TO_DATE('2019-03-01', 'YYYY-MM-DD')) tablespace PDB1_1902,  
  partition P1903 values less than (TO_DATE('2019-04-01', 'YYYY-MM-DD')) tablespace PDB1_1903
);

alter table test_range_tab
add constraint pk_test_range_tab primary key (range_tab_date,range_tab_seq)
using index local
(
 PARTITION P1901 TABLESPACE I_PDB1_1901,
 PARTITION P1902 TABLESPACE I_PDB1_1902,
 PARTITION P1903 TABLESPACE I_PDB1_1903
);

create index ind_test_range_tab_1 on test_range_tab(range_tab_date,range_tab_no)
local 
(
 PARTITION P1901 TABLESPACE I_PDB1_1901,
 PARTITION P1902 TABLESPACE I_PDB1_1902,
 PARTITION P1903 TABLESPACE I_PDB1_1903
);  
----------------------------------------------------------------------------------------------------------------------
--- or
drop TABLE test_range_tab;

CREATE TABLE test_range_tab
(
    range_tab_seq  number not null,
    range_tab_no   varchar2(20),
    range_tab_name varchar2(20),
    range_tab_type varchar2(1),    -- A,B,C 
    range_tab_date date            --範圍分區列
)partition by range (range_tab_date)
(
  partition P1901 values less than (TO_DATE('2019-02-01', 'YYYY-MM-DD')) tablespace PDB1_1901,
  partition P1902 values less than (TO_DATE('2019-03-01', 'YYYY-MM-DD')) tablespace PDB1_1902
);

alter table test_range_tab
add constraint pk_test_range_tab primary key (range_tab_date,range_tab_seq)
using index local
(
 PARTITION P1901 TABLESPACE I_PDB1_1901,
 PARTITION P1902 TABLESPACE I_PDB1_1902
);

create index ind_test_range_tab_1 on test_range_tab(range_tab_date,range_tab_no)
local 
(
 PARTITION P1901 TABLESPACE I_PDB1_1901,
 PARTITION P1902 TABLESPACE I_PDB1_1902
);  
----------------------------------------------------------------------------------------------------------------------
--添加分區
alter table test_range_tab add PARTITION P1903 VALUES LESS THAN (TO_DATE('2019-04-01', 'YYYY-MM-DD')) TABLESPACE PDB1_1903;

alter index pk_test_range_tab rebuild partition P1903  TABLESPACE I_PDB1_1903;

alter index ind_test_range_tab_1 rebuild partition P1903  TABLESPACE I_PDB1_1903;

-----------------------------------------------------------------------------------------------------------------------

SELECT TABLE_NAME, A.INDEX_NAME, A.TABLESPACE_NAME,a.partition_name ,a.partition_position
FROM USER_IND_PARTITIONS A, USER_INDEXES B
WHERE A.INDEX_NAME = B.INDEX_NAME
and A.INDEX_NAME=upper('ind_test_range_tab_1')
ORDER BY 4;

select b.table_name,b.partition_name,b.tablespace_name,b.partition_position from USER_TAB_PARTITIONS b
where b.table_name=upper('test_range_tab')
ORDER BY 4;

範圍列表分區表

------------------------------------------------------------------------------------------------
CREATE TABLE test_range_list_tab
(
    range_tab_seq  number not null,
    range_tab_no   varchar2(20),
    range_tab_name varchar2(20),
    range_tab_type varchar2(1),    -- A,B,C 
    range_tab_date date            --範圍分區列
)
partition by range(range_tab_date)
subpartition by list(range_tab_type)
( 
  partition P1901 values less than (TO_DATE('2019-02-01', 'YYYY-MM-DD')) tablespace PDB1_1901
  (
    SUBPARTITION P1901A VALUES ('A') TABLESPACE PDB1_1901_A, 
    SUBPARTITION P1901B VALUES ('B') TABLESPACE PDB1_1901_B,
    SUBPARTITION P1901C VALUES ('C') TABLESPACE PDB1_1901_C
  ),
  partition P1902 values less than (TO_DATE('2019-03-01', 'YYYY-MM-DD')) tablespace PDB1_1902
  (
    SUBPARTITION P1902A VALUES ('A') TABLESPACE PDB1_1902_A, 
    SUBPARTITION P1902B VALUES ('B') TABLESPACE PDB1_1902_B,
    SUBPARTITION P1902C VALUES ('C') TABLESPACE PDB1_1902_C
  ),  
  partition P1903 values less than (TO_DATE('2019-04-01', 'YYYY-MM-DD')) tablespace PDB1_1903
  (
    SUBPARTITION P1903A VALUES ('A') TABLESPACE PDB1_1903_A, 
    SUBPARTITION P1903B VALUES ('B') TABLESPACE PDB1_1903_B,
    SUBPARTITION P1903C VALUES ('C') TABLESPACE PDB1_1903_C        
  )
);

alter table test_range_list_tab
add constraint pk_test_range_list_tab primary key (range_tab_date,range_tab_type,range_tab_seq)
using index local
(
 PARTITION P1901 TABLESPACE I_PDB1_1901
 (
    SUBPARTITION P1901A TABLESPACE I_PDB1_1901_A, 
    SUBPARTITION P1901B TABLESPACE I_PDB1_1901_B,
    SUBPARTITION P1901C TABLESPACE I_PDB1_1901_C       
 ),
 PARTITION P1902 TABLESPACE I_PDB1_1902
 (
    SUBPARTITION P1902A TABLESPACE I_PDB1_1902_A, 
    SUBPARTITION P1902B TABLESPACE I_PDB1_1902_B,
    SUBPARTITION P1902C TABLESPACE I_PDB1_1902_C       
 ),
 PARTITION P1903 TABLESPACE I_PDB1_1903
 (
    SUBPARTITION P1903A TABLESPACE I_PDB1_1903_A, 
    SUBPARTITION P1903B TABLESPACE I_PDB1_1903_B,
    SUBPARTITION P1903C TABLESPACE I_PDB1_1903_C       
 )
);


create index ind_test_range_list_tab_1 on test_range_list_tab(range_tab_date,range_tab_type,range_tab_no)
local 
(
 PARTITION P1901 TABLESPACE I_PDB1_1901
 (
    SUBPARTITION P1901A TABLESPACE I_PDB1_1901_A, 
    SUBPARTITION P1901B TABLESPACE I_PDB1_1901_B,
    SUBPARTITION P1901C TABLESPACE I_PDB1_1901_C       
 ),
 PARTITION P1902 TABLESPACE I_PDB1_1902
 (
    SUBPARTITION P1902A TABLESPACE I_PDB1_1902_A, 
    SUBPARTITION P1902B TABLESPACE I_PDB1_1902_B,
    SUBPARTITION P1902C TABLESPACE I_PDB1_1902_C       
 ),
 PARTITION P1903 TABLESPACE I_PDB1_1903
 (
    SUBPARTITION P1903A TABLESPACE I_PDB1_1903_A, 
    SUBPARTITION P1903B TABLESPACE I_PDB1_1903_B,
    SUBPARTITION P1903C TABLESPACE I_PDB1_1903_C       
 )
);  
----------------------------------------------------------------------------------------------------------------------
--   or
drop table test_range_list_tab;

CREATE TABLE test_range_list_tab
(
    range_tab_seq  number not null,
    range_tab_no   varchar2(20),
    range_tab_name varchar2(20),
    range_tab_type varchar2(1),    -- A,B,C 
    range_tab_date date            --範圍分區列
)
partition by range(range_tab_date)
subpartition by list(range_tab_type)
( 
  partition P1901 values less than (TO_DATE('2019-02-01', 'YYYY-MM-DD')) tablespace PDB1_1901
  (
    SUBPARTITION P1901A VALUES ('A') TABLESPACE PDB1_1901_A, 
    SUBPARTITION P1901B VALUES ('B') TABLESPACE PDB1_1901_B
  ),
  partition P1902 values less than (TO_DATE('2019-03-01', 'YYYY-MM-DD')) tablespace PDB1_1902
  (
    SUBPARTITION P1902A VALUES ('A') TABLESPACE PDB1_1902_A, 
    SUBPARTITION P1902B VALUES ('B') TABLESPACE PDB1_1902_B
  )
);

alter table test_range_list_tab
add constraint pk_test_range_list_tab primary key (range_tab_date,range_tab_type,range_tab_seq)
using index local
(
 PARTITION P1901 TABLESPACE I_PDB1_1901
 (
    SUBPARTITION P1901A TABLESPACE I_PDB1_1901_A, 
    SUBPARTITION P1901B TABLESPACE I_PDB1_1901_B      
 ),
 PARTITION P1902 TABLESPACE I_PDB1_1902
 (
    SUBPARTITION P1902A TABLESPACE I_PDB1_1902_A, 
    SUBPARTITION P1902B TABLESPACE I_PDB1_1902_B     
 )
);

create index ind_test_range_list_tab_1 on test_range_list_tab(range_tab_date,range_tab_type,range_tab_no)
local 
(
 PARTITION P1901 TABLESPACE I_PDB1_1901
 (
    SUBPARTITION P1901A TABLESPACE I_PDB1_1901_A, 
    SUBPARTITION P1901B TABLESPACE I_PDB1_1901_B      
 ),
 PARTITION P1902 TABLESPACE I_PDB1_1902
 (
    SUBPARTITION P1902A TABLESPACE I_PDB1_1902_A, 
    SUBPARTITION P1902B TABLESPACE I_PDB1_1902_B     
 )
);  
------------------------------------------------------------------------------------------------------
--  添加 子分區
ALTER TABLE test_range_list_tab
   MODIFY PARTITION P1901 
      ADD  SUBPARTITION P1901C VALUES ('C') TABLESPACE PDB1_1901_C;
      
ALTER TABLE test_range_list_tab
   MODIFY PARTITION P1902 
      ADD  SUBPARTITION P1902C VALUES ('C') TABLESPACE PDB1_1902_C;
         
alter index pk_test_range_list_tab rebuild subPARTITION P1901C  TABLESPACE I_PDB1_1901_C;
alter index ind_test_range_list_tab_1 rebuild subPARTITION P1901C  TABLESPACE I_PDB1_1901_C;
      
alter index pk_test_range_list_tab rebuild subPARTITION P1902C  TABLESPACE I_PDB1_1902_C;
alter index ind_test_range_list_tab_1 rebuild subPARTITION P1902C  TABLESPACE I_PDB1_1902_C;

-------------------------------------------------------------------------------------------------------
-- 添加主分區
alter table test_range_list_tab add partition P1903 values less than (TO_DATE('2019-04-01', 'YYYY-MM-DD')) tablespace PDB1_1903
(
  SUBPARTITION P1903A VALUES ('A') TABLESPACE PDB1_1903_A, 
  SUBPARTITION P1903B VALUES ('B') TABLESPACE PDB1_1903_B,
  SUBPARTITION P1903C VALUES ('C') TABLESPACE PDB1_1903_C  
);

--alter index pk_test_range_list_tab rebuild PARTITION P1903  TABLESPACE I_PDB1_1903;
--alter index ind_test_range_list_tab_1 rebuild PARTITION P1903 TABLESPACE I_PDB1_1903;

alter index pk_test_range_list_tab rebuild subPARTITION P1903A  TABLESPACE I_PDB1_1903_A;
alter index ind_test_range_list_tab_1 rebuild subPARTITION P1903A  TABLESPACE I_PDB1_1903_A;

alter index pk_test_range_list_tab rebuild subPARTITION P1903B  TABLESPACE I_PDB1_1903_B;
alter index ind_test_range_list_tab_1 rebuild subPARTITION P1903B  TABLESPACE I_PDB1_1903_B;

alter index pk_test_range_list_tab rebuild subPARTITION P1903C  TABLESPACE I_PDB1_1903_C;
alter index ind_test_range_list_tab_1 rebuild subPARTITION P1903C  TABLESPACE I_PDB1_1903_C;

-------------------------------------------------------------------------------------------------------

SELECT TABLE_NAME, A.INDEX_NAME, A.TABLESPACE_NAME,a.partition_name ,a.partition_position
FROM USER_IND_PARTITIONS A, USER_INDEXES B
WHERE A.INDEX_NAME = B.INDEX_NAME
and A.INDEX_NAME=upper('ind_test_range_list_tab_1')
ORDER BY 4;

select b.table_name,b.partition_name,b.tablespace_name,b.partition_position from USER_TAB_PARTITIONS b
where b.table_name=upper('test_range_list_tab')
ORDER BY 4;

select c.table_name,c.partition_name,c.subpartition_name,c.partition_position,c.subpartition_position,c.tablespace_name 
from USER_TAB_SUBPARTITIONS c 
where c.table_name=upper('test_range_list_tab')
ORDER BY 4,5;

select d.index_name,d.partition_name,d.subpartition_name,d.tablespace_name
,d.partition_position,d.subpartition_position
from user_ind_subpartitions d where d.index_name=upper('ind_test_range_list_tab_1')
order by 5,6

範圍哈希分區表

CREATE TABLE test_range_hast_tab
(
    range_tab_seq  number not null,
    range_tab_no   varchar2(20),
    range_tab_name varchar2(20),
    range_tab_type varchar2(1),    --
    range_tab_date date            --範圍分區列
)
partition by range(range_tab_date)
subpartition by hash(range_tab_type)
( 
  partition P1901 values less than (TO_DATE('2019-02-01', 'YYYY-MM-DD')) tablespace PDB1_1901
  (
    SUBPARTITION P1901A TABLESPACE PDB1_1901_A, 
    SUBPARTITION P1901B TABLESPACE PDB1_1901_B,
    SUBPARTITION P1901C TABLESPACE PDB1_1901_C
  ),
  partition P1902 values less than (TO_DATE('2019-03-01', 'YYYY-MM-DD')) tablespace PDB1_1902
  (
    SUBPARTITION P1902A TABLESPACE PDB1_1902_A, 
    SUBPARTITION P1902B TABLESPACE PDB1_1902_B,
    SUBPARTITION P1902C TABLESPACE PDB1_1902_C
  ),  
  partition P1903 values less than (TO_DATE('2019-04-01', 'YYYY-MM-DD')) tablespace PDB1_1903
  (
    SUBPARTITION P1903A TABLESPACE PDB1_1903_A, 
    SUBPARTITION P1903B TABLESPACE PDB1_1903_B,
    SUBPARTITION P1903C TABLESPACE PDB1_1903_C        
  )
);

alter table test_range_hast_tab
add constraint pk_test_range_hast_tab primary key (range_tab_date,range_tab_type,range_tab_seq)
using index local
(
 PARTITION P1901 TABLESPACE I_PDB1_1901
 (
    SUBPARTITION P1901A TABLESPACE I_PDB1_1901_A, 
    SUBPARTITION P1901B TABLESPACE I_PDB1_1901_B,
    SUBPARTITION P1901C TABLESPACE I_PDB1_1901_C       
 ),
 PARTITION P1902 TABLESPACE I_PDB1_1902
 (
    SUBPARTITION P1902A TABLESPACE I_PDB1_1902_A, 
    SUBPARTITION P1902B TABLESPACE I_PDB1_1902_B,
    SUBPARTITION P1902C TABLESPACE I_PDB1_1902_C       
 ),
 PARTITION P1903 TABLESPACE I_PDB1_1903
 (
    SUBPARTITION P1903A TABLESPACE I_PDB1_1903_A, 
    SUBPARTITION P1903B TABLESPACE I_PDB1_1903_B,
    SUBPARTITION P1903C TABLESPACE I_PDB1_1903_C       
 )
);

create index ind_test_range_hast_tab_1 on test_range_hast_tab(range_tab_date,range_tab_type,range_tab_no)
local 
(
 PARTITION P1901 TABLESPACE I_PDB1_1901
 (
    SUBPARTITION P1901A TABLESPACE I_PDB1_1901_A, 
    SUBPARTITION P1901B TABLESPACE I_PDB1_1901_B,
    SUBPARTITION P1901C TABLESPACE I_PDB1_1901_C       
 ),
 PARTITION P1902 TABLESPACE I_PDB1_1902
 (
    SUBPARTITION P1902A TABLESPACE I_PDB1_1902_A, 
    SUBPARTITION P1902B TABLESPACE I_PDB1_1902_B,
    SUBPARTITION P1902C TABLESPACE I_PDB1_1902_C       
 ),
 PARTITION P1903 TABLESPACE I_PDB1_1903
 (
    SUBPARTITION P1903A TABLESPACE I_PDB1_1903_A, 
    SUBPARTITION P1903B TABLESPACE I_PDB1_1903_B,
    SUBPARTITION P1903C TABLESPACE I_PDB1_1903_C       
 )
);  
-----------------------------------------------------------------------------------------------------------------
--   or
drop table test_range_hast_tab;

CREATE TABLE test_range_hast_tab
(
    range_tab_seq  number not null,
    range_tab_no   varchar2(20),
    range_tab_name varchar2(20),
    range_tab_type varchar2(1),    --
    range_tab_date date            --範圍分區列
)
partition by range(range_tab_date)
subpartition by hash(range_tab_type)
( 
  partition P1901 values less than (TO_DATE('2019-02-01', 'YYYY-MM-DD')) tablespace PDB1_1901
  (
    SUBPARTITION P1901A TABLESPACE PDB1_1901_A, 
    SUBPARTITION P1901B TABLESPACE PDB1_1901_B,
    SUBPARTITION P1901C TABLESPACE PDB1_1901_C
  ),
  partition P1902 values less than (TO_DATE('2019-03-01', 'YYYY-MM-DD')) tablespace PDB1_1902
  (
    SUBPARTITION P1902A TABLESPACE PDB1_1902_A, 
    SUBPARTITION P1902B TABLESPACE PDB1_1902_B,
    SUBPARTITION P1902C TABLESPACE PDB1_1902_C
  )
);

alter table test_range_hast_tab
add constraint pk_test_range_hast_tab primary key (range_tab_date,range_tab_type,range_tab_seq)
using index local
(
 PARTITION P1901 TABLESPACE I_PDB1_1901
 (
    SUBPARTITION P1901A TABLESPACE I_PDB1_1901_A, 
    SUBPARTITION P1901B TABLESPACE I_PDB1_1901_B,
    SUBPARTITION P1901C TABLESPACE I_PDB1_1901_C       
 ),
 PARTITION P1902 TABLESPACE I_PDB1_1902
 (
    SUBPARTITION P1902A TABLESPACE I_PDB1_1902_A, 
    SUBPARTITION P1902B TABLESPACE I_PDB1_1902_B,
    SUBPARTITION P1902C TABLESPACE I_PDB1_1902_C       
 )
);

create index ind_test_range_hast_tab_1 on test_range_hast_tab(range_tab_date,range_tab_type,range_tab_no)
local 
(
 PARTITION P1901 TABLESPACE I_PDB1_1901
 (
    SUBPARTITION P1901A TABLESPACE I_PDB1_1901_A, 
    SUBPARTITION P1901B TABLESPACE I_PDB1_1901_B,
    SUBPARTITION P1901C TABLESPACE I_PDB1_1901_C       
 ),
 PARTITION P1902 TABLESPACE I_PDB1_1902
 (
    SUBPARTITION P1902A TABLESPACE I_PDB1_1902_A, 
    SUBPARTITION P1902B TABLESPACE I_PDB1_1902_B,
    SUBPARTITION P1902C TABLESPACE I_PDB1_1902_C       
 )
);  
-----------------------------------------------------------------------------------------------------------------
-- --删除分区
alter table test_range_hast_tab drop partition P1903;
-- 添加主分區
alter table test_range_hast_tab add partition P1903 values less than (TO_DATE('2019-04-01', 'YYYY-MM-DD')) tablespace PDB1_1903
(
  SUBPARTITION P1903A TABLESPACE PDB1_1903_A, 
  SUBPARTITION P1903B TABLESPACE PDB1_1903_B,
  SUBPARTITION P1903C TABLESPACE PDB1_1903_C   
);

alter index pk_test_range_hast_tab rebuild subPARTITION P1903A  TABLESPACE I_PDB1_1903_A;
alter index ind_test_range_hast_tab_1 rebuild subPARTITION P1903A  TABLESPACE I_PDB1_1903_A;

alter index pk_test_range_hast_tab rebuild subPARTITION P1903B  TABLESPACE I_PDB1_1903_B;
alter index ind_test_range_hast_tab_1 rebuild subPARTITION P1903B  TABLESPACE I_PDB1_1903_B;

alter index pk_test_range_hast_tab rebuild subPARTITION P1903C  TABLESPACE I_PDB1_1903_C;
alter index ind_test_range_hast_tab_1 rebuild subPARTITION P1903C  TABLESPACE I_PDB1_1903_C;
-------------------------------------------------------------------------------------------------------------------
SELECT TABLE_NAME, A.INDEX_NAME, A.TABLESPACE_NAME,a.partition_name ,a.partition_position
FROM USER_IND_PARTITIONS A, USER_INDEXES B
WHERE A.INDEX_NAME = B.INDEX_NAME
and A.INDEX_NAME=upper('ind_test_range_hast_tab_1')
ORDER BY 4;

select b.table_name,b.partition_name,b.tablespace_name,b.partition_position from USER_TAB_PARTITIONS b
where b.table_name=upper('test_range_hast_tab')
ORDER BY 4;

select c.table_name,c.partition_name,c.subpartition_name,c.partition_position,c.subpartition_position,c.tablespace_name 
from USER_TAB_SUBPARTITIONS c 
where c.table_name=upper('test_range_hast_tab')
ORDER BY 4,5;

select d.index_name,d.partition_name,d.subpartition_name,d.tablespace_name
,d.partition_position,d.subpartition_position
from user_ind_subpartitions d where d.index_name=upper('ind_test_range_hast_tab_1')
order by 5,6

哈希范围分区表

CREATE TABLE test_hash_range_tab
(
    range_tab_seq  number not null,
    range_tab_no   varchar2(20),
    range_tab_name varchar2(20),
    range_tab_type varchar2(1),    --hash分區列
    range_tab_date date            --範圍分區列
)
partition by hash(range_tab_type)
subpartition by range(range_tab_date)
( 
  partition PA tablespace PDB1_PA
  (
    SUBPARTITION P1901A values less than (TO_DATE('2019-02-01', 'YYYY-MM-DD')) TABLESPACE PDB1_1901_A, 
    SUBPARTITION P1902A values less than (TO_DATE('2019-03-01', 'YYYY-MM-DD')) TABLESPACE PDB1_1902_A
  ),
  partition PB tablespace PDB1_PB
  (
    SUBPARTITION P1901B values less than (TO_DATE('2019-02-01', 'YYYY-MM-DD')) TABLESPACE PDB1_1901_B, 
    SUBPARTITION P1902B values less than (TO_DATE('2019-03-01', 'YYYY-MM-DD')) TABLESPACE PDB1_1902_B
  )
);

alter table test_hash_range_tab
add constraint pk_test_hash_range_tab primary key (range_tab_type,range_tab_date,range_tab_seq)
using index local
(
 PARTITION PA TABLESPACE I_PDB1_PA
 (
    SUBPARTITION P1901A TABLESPACE I_PDB1_1901_A, 
    SUBPARTITION P1902A TABLESPACE I_PDB1_1902_A      
 ),
 PARTITION PB TABLESPACE I_PDB1_PB
 (
    SUBPARTITION P1901B TABLESPACE I_PDB1_1901_B, 
    SUBPARTITION P1902B TABLESPACE I_PDB1_1902_B      
 )
);

create index ind_test_hash_range_tab_1 on test_hash_range_tab(range_tab_type,range_tab_date,range_tab_no)
local 
(
 PARTITION PA TABLESPACE I_PDB1_PA
 (
    SUBPARTITION P1901A TABLESPACE I_PDB1_1901_A, 
    SUBPARTITION P1902A TABLESPACE I_PDB1_1902_A      
 ),
 PARTITION PB TABLESPACE I_PDB1_PB
 (
    SUBPARTITION P1901B TABLESPACE I_PDB1_1901_B, 
    SUBPARTITION P1902B TABLESPACE I_PDB1_1902_B      
 )
);  

你可能感兴趣的:(分區表創建)