Updated on 2024-08-20 GMT+08:00

Updating Data in a Table

Existing data in a database can be updated. You can update one row, all rows, or specified rows of data, or update data in a single column without affecting the data in the other columns.

The following information is required when the UPDATE statement is used to update rows:

  • Table name and column name of the data to be updated
  • New column value
  • Rows to be updated

Generally, in SQL, no unique ID is specified for a row of data. Therefore, it is impossible to directly specify the rows of the data to be updated. However, you can specify the conditions that must be met by the rows to be updated. If a table contains primary keys, you can specify a row using the primary keys.

For details about how to create a table and insert data to it, see Creating a Table and Inserting Data to Tables.

The value of c_customer_sk in the table customer_t1 must be changed from 9527 to 9876.

1
gaussdb=# UPDATE customer_t1 SET c_customer_sk = 9876 WHERE c_customer_sk = 9527;

You can use a schema to modify the table name. If no such modifier is specified, the table is located based on the default schema path. SET is followed by the column and the new column value. The new value can be a constant or an expression.

For example, increase all the values in the c_customer_sk column by 100.

1
gaussdb=# UPDATE customer_t1 SET c_customer_sk = c_customer_sk + 100;

This statement does not contain the WHERE clause, and therefore all rows are updated. If the statement contains the WHERE clause, only the rows matching the clause are updated.

In the SET clause, the equal sign (=) indicates value setting. In the WHERE clause, the equal sign indicates comparison. A WHERE condition does not have to be an equality comparison, and can be another operator.

You can use an UPDATE statement to update multiple columns by specifying multiple values in the SET clause, for example:

1
gaussdb=# UPDATE customer_t1 SET  c_customer_id = 'Admin', c_first_name = 'Local' WHERE c_customer_sk = 4421; 

After data has been updated or deleted in batches, a large number of deletion markers are generated in the data file. During query, data with these deletion markers needs to be scanned as well. In this case, a large amount of data with deletion marks may greatly affect the query performance after batch updates or deletions. If data needs to be updated or deleted in batches frequently, you are advised to periodically run VACUUM FULL so as to ensure the query performance.