Interval Partitioning in Oracle 11g
Interval Partitioning in Oracle 11g
Example :
-- CREATE TABLE
CREATE TABLE INVOICES
(
INVOICE_NO NUMBER NOT NULL,
INVOICE_DATE DATE NOT NULL,
COMMENTS VARCHAR2(500)
)
PARTITION BY RANGE (INVOICE_DATE)
INTERVAL (NUMTOYMINTERVAL(1,'MONTH'))
(PARTITION P_FIRST VALUES LESS THAN ('01-JAN-2008') TABLESPACE USERS);
-- create indexes
CREATE UNIQUE INDEX INVNO_UNQ_IDX ON INVOICES(INVOICE_NO,INVOICE_DATE)
Also the function NUMTODSINTERVAL will be used to create partitions with intervals of
"WEEK","DAY","HOUR","MINUTE","SECOND".
SQL> COMMIT;
Adding data beyond the range of the initial partition(P_FIRST) will create new partitions as
shown below.
If data is inserted later than the current maximum high value(say like 6 months ahead),only the
required partition is created, not the intermediate partitions.
SQL> COMMIT;
The partition is created is for the month of "2008-07" and not for the intermediate months
between "2009-03" and "2009-08".
-- The following alter table statement will change a range-partitioned table to interval partitioned
based on the interval provided(yearly in this case).
The table will go back to range partitioned where you have to manually add partitions.
Manually adding a partition to a Interval Partitioned Table:
-------------------------------------------------------------
The database automatically creates a partition for an interval when data for that interval is
inserted.But if there's a scenario for a partition exchange where you need to explicitly create
interval partitions you can do it by using the "lock table partition for command shown below.
Create a staging table for invoices called "INVOICES_STAGING" that holds invoices for the
month of August,2008
SQL>
CREATE TABLE INVOICES_STAGING
(
INVOICE_NO NUMBER NOT NULL,
INVOICE_DATE DATE NOT NULL,
COMMENTS VARCHAR2(500)
);
SQL> COMMIT;
The data from the staging table "INVOICES_STAGING" can now be exchanged onto the main
"INVOICES" table.
First, manually create the partition on the main "INVOICES" table using the following lock
command
The last partition "SYS_P4013" is the partition creating using the lock command.
Exchange the data for August,2008 from the staging table "INVOICES_STAGING" to the main
table "INVOICES" using the following alter command.
-- The data for the month of August 2008 is exchanged into "INVOICES" table
SQL> select t.invoice_no,t.invoice_date from invoices t;
INVOICE_NO INVOICE_D
---------- ---------
1 16-JAN-08
1 16-FEB-08
3 16-JUL-08
5 18-AUG-08
6 21-AUG-08
7 31-AUG-08.