全部產品
Search
文件中心

Tablestore:更新單行資料

更新時間:Aug 13, 2026

Go SDK 可新增、修改或刪除寬表模型資料表中一行資料的屬性列。

前提條件

安裝Tablestore Go SDK並初始化用戶端。

功能說明

調用 UpdateRow 更新指定主鍵行的屬性列。可以新增或覆蓋屬性列的資料版本,也可以刪除指定版本或整列資料;可通過 Condition 控制更新是否執行。

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

以下樣本更新 example_table 中主索引值為 row1 的行:修改 status 屬性列,並刪除 temporary 屬性列。

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)
}

參數說明

UpdateRowRequest 包含以下參數。

名稱

類型

說明

UpdateRowChange(必選)

*UpdateRowChange

單行更新配置。

行變更

UpdateRowChange 包含以下參數。

名稱

類型

說明

TableName(必選)

string

資料表名稱。

PrimaryKey(必選)

*PrimaryKey

行主鍵。主鍵列的名稱、順序和類型必須與資料表的主鍵結構一致。

Columns(必選)

[]ColumnToUpdate

屬性列更新操作。可通過 PutColumnPutColumnWithTimestampDeleteColumnDeleteColumnWithTimestamp 添加操作。

Condition(必選)

*RowCondition

更新條件。可設定行存在性條件和列條件。更多資訊,請參見條件更新

TransactionId(可選)

*string

局部事務 ID。僅在局部事務內更新資料時設定。更多資訊,請參見局部事務

ReturnType(可選)

ReturnType

原子計數器傳回型別。調用 SetReturnIncrementValue 後設定為 ReturnType_RT_AFTER_MODIFY

ColumnNamesToReturn(可選)

[]string

原子計數操作後要返回新值的屬性列名稱。

情境樣本

指定資料版本號碼

調用 PutColumnWithTimestamp 寫入指定版本的屬性列,或調用 DeleteColumnWithTimestamp 刪除指定版本。

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