All Products
Search
Document Center

Tablestore:Create a data table

Last Updated:Jun 19, 2025

This topic describes how to create a Tablestore data table using PHP SDK.

Usage notes

After you create a data table, wait until the table is loaded before performing operations on data. Otherwise, the operations will fail. This process typically takes several seconds.

Prerequisites

Initialize a Tablestore client

Method description

public function createTable(array $request)

$request parameter description

  • table_meta (required) object: The table structure information, which includes the following parameters.

    Name

    Type

    Description

    table_name (required)

    string

    The name of the data table.

    primary_key_schema (required)

    array

    The information about the primary key.

    • You can configure 1 to 4 primary key columns, which are sorted in ascending order by default. The first primary key column serves as the partition key.

    • The primary key data types include STRING, INTEGER, and BINARY. Integer-type primary key columns that are not partition keys can be set as auto-increment primary key columns.

    defined_column (optional)

    array

    The predefined column information.

    • Predefined columns are attribute columns that are predefined and can be used to create secondary indexes and search indexes.

    • The predefined column data types include STRING, INTEGER, BLOB, DOUBLE, or BOOLEAN.

  • table_options (optional) object: The table configuration information, which includes the following parameters.

    Name

    Type

    Description

    time_to_live (optional)

    int

    The data lifecycle in seconds. Default value: -1.

    • When this parameter is set to -1, the data never expires. Otherwise, the minimum value is 86400 (1 day). Data that exceeds the lifecycle will be automatically cleared.

    • If you want to use search indexes or secondary indexes, you must set the data lifecycle to -1 or set the allow_update parameter to false.

    max_versions (optional)

    int

    The maximum number of versions. Default value: 1.

    • If you want to use search indexes or secondary indexes, the maximum number of versions must be set to 1.

    deviation_cell_version_in_sec (optional)

    int

    The maximum version offset in seconds. Default value: 86400 (1 day).

    • The difference between the timestamp of the written data and the current system time must be within the maximum version offset range. Otherwise, the data write will fail.

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

    allow_update (optional)

    bool

    Specifies whether to allow updates. Default value: true.

    • When this parameter is set to false, you cannot update data by using the updateRow() method.

  • index_metas (optional) object: The list of secondary indexes. Each index includes the following parameters.

    Name

    Type

    Description

    name (required)

    string

    The name of the index.

    primary_key (required)

    array

    The primary key columns of the index.

    • The primary key columns of an index are a combination of primary key columns and predefined columns of the data table.

    • If you want to create a local secondary index, the first primary key column of the index must be the first primary key column of the data table.

    defined_column (required)

    array

    The predefined columns of the index.

    • The predefined columns are from the predefined columns of the data table.

    index_type (optional)

    string

    The index type. Valid values:

    • GLOBAL_INDEX (default value): global secondary index.

    • LOCAL_INDEX: local secondary index.

    index_update_mode (optional)

    string

    The index update mode. Valid values:

    • ASYNC_INDEX (default value): asynchronous update. The update mode for global secondary indexes must be set to asynchronous update.

    • SYNC_INDEX: synchronous update. The update mode for local secondary indexes must be set to synchronous update.

  • stream_spec (optional) object: The Stream configuration information, which includes the following parameters.

    Name

    Type

    Description

    enable_stream (optional)

    bool

    Specifies whether to enable Stream. Default value: false.

    expiration_time (optional)

    int

    The Stream expiration time, which indicates the retention period of incremental logs. Unit: hours. Maximum value: 168 (7 days).

    • If enable_stream is set to true, you must set expiration_time.

  • reserved_throughput (required) object: The reserved read and write throughput, in CU. Only high-performance instances in CU mode can be set to non-zero values and take effect.

Sample code

Create a data table

The following sample code creates a test_table table that contains one String type primary key.

$request = array (
    // Table structure information
    'table_meta' => array (
        'table_name' => 'test_table',
        // At least one primary key is required to create a data table
        'primary_key_schema' => array (
            array('id', PrimaryKeyTypeConst::CONST_STRING)
        ),
        // (Optional) Add predefined columns
        'defined_column' => array (
            array('name', DefinedColumnTypeConst::DCT_STRING)
        )
    ), 
    // (Optional) Table configuration information
    'table_options' => array (
        // (Optional) Data lifecycle, -1 indicates that data never expires
        'time_to_live' => -1,
        // (Optional) Maximum number of versions
        'max_versions' => 1,
        // (Optional) Maximum version offset
        'deviation_cell_version_in_sec' => 86400,
        // (Optional) Whether to allow updates
        'allow_update' => true
    ), 
    // (Optional) Set Stream information
    'stream_spec' => array (
        'enable_stream' => true,
        'expiration_time' => 168
    ),
    // Reserved read and write throughput must be set when creating a data table (only high-performance instances in CU mode support setting non-zero values for reserved read and write throughput)
    'reserved_throughput' => array (
        'capacity_unit' => array (
            'read' => 0,
            'write' => 0
        )
    )
);

try{
    $client->createTable( $request );
    echo "Create table succeeded.";
} catch (Exception $e) {
    echo "Create table failed.";
}

Create a data table with secondary indexes

The following sample code creates a data table with secondary indexes.

$request = array (
    // Table structure information
    'table_meta' => array (
        'table_name' => 'test_table',
        // Primary key
        'primary_key_schema' => array (
            array('id', PrimaryKeyTypeConst::CONST_STRING)
        ),
        // Predefined columns
        'defined_column' => array (
            array('name', DefinedColumnTypeConst::DCT_STRING)
        )
    ), 
    // Reserved read and write throughput (only high-performance instances in CU mode support setting non-zero values for reserved read and write throughput)
    'reserved_throughput' => array (
        'capacity_unit' => array (
            'read' => 0,
            'write' => 0
        )
    ),
    // Secondary index information
    'index_metas' => array(
        array(
            'name' => 'test_table_index',
            'primary_key' => array('id'),
            'defined_column' => array('name'),
            'index_type' => IndexTypeConst::LOCAL_INDEX,
            'index_update_mode' => IndexUpdateModeConst::SYNC_INDEX
        )
    )
);

try{
    $client->createTable( $request );
    echo "Create table succeeded.";
} catch (Exception $e) {
    echo "Create table failed.";
}

References