UPDATE

Name

UPDATE

Description

This statement is used to update the data. The UPDATE statement currently only supports the UNIQUE KEY model.

The UPDATE operation currently only supports updating the Value column. The update of the Key column can refer to Using FlinkCDC to update Key column.

Syntax

  1. UPDATE target_table [table_alias]
  2. SET assignment_list
  3. WHERE condition
  4. assignment_list:
  5. assignment [, assignment] ...
  6. assignment:
  7. col_name = value
  8. value:
  9. {expr | DEFAULT}

SinceVersion dev

  1. UPDATE target_table [table_alias]
  2. SET assignment_list
  3. [ FROM additional_tables]
  4. WHERE condition

Required Parameters

  • target_table: The target table of the data to be updated. Can be of the form ‘db_name.table_name’
  • assignment_list: The target column to be updated, in the format ‘col_name = value, col_name = value’
  • where condition: the condition that is expected to be updated, an expression that returns true or false can be

Optional Parameters

SinceVersion dev

  • table_alias: alias of table
  • FROM additional_tables: Specifies one or more tables to use for selecting rows to update or for setting new values. Note that if you want use target table here, you should give it a alias explicitly.

Note

The current UPDATE statement only supports row updates on the UNIQUE KEY model.

Example

The test table is a unique model table, which contains four columns: k1, k2, v1, v2. Where k1, k2 are keys, v1, v2 are values, and the aggregation method is Replace.

  1. Update the v1 column in the ‘test’ table that satisfies the conditions k1 =1 , k2 =2 to 1
  1. UPDATE test SET v1 = 1 WHERE k1=1 and k2=2;
  1. Increment the v1 column of the k1=1 column in the ‘test’ table by 1
  1. UPDATE test SET v1 = v1+1 WHERE k1=1;

SinceVersion dev

  1. use the result of t2 join t3 to update t1
  1. -- create t1, t2, t3 tables
  2. CREATE TABLE t1
  3. (id INT, c1 BIGINT, c2 STRING, c3 DOUBLE, c4 DATE)
  4. UNIQUE KEY (id)
  5. DISTRIBUTED BY HASH (id)
  6. PROPERTIES('replication_num'='1', "function_column.sequence_col" = "c4");
  7. CREATE TABLE t2
  8. (id INT, c1 BIGINT, c2 STRING, c3 DOUBLE, c4 DATE)
  9. DISTRIBUTED BY HASH (id)
  10. PROPERTIES('replication_num'='1');
  11. CREATE TABLE t3
  12. (id INT)
  13. DISTRIBUTED BY HASH (id)
  14. PROPERTIES('replication_num'='1');
  15. -- insert data
  16. INSERT INTO t1 VALUES
  17. (1, 1, '1', 1.0, '2000-01-01'),
  18. (2, 2, '2', 2.0, '2000-01-02'),
  19. (3, 3, '3', 3.0, '2000-01-03');
  20. INSERT INTO t2 VALUES
  21. (1, 10, '10', 10.0, '2000-01-10'),
  22. (2, 20, '20', 20.0, '2000-01-20'),
  23. (3, 30, '30', 30.0, '2000-01-30'),
  24. (4, 4, '4', 4.0, '2000-01-04'),
  25. (5, 5, '5', 5.0, '2000-01-05');
  26. INSERT INTO t3 VALUES
  27. (1),
  28. (4),
  29. (5);
  30. -- update t1
  31. UPDATE t1
  32. SET t1.c1 = t2.c1, t1.c3 = t2.c3 * 100
  33. FROM t2 INNER JOIN t3 ON t2.id = t3.id
  34. WHERE t1.id = t2.id;

the expect result is only update the row where id = 1 in table t1

  1. +----+----+----+--------+------------+
  2. | id | c1 | c2 | c3 | c4 |
  3. +----+----+----+--------+------------+
  4. | 1 | 10 | 1 | 1000.0 | 2000-01-01 |
  5. | 2 | 2 | 2 | 2.0 | 2000-01-02 |
  6. | 3 | 3 | 3 | 3.0 | 2000-01-03 |
  7. +----+----+----+--------+------------+

Keywords

  1. UPDATE

Best Practice