全部產品
Search
文件中心

Tablestore:更新單行資料

更新時間:Apr 01, 2026

本文介紹如何通過 Go SDK 更新Table Store資料表中的單行資料,您可以更新屬性列的值、添加屬性列、刪除屬性列的某個版本或整個屬性列。

前提條件

初始化Tablestore Client

方法說明

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

UpdateRowRequest參數說明

  • UpdateRowChange(必選)*UpdateRowChange:更新的行資料資訊,包含以下參數。

    名稱

    類型

    說明

    TableName(必選)

    string

    資料表名稱。

    PrimaryKey(必選)

    *PrimaryKey

    主鍵資訊,包括主鍵列名稱和主索引值。

    • 主鍵列資料類型包括 STRING、INTEGER 和 BINARY。

    • 主鍵個數和類型必須與資料表的主鍵保持一致。

    Columns(必選)

    []ColumnToUpdate

    更新的屬性列資訊和操作類型。

    Condition(必選)

    *RowCondition

    更新條件,詳情請參見條件更新

    TransactionId(可選)

    *string

    局部事務ID,用於唯一標識局部事務,詳情請參見局部事務

範例程式碼

以下範例程式碼用於修改 test_table 表中主索引值為 row1 的行資料,將屬性列 col1 的值修改為 changed_val1。

func UpdateRowSample(client *tablestore.TableStoreClient) {
    // 構造主鍵
    updatePk := new(tablestore.PrimaryKey)
    updatePk.AddPrimaryKeyColumn("id", "row1")

    // 構造更新行資料
    updateRowChange := new(tablestore.UpdateRowChange)
    updateRowChange.TableName = "test_table"
    updateRowChange.PrimaryKey = updatePk
    updateRowChange.PutColumn("col1", "changed_val1")
    // 更新行資料時必須指定更新條件 (tablestore.RowExistenceExpectation_IGNORE,表示不做行存在性判斷)
    updateRowChange.SetCondition(tablestore.RowExistenceExpectation_IGNORE)

    // 調用 UpdateRow 方法更新行資料
    updateRowRequest := new(tablestore.UpdateRowRequest)
    updateRowRequest.UpdateRowChange = updateRowChange
    response, err := client.UpdateRow(updateRowRequest)
    if err != nil {
        fmt.Println("Update row failed with error: ", err)
    } else {
        fmt.Printf("RequestId: %s \n", response.RequestId)
        fmt.Printf("Read CU Cost: %d \n", response.ConsumedCapacityUnit.Read)
        fmt.Printf("Write CU Cost: %d \n", response.ConsumedCapacityUnit.Write)
    }
}

您也可以參照範例程式碼進行以下行資料操作。

  • 添加一個屬性列。

    updateRowChange.PutColumn("col2", "val2")
  • 設定屬性列資料版本號碼。

    updateRowChange.PutColumnWithTimestamp("col2", "val2", int64(time.Now().Unix() * 1000))
    updateRowChange.PutColumnWithTimestamp("col2", int64(1), int64(1758249013000))
  • 刪除屬性列指定版本的資料。

    updateRowChange.DeleteColumnWithTimestamp("col2", 1747893563831)
  • 刪除整個屬性列資料。

    updateRowChange.DeleteColumn("col2")

相關文檔