All Products
Search
Document Center

Tablestore:Configure an auto-increment primary key column

Last Updated:Jul 30, 2025

This topic describes how to configure an auto-increment primary key column for a data table using Tablestore SDK for PHP, and how to write data to the auto-increment column and get the generated value.

Usage notes

  • Tablestore SDK for PHP V4.0.0 or later supports auto-increment primary key columns.

  • The auto-increment values from an auto-increment column are unique and increase at the partition key level, but they are not guaranteed to be consecutive.

Prerequisites

Initialize an OTSClient instance

Configure an auto-increment primary key column

You can set a non-partition primary key column as an auto-increment column when creating a data table. For existing data tables, you cannot configure auto-increment columns.

Note

Only non-partition primary key columns of the INTEGER type can be set as auto-increment columns. A data table can have at most one auto-increment column. The values generated for an auto-increment column are 64-bit signed long integers.

Sample code

The following sample code creates a data table named test_table. The primary key of the table includes the partition key id and the auto-increment column incr.

function createTable($client) 
{
    $request = [
        'table_meta' => [
            'table_name' => 'test_table',
            'primary_key_schema' => [
                ['id', PrimaryKeyTypeConst::CONST_STRING],
                ['incr', PrimaryKeyTypeConst::CONST_INTEGER, PrimaryKeyOptionConst::CONST_PK_AUTO_INCR]
            ]
        ],
        'reserved_throughput' => [
            'capacity_unit' => [
                'read' => 0,
                'write' => 0
            ]
        ],
        'table_options' => [
            'time_to_live' => -1,
            'max_versions' => 1,
            'deviation_cell_version_in_sec' => 86400
        ]
    ];
    $client->createTable($request);
}

Write data

When writing data to an auto-increment column, you only need to set its value to a placeholder. To get the generated auto-increment value for queries and updates, you must set the return type of putRow to CONST_PK.

Sample code

The following sample code writes a row of data to the test_table table, and obtains and prints the primary key information of the written row data.

function putRow($client)
{
    $row = [
        'table_name' => 'test_table',
        'primary_key' => [
            ['id', 'row1'],
            // Configure the auto-increment column.
            ['incr', null, PrimaryKeyTypeConst::CONST_PK_AUTO_INCR]
        ],
        'attribute_columns' => [
            ['col1', 'val1']
        ],
        'return_content' => [
            'return_type' => ReturnTypeConst::CONST_PK
        ]
    ];
    $result = $client->putRow($row);
    // CU consumption for read and write operations.
    echo "Read CU Cost: " . $result['consumed']['capacity_unit']['read'] . "\n";
    echo "Write CU Cost: " . $result['consumed']['capacity_unit']['write'] . "\n";

    // Obtain and print the returned primary key information. If the return type is not set to CONST_PK, the primary key information is not returned by default.
    if(!empty($result['primary_key'])){
        echo "Primary key: ";
        foreach ($result['primary_key'] as $primaryKey) {
            echo "(" . implode(", ", $primaryKey) . ")";
        }
    }
}