ALTER TABLE PARTITION modifies table partitioning, including adding, deleting, splitting, merging partitions, and modifying partition attributes.
1 2 | ALTER TABLE [ IF EXISTS ] { table_name [*] | ONLY table_name | ONLY ( table_name )} action [, ... ]; |
1 2 3 4 5 6 7 8 | move_clause | exchange_clause | row_clause | merge_clause | modify_clause | split_clause | add_clause | drop_clause |
1 | MOVE PARTITION { partition_name | FOR ( partition_value [, ...] ) } TABLESPACE tablespacename |
1 2 3 | EXCHANGE PARTITION { ( partition_name ) | FOR ( partition_value [, ...] ) } WITH TABLE {[ ONLY ] ordinary_table_name | ordinary_table_name * | ONLY ( ordinary_table_name )} [ { WITH | WITHOUT } VALIDATION ] [ VERBOSE ] |
The ordinary table and the partitioned table whose data is to be exchanged must meet the following requirements:
When the execution is complete, the data and tablespace of the ordinary table and the partitioned table are exchanged. In this case, statistics about the ordinary table and the partitioned table become unreliable. Both tables should be analyzed again.
1 | { ENABLE | DISABLE } ROW MOVEMENT |
1 2 | MERGE PARTITIONS { partition_name } [, ...] INTO PARTITION partition_name |
1 | MODIFY PARTITION partition_name { UNUSABLE LOCAL INDEXES | REBUILD UNUSABLE LOCAL INDEXES } |
1 | SPLIT PARTITION { partition_name | FOR ( partition_value [, ...] ) } { split_point_clause | no_split_point_clause } |
1 | AT ( partition_value ) INTO ( PARTITION partition_name , PARTITION partition_name ) |
The size of split point should be in the range of splitting partition key. The split point can only split one partition into two.
1 | INTO { ( partition_less_than_item [, ...] ) | ( partition_start_end_item [, ...] ) } |
1 2 | PARTITION partition_name VALUES LESS THAN ( { partition_value | MAXVALUE } [, ...] ) [ TABLESPACE tablespacename ] |
1 2 3 4 5 6 | PARTITION partition_name { {START(partition_value) END (partition_value) EVERY (interval_value)} | {START(partition_value) END ({partition_value | MAXVALUE})} | {START(partition_value)} | {END({partition_value | MAXVALUE})} } [TABLESPACE tablespace_name] |
1 | ADD {partition_less_than_item | partition_start_end_item} |
1 | DROP PARTITION { partition_name | FOR ( partition_value [, ...] ) } |
1 2 | ALTER TABLE [ IF EXISTS ] { table_name [*] | ONLY table_name | ONLY ( table_name )} RENAME PARTITION { partition_name | FOR ( partition_value [, ...] ) } TO partition_new_name; |
Specifies the name of a partitioned table.
Value range: an existing partitioned table name
Specifies the name of a partition.
Value range: an existing partition name
Specifies the key value of a partition.
The value specified by PARTITION FOR ( partition_value [, ...] ) can uniquely identify a partition.
Value range: value range of the partition key for the partition to be renamed
Sets all the indexes unusable in the partition.
Rebuilds all the indexes in the partition.
Specifies the row movement switch.
If the tuple value is updated on the partition key during the UPDATE action, the partition where the tuple is located is altered. Setting of this parameter enables error messages to be reported or movement of the tuple between partitions.
Valid value:
The switch is disabled by default.
Specifies the name of the ordinary table whose data is to be migrated.
Value range: an existing ordinary table name
Checks whether the ordinary table data meets the specified partition key range of the partition to be migrated.
Valid value:
The default value is WITH.
The check is time consuming, especially when the data volume is large. Therefore, use WITHOUT when you are sure that the current common table data meets the partition key range of the partition to be exchanged.
When VALIDATION is WITH, if the ordinary table contains data that is out of the partition key range, insert the data to the correct partition. If there is no correct partition where the data can be route to, an error is reported.
Only when VALIDATION is WITH, VERBOSE can be specified.
Specifies the new name of a partition.
Value range: a string. It must comply with the naming convention.
Delete partition P8.
1 | ALTER TABLE tpcds.web_returns_p1 DROP PARTITION P8; |
Add a partition WR_RETURNED_DATE_SK with values ranging from 2453005 to 2453105.
1 | ALTER TABLE tpcds.web_returns_p1 ADD PARTITION P8 VALUES LESS THAN (2453105); |
Add a partition WR_RETURNED_DATE_SK with values ranging from 2453105 to MAXVALUE.
1 | ALTER TABLE tpcds.web_returns_p1 ADD PARTITION P9 VALUES LESS THAN (MAXVALUE); |
Rename the P7 partition as P10.
1 | ALTER TABLE tpcds.web_returns_p1 RENAME PARTITION P7 TO P10; |
Rename the P6 partition as P11.
1 | ALTER TABLE tpcds.web_returns_p1 RENAME PARTITION FOR (2452639) TO P11; |
Query rows in the P10 partition.
1 2 3 4 5 | SELECT count(*) FROM tpcds.web_returns_p1 PARTITION (P10); count -------- 9362 (1 row) |
Split the P8 partition at 2453010.
1 2 3 4 5 | ALTER TABLE tpcds.web_returns_p2 SPLIT PARTITION P8 AT (2453010) INTO ( PARTITION P9, PARTITION P10 ); |
Merge the P6 and P7 partitions into one.
1 | ALTER TABLE tpcds.web_returns_p2 MERGE PARTITIONS P6, P7 INTO PARTITION P8; |
Modify the migration attribute of a partitioned table.
1 | ALTER TABLE tpcds.web_returns_p2 DISABLE ROW MOVEMENT; |
Add partitions [5000, 5300), [5300, 5600), [5600, 5900), and [5900, 6000).
1 | ALTER TABLE tpcds.startend_pt ADD PARTITION p6 START(5000) END(6000) EVERY(300); |
Add the partition p7, specified by MAXVALUE.
1 | ALTER TABLE tpcds.startend_pt ADD PARTITION p7 END(MAXVALUE); |
Rename the partition where 5950 is located to p71.
1 | ALTER TABLE tpcds.startend_pt RENAME PARTITION FOR(5950) TO p71; |
Split the partition [4000, 5000) where 4500 is located.
1 | ALTER TABLE tpcds.startend_pt SPLIT PARTITION FOR(4500) INTO(PARTITION q1 START(4000) END(5000) EVERY; |