All Products
Search
Document Center

Tablestore:Configure an auto-increment primary key column

Last Updated:Jun 24, 2025

This topic describes how to configure an auto-increment primary key column for a data table by using Tablestore SDK for Node.js. It also explains how to write data to the auto-increment primary key column and obtain the values generated for the auto-increment primary key column.

Usage notes

The values of an auto-increment primary key column are unique and increase monotonically but not always continuously within a partition that shares the same partition key value.

Prerequisites

A client is initialized. For more information, see Initialize a Tablestore client.

Configure an auto-increment primary key column

You can specify a primary key column that is not the partition key as an auto-increment primary key column when you create a data table. For existing data tables, you cannot configure an auto-increment primary key column.

Note

You can specify a primary key column that is not the partition key as an auto-increment primary key column only when the data type of the primary key column is Integer. A data table can have at most one auto-increment primary key column. The values generated for the auto-increment primary key column are 64-bit signed long integers.

Sample code

The following sample code provides an example on how to create a data table named test_table. The primary key of this table includes the partition key id and the auto-increment primary key column incr.

function createTableSample() {
    var createParams = {
        tableMeta: {
            tableName: 'test_table',
            primaryKey: [
                {
                    name: 'id',
                    type: 'STRING'
                },
                {
                    name: 'incr',
                    type: 'INTEGER',
                    option: 'AUTO_INCREMENT'
                },
            ]
        },
        tableOptions: {
            timeToLive: -1,
            maxVersions: 1
        },
        reservedThroughput: {
            capacityUnit: {
                read: 0,
                write: 0
            }
        },
    };

    client.createTable(createParams, function (err, data) {
        if (err) {
            console.error('error:', err);
            return;
        }
        console.log('success:', data);
    });
}

Write data

When you write data to an auto-increment primary key column, you need to only set the value of the auto-increment primary key column to a placeholder. If you want to obtain the generated value of the auto-increment primary key column for data queries and updates, you also need to set the returnType parameter to Primarykey.

Sample code

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 written row data.

function putRowSample() {
    var putParams = {
        tableName: 'test_table',
        condition: new TableStore.Condition(TableStore.RowExistenceExpectation.IGNORE, null),
        primaryKey: [
            { id: 'row1' },
            // Set the auto-increment primary key column to a placeholder.
            { incr: TableStore.PK_AUTO_INCR }
        ],
        attributeColumns: [
            { 'col1': 'val1' }
        ],
        // Set the return type to Primarykey to return the primary key information of the written row data.
        returnContent: { returnType: TableStore.ReturnType.Primarykey }
    };

    client.putRow(putParams, function (err, data) {
        if (err) {
            console.error('error:', err);
            return;
        }

        // RequestId and consumed CUs
        console.log("RequestId: ", data.RequestId);
        console.log("Read CU Cost: ", data.consumed.capacityUnit.read);
        console.log("Write CU Cost: ", data.consumed.capacityUnit.write);

        // Obtain and print the returned primary key information. If the return type is not set to Primarykey, the primary key information is not returned by default.
        if (data.row.primaryKey) {
            console.log(JSON.stringify(data.row.primaryKey));
        }
    });
}