All Products
Search
Document Center

Tablestore:Configure an auto-increment primary key column

Last Updated:May 31, 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 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 Initialize an OTSClient instance.

Procedure

  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 the ReturnType parameter to RT_PK.

    When you read data from the table, you must specify the values of all primary key columns. To obtain the values of all primary key columns, you can set the ReturnType parameter to RT_PK when you call 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 values of the auto-increment primary key column, call the GetRange operation to obtain the primary key information about the row before you update.

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, the pk2 primary key column of the Integer type, and the pk3 primary key column of the Binary type. The pk1 primary key column is the partition key and the pk2 primary key column is an auto-increment primary key column.

    import (
        "fmt"
        "github.com/aliyun/aliyun-tablestore-go-sdk/tablestore"
    )
    
    func CreateTableKeyAutoIncrementSample(client *tablestore.TableStoreClient) {
        createtableRequest := new(tablestore.CreateTableRequest)
        tableMeta := new(tablestore.TableMeta)
        // Specify the name of the data table. 
        tableMeta.TableName = "incrementsampletable"
        // Construct the primary key information about the data table. The primary key columns of the data table are pk1 of the String type, pk2 of the Integer type, and pk3 of the Binary type. The pk2 primary key column is an auto-increment primary key column. 
        tableMeta.AddPrimaryKeyColumn("pk1", tablestore.PrimaryKeyType_STRING)
        tableMeta.AddPrimaryKeyColumnOption("pk2", tablestore.PrimaryKeyType_INTEGER, tablestore.AUTO_INCREMENT)
        tableMeta.AddPrimaryKeyColumn("pk3", tablestore.PrimaryKeyType_BINARY)    
        tableOption := new(tablestore.TableOption)
        tableOption.TimeToAlive = -1
        tableOption.MaxVersion = 3
        reservedThroughput := new(tablestore.ReservedThroughput)
        reservedThroughput.Readcap = 0
        reservedThroughput.Writecap = 0
        createtableRequest.TableMeta = tableMeta
        createtableRequest.TableOption = tableOption
        createtableRequest.ReservedThroughput = reservedThroughput
        _, err := client.CreateTable(createtableRequest)
        if (err != nil) {
            fmt.Println("Failed to create table with error:", err)
        } else {
            fmt.Println("Create table finished")
        }
    }
  2. Write data

    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.

    import (
        "fmt"
        "github.com/aliyun/aliyun-tablestore-go-sdk/tablestore"
    )
    
    func PutRowWithKeyAutoIncrementSample(client *tablestore.TableStoreClient) {
        fmt.Println("begin to put row")
        putRowRequest := new(tablestore.PutRowRequest)
        putRowChange := new(tablestore.PutRowChange)
        putRowChange.TableName = "incrementsampletable"
        // Add primary key columns in the sequence in which primary key columns are added when the table was created and specify pk2 as the auto-increment primary key column. 
        putPk := new(tablestore.PrimaryKey)
        putPk.AddPrimaryKeyColumn("pk1", "pk1value1")
        putPk.AddPrimaryKeyColumnWithAutoIncrement("pk2")
        putPk.AddPrimaryKeyColumn("pk3", []byte("pk3"))
        putRowChange.PrimaryKey = putPk
        putRowChange.AddColumn("col1", "col1data1")
        putRowChange.AddColumn("col2", int64(100))
        putRowChange.SetCondition(tablestore.RowExistenceExpectation_IGNORE)
        putRowRequest.PutRowChange = putRowChange
        _, err := client.PutRow(putRowRequest)
    
        if err != nil {
            fmt.Println("put row failed with error:", err)
        } else {
            fmt.Println("put row finished")
        }
    }