All Products
Search
Document Center

Tablestore:Configure an auto-increment primary key column

Last Updated:Jul 23, 2024

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 an 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. Tablestore automatically generates values for the auto-increment primary key column. The values generated for the auto-increment primary key column are unique and increase monotonically within a partition.

Prerequisites

An OTSClient instance is initialized. For more information, see Initialize an OTSClient instance.

Configure an auto-increment primary key column

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

    Only a primary key column of the INTEGER type can be specified as an auto-increment primary key column. Each value generated for an auto-increment primary key column is a 64-bit signed integer of the LONG 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 specify the specific primary key column as an auto-increment primary key column.

    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 the ReturnType parameter to Primarykey in the PutRow, UpdateRow, or BatchWriteRow operation.

    When you read data from the data table, if the complete primary key is recorded, you can read the data based on the primary key. For more information, see Read a single row of data and Read multiple rows of data at a time. If the values of the auto-increment primary key column are not recorded, you must determine the range of data based on the values of the first primary key column and then read the data. For more information, see Read data whose primary key values are within a specific range.

    Note

    If you want to update an existing row but you do not record the value of the auto-increment primary key column, call the GetRange operation to obtain the primary key information of the row before you update 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 a primary key column to AUTO_INCREMENT.

    The following sample code provides an example on how to create a table that contains an auto-increment primary key column. The table contains the pk1 primary key column of the STRING type and the pk2 primary key column of the INTEGER type. The pk1 primary key column is the partition key and the pk2 primary key column is an auto-increment primary key column.

    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'// Set the primary key column to 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

    When 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. You need to only set the attribute of the 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));
        });
    
    }