DB2 Version 9.7 for Linux, UNIX, and Windows

Scenario: Rotating data in a partitioned table

Rotating data in DB2® databases refers to a method of reusing space in a data partition by removing obsolete data from the table then adding new data. Table partitioning functionality allows you to detach the data partition with the obsolete data then attach a new data partition with the latest data.

About this task

To rotate data in a partitioned table, from the command line, issue the ALTER TABLE statement. The following example demonstrates how to update the stock table by removing the data from December 2001 and replacing it with the latest data from December 2003.
  1. Remove the old data from table stock.
    	ALTER TABLE stock DETACH PARTITION dec01 INTO newtable;
  2. Load the new data. Using LOAD with the REPLACE option overwrites existing data.
    LOAD FROM data_file OF DEL REPLACE INTO newtable
    Note: If there are detached dependents, then you must run the SET INTEGRITY statement on the detached dependents before you can load the detached table.
  3. If desired, perform data cleansing. Data cleansing activities include:
    • Filling in missing values
    • Deleting inconsistent and incomplete data
    • Removing redundant data arriving from multiple sources
    • Transforming data
      • Normalization (Data from different sources that represents the same value in different ways must be reconciled as part of rolling the data into the warehouse.)
      • Aggregation (Raw data that is too detailed to store in the warehouse must be pre-aggregated during roll-in.)
  4. Attach the new data as a new range.
    ALTER TABLE stock ATTACH PARTITION dec03 
    STARTING '12/01/2003' ENDING '12/31/2003'
     FROM newtable;

    Attaching a data partition drains queries and invalidates packages.

  5. Use the SET INTEGRITY statement to update indexes and other dependent objects. Read and write access is permitted during the execution of the SET INTEGRITY statement.
    SET INTEGRITY FOR stock ALLOW WRITE ACCESS 
    IMMEDIATE CHECKED FOR EXCEPTION IN stock USE stock_ex;

Before you begin

To detach a data partition from a partitioned table the user must have the following authorities or privileges: To alter a table to attach a data partition, the user must have the following authorities or privileges: