UPDATE

Function

UPDATE updates data in a table. UPDATE changes the values of the specified columns in all rows that satisfy the condition. The WHERE clause clarifies conditions. The columns to be modified need be mentioned in the SET clause; columns not explicitly modified retain their previous values.

Precautions

  • Avoid using UPDATE or DELETE to modify or delete a large volume of data and use TRUNCATE PARTITION or DROP PARTITION instead.
  • Do not concurrently update the same column-store table using the UPDATE and UPSERT statements.
  • For more information about development and design specifications, see "GaussDB(DWS) Development and Design Proposal" in the Data Warehouse Service (DWS) Developer Guide.

Syntax

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
    UPDATE [/*+ plan_hint */] [ ONLY ] table_name [ * ] [ [ AS ] alias ]
    [ PARTITION ( partition_name ) | PARTITION FOR ( partition_key_value [, ...] ) ]
SET {column_name = { expression | DEFAULT } 
    |( column_name [, ...] ) = {( { expression | DEFAULT } [, ...] ) |sub_query }}[, ...]
    [ FROM from_list] [ WHERE condition ]
    [ RETURNING {* 
                | {output_expression [ [ AS ] output_name ]} [, ...] }];

where sub_query can be:
SELECT [ ALL | DISTINCT [ ON ( expression [, ...] ) ] ]
{ * | {expression [ [ AS ] output_name ]} [, ...] }
[ FROM from_item [, ...] ]
[ WHERE condition ]
[ GROUP BY grouping_element [, ...] ]
[ HAVING condition [, ...] ]

Parameter Description

Examples

Update the values of all records.

1
UPDATE reason SET r_reason_sk = r_reason_sk * 2;

If the WHERE clause is not included, all r_reason_sk values are updated.

1
UPDATE reason SET r_reason_sk = r_reason_sk + 100;

Redefine r_reason_sk whose r_reason_desc is reason2 in the reason table.

1
UPDATE reason SET r_reason_sk = 5 WHERE r_reason_desc = 'reason2';

Redefine r_reason_sk whose value is 2 in the reason table.

1
UPDATE reason SET r_reason_sk = r_reason_sk + 100 WHERE r_reason_sk = 2;

Redefine the course IDs whose r_reason_sk is greater than 2 in the reason table.

1
UPDATE reason SET r_reason_sk = 201 WHERE r_reason_sk > 2;

You can run an UPDATE statement to update multiple columns by specifying multiple values in the SET clause. For example:

1
UPDATE reason SET r_reason_sk = 5, r_reason_desc = 'reason5' WHERE r_reason_id = 'fourth'; 

Update partition p1 in the partitioned table test_range_row:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
CREATE TABLE test_range_row(a int, d int)
DISTRIBUTE BY hash(a) PARTITION BY RANGE(d)
(
    PARTITION p1 values LESS THAN (60),
    PARTITION p2 values LESS THAN (75),
    PARTITION p3 values LESS THAN (90),
    PARTITION p4 VALUES LESS THAN (maxvalue)
);
INSERT OVERWRITE INTO test_range_row PARTITION(p1) VALUES(55,51);
INSERT OVERWRITE INTO test_range_row PARTITION(p3) VALUES(85,80);

UPDATE test_range_row PARTITION(p1) SET d = 41;