All Products
Search
Document Center

Tablestore:Auto-increment primary key column

Last Updated:Nov 17, 2025

If you set a primary key column that is not a partition key as an auto-increment column, you do not need to specify a value for the column when you write data. Tablestore automatically generates a unique value for the auto-increment column that strictly increases at the partition key level.

Features

The auto-increment primary key column has the following features:

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

  • The data type of an auto-increment column is a 64-bit signed integer.

  • The auto-increment column is defined at the table level. An instance can contain tables with and without auto-increment columns.

Note

Using the auto-increment primary key column feature does not affect the rules for conditional updates. For more information, see Conditional updates.

Usage notes

  • Each data table can have a maximum of one auto-increment primary key column. A partition key column cannot be set as an auto-increment column.

  • You can specify an auto-increment column only when you create a data table. You cannot add an auto-increment column to an existing data table.

  • Only a primary key column of the integer type can be set as an auto-increment column. The system-generated value for an auto-increment column is a 64-bit signed integer.

  • An attribute column cannot be set as an auto-increment column.

  • The auto-increment primary key column feature and the local transaction feature cannot be used at the same time.

  • When you write data to a table with an auto-increment primary key column, you must retrieve and record the value of the auto-increment column for subsequent data updates or reads.

API operations

The following table describes the API operations related to the auto-increment primary key column feature.

API

Description

CreateTable

When you create a data table, set a primary key column that is not a partition key as an auto-increment column. Otherwise, you cannot use the auto-increment primary key column feature.

UpdateTable

After a data table is created, you cannot use the UpdateTable operation to set a primary key column as an auto-increment column.

PutRow

When you write data, you do not need to specify a value for the auto-increment column. Tablestore automatically generates a value for the column.

By setting ReturnType to RT_PK, you can get the complete primary key value. The complete primary key value can be used to query data with GetRow.

Important

To update an existing row, if you have not recorded the value of the auto-increment primary key column, first use the GetRange API operation to get the primary key information of the row to be updated. Then, perform the data update.

UpdateRow

BatchWriteRow

GetRow

When you use GetRow, you need the complete primary key value. You can get the complete primary key value by setting the ReturnType parameter in PutRow, UpdateRow, or BatchWriteRow to RT_PK.

Important

When you write data, if you do not record the value of the auto-increment primary key column, you can read a range of data. The range is determined by the first primary key column. For more information, see Read a range of data.

BatchGetRow

How to use

Use the console

  1. Create a data table with an auto-increment primary key column.

    1. Log on to the Tablestore console.

    2. On the Overview page, click Manage Instance in the Actions column of the instance.

    3. On the Instance Details tab, in the Tables section, click Create Table.

    4. In the Create Table dialog box, configure Table Name and Primary Key. Configure other parameters as needed.

      When you configure the table primary key, select Auto Increment as the type for a non-partition key primary key column.

      Note

      For more information about parameter configurations, see Data table operations.

    5. Click Create.

  2. Write data.

    1. On the Instance Details tab, in the Tables section, click the data table name.

    2. On the Query Data tab, click Insert.

    3. In the Insert dialog box, enter the primary key values and add attribute columns as needed.

      You do not need to enter a value for the auto-increment column. The system automatically generates a value when the data is written. To add multiple attribute columns, click Add Column multiple times and configure the name, type, and value for each attribute column.

    4. Click OK.

      The system displays the data that was successfully written. Record the complete primary key of the row for subsequent data updates or reads.

Use the command line interface

  1. Create and use a data table with an auto-increment primary key column.

    1. Run the create command to create a data table. For more information, see Create a table.

      The following example creates a data table named mytable with an auto-increment primary key column. This table has two primary key columns: uid (string type) and pid (integer type). The second primary key column, pid, is set as an auto-increment column. The data in the table never expires.

      create -t mytable --pk '[{"c":"uid", "t":"string"}, {"c":"pid", "t":"integer", "opt":"auto"}]'
    2. Run the use --wc -t mytable command to use the data table.

  2. Run the put command to write a row of data. For more information, see Insert new data.

    Note

    After you write the data, to retrieve the complete primary key information, you can run the scan command to retrieve the specified row data. For more information, see Scan data or Export data.

    The following example inserts a row of data into the data table. The value of the first primary key column is "86". The value of the second primary key column is null. The row has two attribute columns: name (string type) and country (string type).

    put --pk '["86", null]' --attr '[{"c":"name", "v":"redchen"}, {"c":"country", "v":"china"}]'

Use SDKs

You can use the auto-increment primary key column feature with the Java SDK, Go SDK, Python SDK, Node.js SDK, .NET SDK, and PHP SDK. This section uses the Java SDK as an example.

The following sample code provides an example on how to write a row of data to the test_table table, and obtain and print the primary key information of the row:

public static void putRowTest(SyncClient client) {
    // Construct the primary key.
    PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
    primaryKeyBuilder.addPrimaryKeyColumn("id", PrimaryKeyValue.fromString("row1"));
    // Set the value of the auto-increment primary key column to a placeholder.
    primaryKeyBuilder.addPrimaryKeyColumn("incr", PrimaryKeyValue.AUTO_INCREMENT);
    PrimaryKey primaryKey = primaryKeyBuilder.build();

    // Construct the row data to write.
    RowPutChange rowPutChange = new RowPutChange("test_table", primaryKey);
    rowPutChange.addColumn("col1", ColumnValue.fromString("val1"));
    // Set the return type to RT_PK to return the primary key information of the written row.
    rowPutChange.setReturnType(ReturnType.RT_PK);

    // Call the putRow method to write the row of data.
    PutRowRequest putRowRequest = new PutRowRequest();
    putRowRequest.setRowChange(rowPutChange);
    PutRowResponse putRowResponse = client.putRow(putRowRequest);

    // Display the request Id and read/write CU consumption.
    System.out.println("RequestId: " + putRowResponse.getRequestId());
    System.out.println("Read CU Cost: " + putRowResponse.getConsumedCapacity().getCapacityUnit().getReadCapacityUnit());
    System.out.println("Write CU Cost: " + putRowResponse.getConsumedCapacity().getCapacityUnit().getWriteCapacityUnit());

    // Obtain and print the returned primary key information. If the return type is not set to RT_PK, the primary key information is not returned.
    Row row = putRowResponse.getRow();
    if(row != null)
        System.out.println(row.getPrimaryKey().toString());
}

Billing

Using the auto-increment primary key column feature does not affect existing billing rules. The primary key column data that is returned does not consume extra read capacity units (CUs).