Update a row

Updated at:

Use the Tablestore SDK for Go to add, modify, or delete attribute columns in a row of a Wide Column model table.

Prerequisites

Install the Tablestore SDK for Go and initialize the client.

Description

Call UpdateRow to update attribute columns in a row identified by its primary key. You can add or overwrite data versions and delete a specific version or an entire attribute column. You can use Condition to control whether the update is performed.

func (tableStoreClient *TableStoreClient) UpdateRow(request *UpdateRowRequest) (*UpdateRowResponse, error)

The following sample updates the row whose primary key value is row1 in the example_table table. The sample modifies the status attribute column and deletes the temporary attribute column.

primaryKey := &tablestore.PrimaryKey{}
primaryKey.AddPrimaryKeyColumn("id", "row1")

change := &tablestore.UpdateRowChange{
    TableName:  "example_table",
    PrimaryKey: primaryKey,
}
change.PutColumn("status", "active")
change.DeleteColumn("temporary")
change.SetCondition(tablestore.RowExistenceExpectation_EXPECT_EXIST)

_, err := client.UpdateRow(&tablestore.UpdateRowRequest{UpdateRowChange: change})
if err != nil {
    log.Fatal(err)
}

Parameters

UpdateRowRequest contains the following parameter.

Name

Type

Description

UpdateRowChange (required)

*UpdateRowChange

The row update configuration.

Row change

UpdateRowChange contains the following parameters.

Name

Type

Description

TableName (required)

string

The table name.

PrimaryKey (required)

*PrimaryKey

The primary key of the row. The names, order, and types of the primary key columns must match the table schema.

Columns (required)

[]ColumnToUpdate

The attribute column operations. Add operations by calling PutColumn, PutColumnWithTimestamp, DeleteColumn, or DeleteColumnWithTimestamp.

Condition (required)

*RowCondition

The update condition, which can include a row existence condition and a column condition. For more information, see Use conditional updates.

TransactionId (optional)

*string

The local transaction ID. Specify this parameter only for an update in a local transaction. For more information, see Use local transactions.

ReturnType (optional)

ReturnType

The return type for an atomic counter. Calling SetReturnIncrementValue sets this parameter to ReturnType_RT_AFTER_MODIFY.

ColumnNamesToReturn (optional)

[]string

The attribute columns whose new values are returned after atomic counter operations.

Examples

Specify data versions

Call PutColumnWithTimestamp to write a specific version of an attribute column or DeleteColumnWithTimestamp to delete a specific version.

timestamp := time.Now().UnixMilli()
change.PutColumnWithTimestamp("score", int64(95), timestamp)
change.DeleteColumnWithTimestamp("legacy_status", timestamp)