This topic describes how to configure an auto-increment primary key column. You can specify a primary key column that is not the partition key as the auto-increment primary key column. If you write data to a table that contains an auto-increment primary key column, you do not need to specify values for the auto-increment primary key column because Tablestore automatically generates values for the auto-increment primary key column. Values generated for the auto-increment primary key column are unique and increase monotonically within a partition that shares the same partition key value.

Prerequisites

An OTSClient instance is initialized. For more information, see Initialization.

Configure an auto-increment primary key column

  1. When you create a table, you can specify a primary key column that is not the partition key as the auto-increment primary key column.

    You can specify a primary key column only of the INTEGER type as the auto-increment primary key column. Each value generated for an auto-increment primary key column is a 64-bit signed integer of the LONG data type.

  2. When you write data to a table, you do not need to specify values for the auto-increment primary key column. You need to only set the values of the auto-increment primary key column to placeholders.

    If you want to obtain the values of the auto-increment primary key column after data is written to the table, you can set ReturnType to Primarykey.

    When you query data, you must specify the values of all primary key columns. To obtain a complete primary key value, you can set ReturnType to Primarykey in PutRow, UpdateRow, or BatchWriteRow.

    Note If you want to update an existing row, call the GetRange operation to obtain the primary key information about the row before you update the data.

Examples

You can use the auto-increment primary key column feature when you call the CreateTable, PutRow, UpdateRow, or BatchWriteRow operation.

  1. Create a table

    To create an auto-increment primary key column when you create a table, you need to only set the attribute of the primary key column to AUTO_INCREMENT.

    var TableStore = require('../index.js');
    var Long = TableStore.Long;
    var client = require('./client');
    
    var tableName = "autoIncTable";
    var pk1 = "stringPK";
    var pk2 = "autoIncPK";
    
    function createTableWithAutoIncrementPk() {
        var createParams = {
            tableMeta: {
                tableName: tableName,
                primaryKey: [
                    {
                        name: pk1,
                        type: 'STRING'
                    },
                    {
                        name: pk2,
                        type: 'INTEGER',
                        option: 'AUTO_INCREMENT'// Specify the primary key column as the auto-increment primary key column by setting option to AUTO_INCREMENT. 
                    },
                ]
            },
            reservedThroughput: {
                capacityUnit: {
                    read: 0,
                    write: 0
                }
            },
            tableOptions: {
                timeToLive: -1,// Specify the validity period of data in seconds. A value of -1 indicates that the data never expires. If you set this parameter to 31,536,000 (365 × 24 x 3,600), the validity period of the data is one year. 
                maxVersions: 1// Specify the maximum number of versions that can be retained in each column. A value of 1 indicates that only the latest version is retained in each column. 
            },
        };
    
        client.createTable(createParams, function (err, data) {
            if (err) {
                console.log('error:', err);
                return;
            }
            console.log('create table success');
        });
    }
  2. Write data to a table

    When you write data to a table, you do not need to specify values for the auto-increment primary key column. You need to only set the values of the auto-increment primary key column to PK_AUTO_INCR.

    var TableStore = require('../index.js');
    var Long = TableStore.Long;
    var client = require('./client');
    
    var tableName = "autoIncTable";
    var pk1 = "stringPK";
    var pk2 = "autoIncPK";
    
    function putRow() {
        var putParams = {
            tableName: tableName,
            condition: new TableStore.Condition(TableStore.RowExistenceExpectation.IGNORE, null),
            primaryKey: [
                { stringPK: 'pk1' },
                { autoIncPK: TableStore.PK_AUTO_INCR }
            ],
            attributeColumns: [
                { 'col1': 'col1val' }
            ],
            returnContent: { returnType: TableStore.ReturnType.Primarykey }
        };
    
        client.putRow(putParams, function (err, data) {
            if (err) {
                console.log('error:', err);
                return;
            }
    
            console.log('put row success,autoIncrement pk value:' + JSON.stringify(data.row.primaryKey));
        });
    
    }