All Products
Search
Document Center

Tablestore:Create a data table

Last Updated:Apr 29, 2026

Create a Tablestore data table using the Tablestore SDK for Python. Configure the table schema, options, secondary indexes, and encryption settings as needed.

Usage notes

After creating a data table, wait for it to finish loading before performing any operations. This typically takes a few seconds. Operations attempted before the table is ready will fail.

Prerequisites

Before you begin, ensure that you have:

Method

def create_table(self, table_meta, table_options, reserved_throughput, secondary_indexes=[], sse_spec)

Parameters

  • table_meta (required) TableMeta: the schema of the table, including the following parameters.

    Name

    Type

    Description

    table_name (required)

    str

    The name of the data table.

    schema_of_primary_key (required)

    List[Tuple]

    The primary key configuration.

    • Define 1 to 4 primary key columns, sorted in ascending order by default. The first column serves as the partition key.

    • Primary key columns support STRING, INTEGER, and BINARY types. Set an auto-increment primary key on any non-partition-key column of type INTEGER.

    defined_columns (optional)

    List[Tuple]

    The predefined columns configuration.

    • Predefined columns are attribute columns defined at table creation that can be used as index columns for secondary indexes and search indexes.

    • Predefined columns support STRING, INTEGER, BINARY, DOUBLE, and BOOLEAN types.

  • table_options (required) TableOptions: the table configuration, including the following parameters.

    Name

    Type

    Description

    time_to_live (optional)

    int

    The time to live (TTL) of data, in seconds. Default: -1.

    • Set to -1 to keep data indefinitely. Otherwise, the minimum value is 86400 (one day). Data older than the TTL is automatically deleted.

    • To use search indexes or secondary indexes, set this parameter to -1 or set allow_update to False.

    max_version (optional)

    int

    The maximum number of versions to retain per attribute column. Default: 1.

    • To use search indexes or secondary indexes, set this parameter to 1.

    max_time_deviation (optional)

    int

    The max version offset, in seconds. Default: 86400 (one day).

    • The timestamp of written data must be within this offset from the current system time. Data written outside this range will fail.

    • The valid version range for attribute column data is [max(Data written time - Maximum version offset, Data written time - TTL), Data written time + Maximum version offset).

    allow_update (optional)

    bool

    Specifies whether row updates are allowed. Default: True.

    • Set to False to disable updates via the update_row() method.

  • secondary_indexes (optional) List[SecondaryIndexMeta]: the list of secondary indexes. Each index includes the following parameters.

    Name

    Type

    Description

    index_name (required)

    str

    The name of the index.

    primary_key_names (required)

    List[str]

    The primary key columns of the index.

    • Can include the data table's primary key columns and predefined columns.

    • For a local secondary index, the first primary key column of the index must match the first primary key column of the data table.

    defined_column_names (optional)

    List[str]

    The predefined columns included in the index. Must be columns already defined in the data table.

    index_type (optional)

    SecondaryIndexType

    The type of the index. Valid values:

    • GLOBAL_INDEX (default): global secondary index.

    • LOCAL_INDEX: local secondary index.

  • reserved_throughput (required) ReservedThroughput: the reserved throughput, in capacity units (CU). Default: 0. A non-zero value only takes effect for data tables in high-performance instances running in CU mode.

  • sse_spec (optional) SSESpecification: the data encryption settings, including the following parameters.

    Important

    Configure data encryption only when creating a data table. Encryption cannot be disabled after the table is created. This feature requires Python SDK version 6.4.0 or later.

    Name

    Type

    Description

    enable (required)

    boolean

    Specifies whether to enable data encryption. Default: False.

    key_type (optional)

    SSEKeyType

    The encryption type. Valid values of SSEKeyType:

    • SSE_KMS_SERVICE: KMS encryption.

    • SSE_BYOK: BYOK encryption.

    key_id (optional)

    str

    The ID of the customer master key (CMK). Required only when key_type is SSE_BYOK.

    role_arn (optional)

    str

    The ARN of the RAM role. Required only when key_type is SSE_BYOK.

Examples

The following example creates a data table named test_table with a single primary key column of type STRING.

# A data table requires at least one primary key column.
schema_of_primary_key = [('id', 'STRING')]
# Define the table schema.
table_meta = TableMeta('test_table', schema_of_primary_key)

# Configure table options.
table_options = TableOptions(time_to_live=-1, max_version=1, max_time_deviation=86400, allow_update=True)

# Set the reserved throughput. Default is 0 CU.
# A non-zero value only takes effect for high-performance instances in CU mode.
reserved_throughput = ReservedThroughput(CapacityUnit(0,0))

try:
    client.create_table(table_meta, table_options, reserved_throughput)
    print("Create table succeeded.")
except Exception as e:
    print("Create table failed. %s" % e)

The following examples show how to configure specific settings when creating a data table.

  • Add predefined columns

    defined_columns = [('name', 'STRING')]
    # Include predefined columns in the table schema.
    table_meta = TableMeta('test_table', schema_of_primary_key, defined_columns)
  • Add secondary indexes

    # Define the secondary index list.
    secondary_indexes = [
        # Specify the index name, primary key columns, predefined columns, and index type.
        SecondaryIndexMeta('test_table_index', ['id', 'name'], [], index_type= SecondaryIndexType.LOCAL_INDEX)
    ]
    client.create_table(table_meta, table_options, reserved_throughput, secondary_indexes)
  • Configure data encryption

    Use the SSESpecification class to configure server-side encryption (SSE) for the data table.

    • KMS encryption

      sse_specification = SSESpecification(enable=True, key_type=SSEKeyType.SSE_KMS_SERVICE, key_id=None,
                                                   role_arn=None)
      client.create_table(table_meta, table_option, reserved_throughput, sse_spec=sse_specification)
    • BYOK encryption

      Note

      Before running this code, get the CMK ID and the RAM role ARN. For more information, see BYOK encryption.

      key_id = "key-hzz6*****************"
      role_arn = "acs:ram::1705************:role/tabletorebyok"
      sse_specification = SSESpecification(enable=True, key_type=SSEKeyType.SSE_BYOK, key_id=key_id,
                                                   role_arn=role_arn)
      client.create_table(table_meta, table_option, reserved_throughput, sse_spec=sse_specification)

References