UPDATE
Description
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 to be mentioned in the SET clause; columns not explicitly modified retain their previous values.
Precautions
- The owner of a table, users granted with the UPDATE permission on the table, or users granted with the UPDATE ANY TABLE permission can update data in the table. System administrators have the permission to update data in the table by default.
- You must have the SELECT permission on all tables involved in the expressions or conditions.
- The generated column cannot be directly written. In the UPDATE statement, values cannot be specified for generated columns, but the keyword DEFAULT can be specified.
Syntax
[ WITH [ RECURSIVE ] with_query [, ...] ] UPDATE [/*+ plan_hint */] [ ONLY ] table_name [ partition_clause ] [ * ] [ [ AS ] alias ] 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 [, ...] ] [ ORDER BY {expression [ [ ASC | DESC | USING operator ] | nlssort_expression_clause ] [ NULLS { FIRST | LAST } ]} [, ...] ] [ LIMIT { [offset,] count | ALL } ]
with_query_name [ ( column_name [, ...] ) ] AS [ [ NOT ] MATERIALIZED ] ( {select | values | insert | update | delete} )
Parameters
- WITH [ RECURSIVE ] with_query [, ...]
Specifies one or more subqueries that can be referenced by name in the main query, which is equivalent to a temporary table. This subquery statement structure is called the common table expression (CTE) structure. When this structure is used, the execution plan contains the CTE SCAN content.
If RECURSIVE is specified, it allows a SELECT subquery to reference itself by name.
Format of with_query:
with_query_name [ ( column_name [, ...] ) ] AS [ [ NOT ] MATERIALIZED ] ( {select | values | insert | update | delete} )
- with_query_name specifies the name of the result set generated by a subquery. Such names can be used to access the result sets of subqueries in a query.
- column_name specifies the column name displayed in the subquery result set.
- Each subquery can be a SELECT, VALUES, INSERT, UPDATE, or DELETE statement.
- You can use MATERIALIZED or NOT MATERIALIZED to modify the CTE.
- If MATERIALIZED is specified, the WITH query will be materialized, and a copy of the subquery result set is generated. The copy is directly queried at the reference point. Therefore, the WITH subquery cannot be jointly optimized with the SELECT statement trunk (for example, predicate pushdown and equivalence class transfer). In this scenario, you can use NOT MATERIALIZED for modification. If the WITH query can be executed as a subquery inline, the preceding optimization can be performed.
- If the user does not explicitly declare the materialized attribute, comply with the following rules: If the CTE is referenced only once in the SELECT statement trunk to which it belongs and semantically supports inline execution, it will be rewritten as subquery inline execution. Otherwise, the materialized execution will be performed in CTE Scan mode.
- plan_hint clause
Follows the UPDATE keyword in the /*+ */ format. It is used to optimize the plan of an UPDATE statement block. For details, see Hint-based Tuning. In each statement, only the first /*+ plan_hint */ comment block takes effect as a hint. Multiple hints can be written.
- table_name
Specifies the name (optionally schema-qualified) of the table to be updated.
Value range: an existing table name.
- partition_clause
Updates a specified partition.
PARTITION { ( partition_name ) | FOR ( partition_value [, ...] ) } |
SUBPARTITION { ( subpartition_name ) | FOR ( subpartition_value [, ...] ) }
For details about the keywords, see SELECT.
For details, see CREATE TABLE SUBPARTITION.
- alias
Specifies the alias of the target table.
Value range: a string that complies with the Identifier Naming Conventions.
- column_name
Specifies the name of the column to be modified.
You can refer to this column by specifying the target table alias and the column name. For example:
UPDATE foo AS f SET f.col_name = 'namecol';
Value range: name of an existing column.
- expression
Specifies a value assigned to a column or an expression that assigns the value.
- DEFAULT
Specifies the default value of a column.
The value is NULL if no default value has been assigned to it.
- sub_query
Specifies a subquery.
This statement can be executed to update a table with information for other tables in the same database. For details about clauses in the SELECT statement, see SELECT.
When a single column is updated, the ORDER BY and LIMIT clauses can be used. When multiple columns are updated, the ORDER BY and LIMIT clauses cannot be used.
- from_list
Specifies a list of table expressions. You can use columns of other tables in the WHERE condition. It is similar to specifying a table list in a FROM clause of a SELECT statement.
Note that the target table cannot appear in the from_list, unless you intend a self-join (in which case it must appear with an alias in the from_list).
- condition
Specifies an expression that returns a value of type Boolean. Only rows for which this expression returns true are updated. You are advised not to use numeric types such as int for condition, because such types can be implicitly converted to bool values (non-zero values are implicitly converted to true and 0 is implicitly converted to false), which may cause unexpected results.
- output_expression
Specifies an expression to be computed and returned by the UPDATE command after each row is updated.
Value range: any table and table columns listed in FROM. Write * to return all columns.
- output_name
Specifies a name to use for a returned column.
Examples
-- Create the student1 table. openGauss=# CREATE TABLE student1 ( stuno int, classno int ); -- Insert data. openGauss=# INSERT INTO student1 VALUES(1,1); openGauss=# INSERT INTO student1 VALUES(2,2); openGauss=# INSERT INTO student1 VALUES(3,3); -- View data. openGauss=# SELECT * FROM student1; -- Update the values of all records. openGauss=# UPDATE student1 SET classno = classno*2; -- View data. openGauss=# SELECT * FROM student1; -- Drop the table. openGauss=# DROP TABLE student1;
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot