All Products
Search
Document Center

PolarDB:KEY-RANGE

Last Updated:Feb 06, 2024

This topic describes how to create a key-range partitioned table.

Syntax

The following statement is used to create one or more key-range partitioned table where each partition may contain one or more subpartitions:

CREATE TABLE [ schema. ]table_name
 table_definition
   PARTITION BY [LINEAR] KEY (column_list)
   SUBPARTITION BY RANGE (expr)
   (partition_definition [, partition_definition] ...)

partition_definition is:

 PARTITION partition_name
        (subpartition_definition [, subpartition_definition] ...)

subpartition_definition is:

SUBPARTITION subpartition_name
   VALUES LESS THAN {value| MAXVALUE}

Parameters

Parameter

Description

table_name

The name of the table.

expr

The expression of the partition. It must be of the INT type. The string type is not supported.

column_list

The list of partitions. It is used in RANGE COLUMNS(). Expressions are not supported.

value

The boundary value of the partition.

MAXVALUE

The maximum value of the partition.

partition_name

The name of the partition. The name must be unique within the table.

subpartition_name

The name of the subpartition. The name must be unique within the table.

Examples

Create a key-range partitioned table:

CREATE TABLE sales_key_range
(
  dept_no     varchar(20),
  part_no     INT,
  country     varchar(20),
  date        DATE,
  amount      INT
)
		PARTITION BY KEY(dept_no)
     SUBPARTITION BY RANGE COLUMNS(date)
(
  PARTITION  p0(
            SUBPARTITION p0_q1_2023 VALUES LESS THAN('2023-04-01'),
            SUBPARTITION p0_q2_2023 VALUES LESS THAN('2023-07-01'),
            SUBPARTITION p0_q3_2023 VALUES LESS THAN('2023-10-01'),
            SUBPARTITION p0_q4_2023 VALUES LESS THAN('2024-01-01')
        ),
  PARTITION  p1(
            SUBPARTITION p1_q1_2023 VALUES LESS THAN('2023-04-01'),
            SUBPARTITION p1_q2_2023 VALUES LESS THAN('2023-07-01'),
            SUBPARTITION p1_q3_2023 VALUES LESS THAN('2023-10-01'),
            SUBPARTITION p1_q4_2023 VALUES LESS THAN('2024-01-01')
        ),
  PARTITION  p2(
            SUBPARTITION p2_q1_2023 VALUES LESS THAN('2023-04-01'),
            SUBPARTITION p2_q2_2023 VALUES LESS THAN('2023-07-01'),
            SUBPARTITION p2_q3_2023 VALUES LESS THAN('2023-10-01'),
            SUBPARTITION p2_q4_2023 VALUES LESS THAN('2024-01-01')
        ),
  PARTITION  p3(
            SUBPARTITION p3_q1_2023 VALUES LESS THAN('2023-04-01'),
            SUBPARTITION p3_q2_2023 VALUES LESS THAN('2023-07-01'),
            SUBPARTITION p3_q3_2023 VALUES LESS THAN('2023-10-01'),
            SUBPARTITION p3_q4_2023 VALUES LESS THAN('2024-01-01')
        )
);