如何在 Oracle 中为现有 table 添加子分区

How to add Subpartition for existing table in Oracle

我想在 SUBPARTITION TEMPLATE 中为现有的 INT_TMP table 添加“US”市场的子分区。所以子分区应该是这样的: SUBPARTITION "SP_US" VALUES ( 'US' ) in SUBPARTITION TEMPLATE for exisitng INT_TMP table.

下面是我的创建 table 语句:

CREATE TABLE INT_TMP
   (LOAD_ID NUMBER 
, SYS_DB_NAME VARCHAR2(50 BYTE) 
, ENTITY_ID NUMBER 
, CHK_DATE DATE 
, VALUE NUMBER 
, NBR_CHECK VARCHAR2(50 BYTE) 
, SOURCE VARCHAR2(50 BYTE),
  MONTH_ID NUMBER
   ) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 
 NOCOMPRESS 
  STORAGE(
  BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE "DATA_INT_TCA_EDW" 
  PARTITION BY LIST ("MONTH_ID") 
  SUBPARTITION BY LIST ("SYS_DB_NAME") 
  SUBPARTITION TEMPLATE ( 
    SUBPARTITION "SP_SO" VALUES ( 'SO' ), 
    SUBPARTITION "SP_UK" VALUES ( 'UK' ), 
    SUBPARTITION "SP_OTHERS" VALUES ( DEFAULT ) ) 
 ( 
 PARTITION P_201801  VALUES (201801) 
PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 
  STORAGE(
  BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE DATA_INT_TCA_EDW , 
 PARTITION P_201802  VALUES (201802) 
PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 
  STORAGE(
  BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE DATA_INT_TCA_EDW , 
 PARTITION P_201803  VALUES (201803) 
PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 
  STORAGE(
  BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE DATA_INT_TCA_EDW , 
 PARTITION P_201804  VALUES (201804) 
PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 
  STORAGE(
  BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE DATA_INT_TCA_EDW , 
 PARTITION P_201805  VALUES (201805) 
PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 
  STORAGE(
  BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE DATA_INT_TCA_EDW , 
 PARTITION P_201806  VALUES (201806) 
PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 
  STORAGE(
  BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE DATA_INT_TCA_EDW ) ;

如果您想更改子分区模板,您可以这样做:

ALTER TABLE INT_TMP
SET SUBPARTITION TEMPLATE ( 
    SUBPARTITION "SP_SO" VALUES ( 'SO' ), 
    SUBPARTITION "SP_UK" VALUES ( 'UK' ), 
    SUBPARTITION "SP_US" VALUES ( 'US' ),
    SUBPARTITION "SP_OTHERS" VALUES ( DEFAULT ) )
/

Table INT_TMP altered.

但您可能想(也)拆分现有的默认子分区:

ALTER TABLE INT_TMP
SPLIT SUBPARTITION "P_201801_SP_OTHERS"
VALUES ('US') INTO (SUBPARTITION "P_201801_SP_US", SUBPARTITION "P_201801_SP_OTHERS")
PARALLEL
/

Table INT_TMP altered.

ALTER TABLE INT_TMP
SPLIT SUBPARTITION "P_201802_SP_OTHERS"
VALUES ('US') INTO (SUBPARTITION "P_201802_SP_US", SUBPARTITION "P_201802_SP_OTHERS")
PARALLEL
/

Table INT_TMP altered.

等等

如果您不想全部重新输入,可以从数据字典中生成 split 语句:

select 'ALTER TABLE INT_TMP SPLIT SUBPARTITION "' || subpartition_name || '"'
  || ' VALUES (''US'') INTO (SUBPARTITION "' || partition_name || '_SP_US",'
  || ' SUBPARTITION "' || subpartition_name || '");'
from user_tab_subpartitions
where subpartition_name = partition_name || '_SP_OTHERS';

然后 运行 那些生成的语句。 (或者做同样的事情,但是 运行 它们会自动,在匿名块中使用动态 SQL...)