• Data Warehouse Service

dws
  1. Help Center
  2. Data Warehouse Service
  3. Developer Guide
  4. SQL Reference
  5. SQL Syntax
  6. ALTER TABLE PARTITION

ALTER TABLE PARTITION

Function

ALTER TABLE PARTITION modifies table partition, including add, delete, split, merge partitions, and modify partition attributes.

Precautions

  • The name of the added partition must be different from names of existing partitions in the partition table.
  • The partition key of the added partition must be the same type as that of the partition table. The key value of the added partition must exceed the upper limit of the last partition range.
  • If the number of partitions in the target partition table has reached the maximum (32767), partitions cannot be added.
  • If a partition table has only one partition, the partition cannot be deleted.
  • Use PARTITION FOR() to choose partitions. The number of specified values in the brackets should be the same as the column number in customized partition, and they must be consistent.
  • The Value partition table does not support the Alter Partition operation.

Syntax

  • Modify the syntax of the table partition.
    ALTER TABLE [ IF EXISTS ] { table_name  [*] | ONLY table_name | ONLY ( table_name  )}
        action [, ... ];
    The action syntax is as follows:
    move_clause  |
        exchange_clause  |
        row_clause  |
        merge_clause  |
        modify_clause  |
        split_clause  |
        add_clause  |
        drop_clause
    • The move_clause syntax is used to move the partition to a new tablespace.
      MOVE PARTITION { partion_name | FOR ( partition_value [, ...] ) } TABLESPACE tablespacename
    • The exchange_clause syntax is used to move the data from an ordinary table to a specified partition.
      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 partition whose data is to be exchanged must meet the following requirements:

      • The number of columns of the ordinary table is the same as that of the partition, and their information should be consistent, including: column name, data type, constraint, collation information, storage parameter, and compress information.
      • The compressed information of the ordinary table and partition table should be consistent.
      • The distribution column information of the ordinary table and partition should be consistent.
      • The number and information of indexes of the ordinary table and partition should be consistent.
      • The number and information of constraints of the ordinary table and partition should be consistent.
      • The ordinary table cannot be a temporary table.

      When the exchange is done, the data and tablespace of the ordinary table and partition are exchanged. The statistics of ordinary tables and partitions are no longer inaccurate after the exchange, and they should be analyzed again.

    • The syntax of row_clause is used to set the row movement switch of a partitioned table.
      { ENABLE | DISABLE } ROW MOVEMENT
    • The merge_clause syntax is used to merge partitions into one partition.
      MERGE PARTITIONS { partition_name } [, ...] INTO PARTITION partition_name 
    • The syntax of modify_clause is used to set whether a partition index is usable.
      MODIFY PARTITION partition_name { UNUSABLE LOCAL INDEXES | REBUILD UNUSABLE LOCAL INDEXES }
    • The split_clause syntax is used to split one partition into partitions.
      SPLIT PARTITION { partition_name | FOR ( partition_value [, ...] ) } { split_point_clause | no_split_point_clause }
      • The syntax of specified split_point_clause is as follows:

        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 new partitions.

      • The syntax of no_split_point_clause is as follows:
        INTO { ( partition_less_than_item [, ...] ) | ( partition_start_end_item [, ...] ) }
        • The first new partition key specified by partition_less_than_item should be greater than that of the previously split partition (if any), and the last partition key specified by partition_item_clause should equal that of the partition being split.
        • The first new partition key specified by partition_start_end_item should equal that of the former partition (if any), and the last partition key specified by partition_start_end_item should equal that of the partition being split.
        • partition_less_than_item supports a maximum of four partition keys and partition_start_end_item supports only one partition key. For details about the supported data types, see Partition Key.
      • The syntax of partition_less_than_item is as follows:
        PARTITION partition_name VALUES LESS THAN ( { partition_value | MAXVALUE }  [, ...] ) 
            [ TABLESPACE tablespacename ]
      • The syntax of partition_start_end_item is as follows. For details about the constraints, see the description on START and END syntaxes.
        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]
    • The syntax of add_clause is used to add a partition to one or more specified partitioned tables.
      ADD {partition_less_than_item | partition_start_end_item}
    • The syntax of drop_clause is used to remove a specified partition from a partitioned table.
      DROP PARTITION  { partition_name | FOR (  partition_value [, ...] )  } 
  • The syntax of modifying table partition name is as follows:
    ALTER TABLE [ IF EXISTS ] { table_name [*] | ONLY table_name | ONLY ( table_name  )}
        RENAME PARTITION { partion_name | FOR ( partition_value [, ...] ) } TO partition_new_name;

Parameter Description

  • table_name

    Specifies the name of a partition table.

    Value range: An existing partition table name.

  • partition_name

    Specifies the name of a partition.

    Value range: An existing partition name.

  • partition_value

    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.

  • UNUSABLE LOCAL INDEXES

    Sets all the indexes unusable in the partition.

  • REBUILD UNUSABLE LOCAL INDEXES

    Rebuilds all the indexes in the partition.

  • ENABLE/DISABLE ROW MOVEMET

    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.

    Value range:

    • ENABLE: The row movement switch is enabled.
    • DISABLE: The row movement switch is disabled.

    The switch is disabled by default.

  • ordinary_table_name

    Specifies the name of the ordinary table whose data is to be migrated.

    Value range: An existing ordinary table name.

  • { WITH | WITHOUT } VALIDATION

    Checks whether the ordinary table data meets the specified partition key range of the partition to be migrated.

    Value range:

    • WITH: checks whether the common table data meets the partition key range of the partition to be exchanged. If any data does not meet the required range, an error is reported.
    • WITHOUT: does not check whether the common table data meets the partition key range of the partition to be exchanged.

    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.

  • VERBOSE

    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.

  • partition_new_name

    Specifies the new name of a partition.

    Value range: a string. It must comply with the naming convention rule.

Example

See Examples in CREATE TABLE PARTITION.