DB2 Version 9.7 for Linux, UNIX, and Windows

Adding data partitions to partitioned tables

You can use the ALTER TABLE statement to modify a partitioned table after the table is created. Specifically, you can use the ADD PARTITION clause to add a new data partition to an existing partitioned table. Adding a data partition to a partitioned table is more appropriate than attaching a data partition in situations where data is added to the data partition over time, when data is trickling in rather than rolling in from an external source, or when you are inserting or loading data directly into a partitioned table. Specific examples include daily loads of data into a data partition for January data, or ongoing inserts of individual rows.

About this task

To add the new data partition to a specific table space location, the IN clause is added as an option on the ALTER TABLE ADD PARTITION statement.

To add the partitioned index of a new data partition to a specific table space location separate from the table space location of the data partition, the partition level INDEX IN clause is added as an option on the ALTER TABLE ADD PARTITION statement. If no INDEX IN option is specified, by default any partitioned indexes on the new data partition will reside in the same table space as the data partition. If any partitioned indexes exist on the partitioned table, ADD PARTITION creates the corresponding empty index partitions for the new partition. A new index partition entry is inserted into the system catalog table SYSIBM.SYSINDEXPARTITIONS for each partitioned index.

To add the Long data, LOB, or XML data of a new data partition to a specific table space location separate from the table space location of the data partition, the partition level LONG IN clause is added as an option on the ALTER TABLE ADD PARTITION statement.

Restrictions and usage guidelines

  • You cannot add a data partition to a non-partitioned table. For details on migrating an existing table to a partitioned table, see Migrating existing tables and views to partitioned tables.
  • The range of values for each new data partition, are determined by the STARTING and ENDING clauses.
  • One or both of the STARTING and ENDING clauses must be supplied.
  • The new range must not overlap with the range of an existing data partition.
  • When adding a new data partition before the first existing data partition, the STARTING clause must be specified. Use MINVALUE to make this range open ended.
  • Likewise, the ENDING clause must be specified if you want to add a new data partition after the last existing data partition. Use MAXVALUE to make this range open ended.
  • If the STARTING clause is omitted, then the database manufactures a starting bound just after the ending bound of the previous data partition. Likewise, if the ENDING clause is omitted, the database manufactures an ending bound just before the starting bound of the next data partition.
  • The start-clause and end-clause syntax is the same as specified in the CREATE TABLE statement.
  • If no IN, INDEX IN, or LONG IN clause is specified for ADD PARTITION, the table space in which to place the data partition is chosen using the same method as is used by the CREATE TABLE statement.
  • Packages are invalidated during the ALTER TABLE ...ADD PARTITION operation.
  • The newly added data partition is available once the ALTER TABLE statement is committed.
Omitting the STARTING or ENDING bound for an ADD operation is also used to fill a gap in range values. Here is an example of filling in a gap using the ADD operation where only the starting bound is specified:
CREATE TABLE hole (c1 int) PARTITION BY RANGE (c1)
(STARTING FROM 1 ENDING AT 10, STARTING FROM 20 ENDING AT 30);
DB20000I The SQL command completed successfully.
ALTER TABLE hole ADD PARTITION STARTING 15;
DB20000I The SQL command completed successfully.
SELECT SUBSTR(tabname, 1,12) tabname, 
SUBSTR(datapartitionname, 1, 12) datapartitionname,
seqno, SUBSTR(lowvalue, 1, 4) lowvalue, SUBSTR(highvalue, 1, 4) highvalue
FROM SYSCAT.DATAPARTITIONS WHERE TABNAME='HOLE' ORDER BY seqno;
TABNAME DATAPARTITIONNAME SEQNO LOWVALUE HIGHVALUE
------------ ----------------- ----------- -------- ---------
HOLE PART0 0 1 10 
HOLE PART2 1 15 20 
HOLE PART1 2 20 30 

3 record(s) selected.
Example 1:  Add a data partition to an existing partitioned table holding a range of values 901 to 1000 inclusive. Assume table sales holds nine ranges 0-100, 101-200, and so on, up to the value of 900. The example adds an additional range at the end of the table, indicated by the exclusion of the STARTING clause:
ALTER TABLE sales ADD PARTITION dp10
ENDING AT 1000 INCLUSIVE

To add the partitioned index of a new data partition to a specific table space location separate from the table space location of the data partition, the partition level INDEX IN clause is added as an option on the ALTER TABLE ADD PARTITION statement. If no INDEX IN option is specified, by default any partitioned indexes on the new data partition will reside in the same table space as the data partition. If any partitioned indexes exist on the partitioned table, ADD PARTITION creates the corresponding empty index partitions for the new partition. A new index partition entry is inserted into the system catalog table SYSIBM. SYSINDEXPARTITIONS for each partitioned index.

Example 2: Add a data partition to an existing partitioned table separating out long data and indexes from the rest of the data partition.
ALTER TABLE newbusiness ADD PARTITION IN tsnewdata
INDEX IN tsnewindex LONG IN tsnewlong