创建范围分区表
SQL> create table range_part_tab (id number,deal_date date,area_code number,contents varchar2(4000))
partition by range (deal_date)
(
partition p1 values less than (to_date(\'2020-02-01\',\'yyyy-mm-dd\')),
partition p2 values less than (to_date(\'2020-03-01\',\'yyyy-mm-dd\')),
partition p3 values less than (to_date(\'2020-04-01\',\'yyyy-mm-dd\')),
partition p4 values less than (to_date(\'2020-05-01\',\'yyyy-mm-dd\')),
partition p5 values less than (to_date(\'2020-06-01\',\'yyyy-mm-dd\')),
partition p6 values less than (to_date(\'2020-07-01\',\'yyyy-mm-dd\')),
partition p7 values less than (to_date(\'2020-08-01\',\'yyyy-mm-dd\')),
partition p8 values less than (to_date(\'2020-09-01\',\'yyyy-mm-dd\')),
partition p9 values less than (to_date(\'2020-10-01\',\'yyyy-mm-dd\')),
partition p10 values less than (to_date(\'2020-11-01\',\'yyyy-mm-dd\')),
partition p11 values less than (to_date(\'2020-12-01\',\'yyyy-mm-dd\')),
partition p12 values less than (to_date(\'2021-01-01\',\'yyyy-mm-dd\')),
partition p_max values less than (maxvalue)
);
Table created.
SQL> insert into range_part_tab (id,deal_date,area_code,contents)
select rownum,to_date(to_char(sysdate-365,\'j\')+trunc(dbms_random.value(0,730)),\'J\'),
ceil(dbms_random.value(590,599)),rpad(\'*\',400,\'*\') from dual
connect by rownum <=100000;
100000 rows created.
创建列表分区表
SQL> create table list_part_tab (id number,deal_date date,area_code number,contents varchar2(4000))
partition by list (area_code)
(
partition p_591 values(591),
partition p_592 values(592),
partition p_593 values(593),
partition p_594 values(594),
partition p_595 values(595),
partition p_596 values(596),
partition p_597 values(597),
partition p_598 values(598),
partition p_599 values(599),
partition p_other values (default)
);
Table created.
SQL> insert into list_part_tab (id,deal_date,area_code,contents)
select rownum,to_date(to_char(sysdate-365,\'j\')+trunc(dbms_random.value(0,365)),\'J\'),
ceil(dbms_random.value(590,599)),rpad(\'*\',400,\'*\') from dual
connect by rownum <=100000
100000 rows created.
创建哈希分区表
SQL> create table hash_part_tab (id number,deal_date date,area_code number,contents varchar2(4000))
partition by hash (id)
partitions 12;
Table created.
SQL> insert into hash_part_tab (id,deal_date,area_code,contents)
select rownum,to_date(to_char(sysdate-365,\'j\')+trunc(dbms_random.value(0,365)),\'J\'),
ceil(dbms_random.value(590,599)),rpad(\'*\',400,\'*\') from dual
connect by rownum <=100000
100000 rows created.
创建组合分区表
SQL> create table range_list_part_tab (id number,deal_date date,area_code number,contents varchar2(4000))
partition by range (deal_date)
subpartition by list (area_code)
subpartition template
(subpartition p_591 values(591),
subpartition p_592 values(592),
subpartition p_593 values(593),
subpartition p_594 values(594),
subpartition p_595 values(595),
subpartition p_596 values(596),
subpartition p_597 values(597),
subpartition p_598 values(598),
subpartition p_599 values(599),
subpartition p_other values (default))
(
partition p1 values less than (to_date(\'2020-02-01\',\'yyyy-mm-dd\')),
partition p2 values less than (to_date(\'2020-03-01\',\'yyyy-mm-dd\')),
partition p3 values less than (to_date(\'2020-04-01\',\'yyyy-mm-dd\')),
partition p4 values less than (to_date(\'2020-05-01\',\'yyyy-mm-dd\')),
partition p5 values less than (to_date(\'2020-06-01\',\'yyyy-mm-dd\')),
partition p6 values less than (to_date(\'2020-07-01\',\'yyyy-mm-dd\')),
partition p7 values less than (to_date(\'2020-08-01\',\'yyyy-mm-dd\')),
partition p8 values less than (to_date(\'2020-09-01\',\'yyyy-mm-dd\')),
partition p9 values less than (to_date(\'2020-10-01\',\'yyyy-mm-dd\')),
partition p10 values less than (to_date(\'2020-11-01\',\'yyyy-mm-dd\')),
partition p11 values less than (to_date(\'2020-12-01\',\'yyyy-mm-dd\')),
partition p12 values less than (to_date(\'2021-01-01\',\'yyyy-mm-dd\')),
partition p_max values less than (maxvalue)
);
Table created.
SQL> insert into range_list_part_tab (id,deal_date,area_code,contents)
select rownum,to_date(to_char(sysdate-365,\'j\')+trunc(dbms_random.value(0,365)),\'J\'),
ceil(dbms_random.value(590,599)),rpad(\'*\',400,\'*\') from dual
connect by rownum <=100000
100000 rows created.
分区交换
从分区表交换到普通表
SQL> create table mid_table (id number,deal_date date,area_code number,contents varchar2(4000));
Table created.
SQL> select count(*) from mid_table;
COUNT(*)
----------
0
SQL> select count(*) from range_part_tab partition(p8);
COUNT(*)
----------
8351
SQL> alter table range_part_tab exchange partition p8 with table mid_table;
Table altered.
SQL> select count(*) from range_part_tab partition(p8);
COUNT(*)
----------
0
SQL> select count(*) from mid_table;
COUNT(*)
----------
8351
从普通表交换到分区表
SQL> alter table range_part_tab exchange partition p8 with table mid_table;
Table altered.
SQL> select count(*) from range_part_tab partition(p8);
COUNT(*)
----------
8351
SQL> select count(*) from mid_table;
COUNT(*)
----------
0
分区切割
SQL> alter table range_part_tab split partition p_max at (to_date(\'2021-02-01\',\'yyyy-mm-dd\')) into (partition p2021_01,partition p_max);
Table altered.
分区合并
SQL> alter table range_part_tab merge partitions p2021_01,p_max into partition p_max;
Table altered.
增加与删除分区
SQL> alter table range_part_tab add partition p2021_01 values less than (to_date(\'2021-02-01\',\'yyyy-mm-dd\'));
alter table range_part_tab add partition p2021_01 values less than (to_date(\'2021-02-01\',\'yyyy-mm-dd\'))
*
ERROR at line 1:
ORA-14074: partition bound must collate higher than that of the last partition
SQL> alter table range_part_tab drop partition p_max;
Table altered.
SQL> alter table range_part_tab add partition p2021_01 values less than (to_date(\'2021-02-01\',\'yyyy-mm-dd\'));
Table altered.
SQL> alter table range_part_tab add partition p_max values less than (maxvalue);
Table altered.
分区截断
SQL> select count(*) from range_part_tab partition(p8);
COUNT(*)
----------
8351
SQL> alter table range_part_tab truncate partition p8;
Table truncated.
SQL> select count(*) from range_part_tab partition(p8);
COUNT(*)
----------
0