edit-icon download-icon

Update data

Last Updated: Mar 01, 2018

SQL

In a relational database, you can use the UPDATE statement to modify one or more rows in a table as follows.

  1. UPDATE UserHistory
  2. SET behavior_type = 'collect'
  3. WHERE user_id = '10100' AND time_stamp = 1479265526 AND behavior_count > 4.0;

Table Store

In Table Store, you can use the UpdateRow API to update one row of data as follows.

  1. // Set primary keys
  2. PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
  3. primaryKeyBuilder.addPrimaryKeyColumn("user_id", PrimaryKeyValue.fromString("10100"));
  4. primaryKeyBuilder.addPrimaryKeyColumn("time_stamp", PrimaryKeyValue.fromLong(1479265526));
  5. PrimaryKey primaryKey = primaryKeyBuilder.build();
  6. // Set the update conditions. In this example, the row already exists and only updates if the behavior_count value is greater than 4.0
  7. Condition condition = new Condition(RowExistenceExpectation.EXPECT_EXIST);
  8. condition.setColumnCondition(new SingleColumnValueCondition("behavior_count", SingleColumnValueCondition.CompareOperator.GREATER_THAN, ColumnValue.fromDouble(4.0)));
  9. // Set the attribute column value
  10. RowUpdateChange rowUpdateChange = new RowUpdateChange(TABLE_NAME, primaryKey);
  11. rowUpdateChange.put(new Column("behavior_type", ColumnValue.fromString("collect")));
  12. rowUpdateChange.setCondition(condition);
  13. // Insert this row of data
  14. client.updateRow(new UpdateRowRequest(rowUpdateChange));

Note:

  • When using UpdateRow, you must specify the table name TABLE_NAME and all primary keys primaryKey. The column to be updated may or may not already exist.
  • UpdateRow only modifies the specified columns in a single row. PutRow can be used to overwrite all data in an existing row.
  • For the update operation, you can set two types of check conditions: Row existence check and Condition check.
  • In SQL, the UPDATE statement updates all records that satisfy the WHERE condition. Table Store’s UpdateRow API only updates the one row of data with the specified primary key. The condition check function only checks the primary key and attribute columns of this row.
  • You can use the BatchWriteRow API to update multiple records, making your data writing operations faster.
Thank you! We've received your feedback.