11g之前,维护分区需要手工。11g之后使用interval来实现自动扩展分区,简化了维护。
根据年 INTERVAL(NUMTOYMINTERVAL(1,‘YEAR‘))
根据月 INTERVAL(NUMTOYMINTERVAL(1,‘MONTH‘))
根据天 INTERVAL(NUMTODSINTERVAL(1,‘DAY‘))
下面用按月自动扩展来做个试验:
SQL> create table t_range (id number not null PRIMARY KEY, test_date date)
partition by range (test_date) interval (numtoyMinterval (1,‘MONTH‘))
(
partition p_2014_01_01 values less than (to_date(‘2014-01-01‘, ‘yyyy-mm-dd‘))
);
--看到只有一个分区
SQL> select partition_name from user_tab_partitions where table_name=‘T_RANGE‘;
PARTITION_NAME
------------------------------
P_2014_01_01
SQL> insert /*+append */ into t_range select rownum,
to_date(to_char(sysdate - 140, ‘J‘) +
trunc(dbms_random.value(0, 80)),
‘J‘)
from dual
connect by rownum <= 100000;
SQL> commit;
--可以看到SYS开头的分区是自动扩展的
SQL> select partition_name from user_tab_partitions where table_name=‘T_RANGE‘;
PARTITION_NAME
------------------------------
P_2014_01_01
SYS_P21
SYS_P22
SYS_P23
--再看看t_range的表结构
create table T_RANGE
(
ID NUMBER not null,
TEST_DATE DATE
)
partition by range (TEST_DATE)
(
partition P_2014_01_01 values less than (TO_DATE(‘ 2014-01-01 00:00:00‘,
‘SYYYY-MM-DD HH24:MI:SS‘, ‘NLS_CALENDAR=GREGORIAN‘)),
partition SYS_P21 values less than (TO_DATE(‘ 2014-02-01 00:00:00‘,
‘SYYYY-MM-DD HH24:MI:SS‘, ‘NLS_CALENDAR=GREGORIAN‘)),
partition SYS_P22 values less than (TO_DATE(‘ 2014-03-01 00:00:00‘,
‘SYYYY-MM-DD HH24:MI:SS‘, ‘NLS_CALENDAR=GREGORIAN‘)),
partition SYS_P23 values less than (TO_DATE(‘ 2014-04-01 00:00:00‘,
‘SYYYY-MM-DD HH24:MI:SS‘, ‘NLS_CALENDAR=GREGORIAN‘))
);
-- Create/Recreate primary, unique and foreign key constraints
alter table T_RANGE
add primary key (ID);
--如果对分区名不太爽,则可以自己修改一下:
alter table t_range rename partition SYS_P21 to p_2014_02_01;
alter table t_range rename partition SYS_P22 to p_2014_03_01;
alter table t_range rename partition SYS_P23 to p_2014_04_01;
Oracle 11g下自动创建分区