Lesson 6 | Manipulating Partitions |
Objective | Rename, move, and coalesce Partitions |
You can rename a partition in an interval, range, list, or hash-partitioned table using the ALTER TABLE
statement:
ALTER TABLE table_name RENAME PARTITION old_partition_name TO new_partition_name;
Example:
ALTER TABLE sales RENAME PARTITION sales_q1 TO sales_2024_q1;
Moving a partition is useful for changing tablespace storage or reorganizing data.
ALTER TABLE table_name MOVE PARTITION partition_name TABLESPACE new_tablespace;
Example:
ALTER TABLE sales MOVE PARTITION sales_q1 TABLESPACE sales_archive;
For index-organized tables, you must include the UPDATE INDEXES
clause to maintain index integrity:
ALTER TABLE sales MOVE PARTITION sales_q1 TABLESPACE sales_archive UPDATE INDEXES;
The ALTER TABLE ... COALESCE PARTITION
command merges adjacent partitions in hash-partitioned tables.
ALTER TABLE table_name COALESCE PARTITION;
ALTER TABLE sales COALESCE PARTITION;
ALTER TABLE tablename RENAME current_partition_name TO new_partition_name;
ELVIS_ERA
partition in the CD_LIBRARY
table to be called the HANK_WILLIAMS_ERA
partition, the command looks like this:
ALTER TABLE CD_LIBRARY RENAME ELVIS_ERA TO HANK_WILLIAMS_ERA;
ALTER TABLE tablename MOVE partition_name new_tablespacename;
HANK_WILLIAMS_ERA
in the CD_LIBRARY
table to a new tablespace called HANK_TS
, you would use this command:
ALTER TABLE CD_LIBRARY MOVE HANK_WILLIAMS_ERA TABLESPACE HANK_TS;
ALTER TABLE
command to accomplish the task. Merging partitions might be useful when you find that two partitions contain only a few rows because the range in both partitions was originally defined as too narrow a range. ALTER TABLE tablename MERGE PARTITIONS partition1, partition2 INTO PARTITION newpartition
newpartition
takes on the partition values of the higher partition.ALTER TABLE WHOLESALE_ORDER MERGE PARTITIONS LOW_PART, MED_PART INTO PARTITION LOWMED_PART
ALTER TABLE WHOLESALE_ORDER MOVE PARTITION LOW_PART TABLESPACE LOW_PART_TS / ALTER TABLE WHOLESALE_ORDER MERGE PARTITIONS LOW_PART, MED_PART INTO PARTITION LOW1_PART / ALTER TABLE WHOLESALE_ORDER RENAME PARTITION LOW1_PART TO LOWEST_PART /
ALTER TABLE ... MOVE PARTITIONclause.
ALTER TABLE current_table MOVE PARTITION partition_name TABLESPACE destination_table_space LOB (column_name) STORE AS (TABLESPACE current_tablespace);