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.

  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 the 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 RT_PK.

    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 RT_PK 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.

    import (
        "fmt"
        "github.com/aliyun/aliyun-tablestore-go-sdk/tablestore"
    )
    
    func CreateTableKeyAutoIncrementSample(client *tablestore.TableStoreClient) {
        createtableRequest := new(tablestore.CreateTableRequest)
        // Create a table that contains three primary key columns: pk1 of the STRING type, pk2 of the INTEGER type, and pk3 of the BINARY type. pk2 is the auto-increment primary key column. 
        tableMeta := new(tablestore.TableMeta)
        tableMeta.TableName = "incrementsampletable"
        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
    
        client.CreateTable(createtableRequest)
    }
  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 specify the specific primary key column as the 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 were 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")
        }
    }