English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PostgreSQL UPDATE Statement

If we want to update the data in the PostgreSQL database, we can use UPDATE to operate.

Syntax

The following is the general SQL syntax for modifying data with the UPDATE statement:

UPDATE table_name
SET column1 = value1, column2 = value2...., columnN = valueN
WHERE [condition];
  • We can update one or more fields at the same time.

  • We can specify any condition in the WHERE clause.

Online example

Create COMPANY table (Download COMPANY SQL file ),Data content is as follows:

w3codeboxdb# select * from COMPANY;
 id | name  | age | address   | salary
----+-------+-----+-----------+--------
  1 | Paul  |  32 | California|  20000
  2 | Allen |  25 | Texas     |  15000
  3 | Teddy |  23 | Norway    |  20000
  4 | Mark  |  25 | Rich-Mond |  65000
  5 | David |  27 | Texas     |  85000
  6 | Kim   |  22 | South-Hall|  45000
  7 | James |  24 | Houston   |  10000
(7 rows)

The following example will update the COMPANY table where the id is 3 The salary field value:

w3codeboxdb=# UPDATE COMPANY SET SALARY= 15000 WHERE ID = 3;

The result is as follows:

id | name  | age | address    | salary
----+-------+-----+------------+--------
  1 | Paul  |  32 | California |  20000
  2 | Allen |  25 | Texas      |  15000
  4 | Mark  |  25 | Rich-Mond  |  65000
  5 | David |  27 | Texas      |  85000
  6 | Kim   |  22 | South-Hall |  45000
  7 | James |  24 | Houston    |  10000
  3 | Teddy |  23 | Norway     |  15000

From the results, the id in the COMPANY table is 3 The salary field value has been modified.

The following example will update the values of both salary and address fields at the same time:

w3codeboxdb=# UPDATE COMPANY SET ADDRESS = 'Texas', SALARY=20000;

The result is as follows:

id | name  | age | address | salary
----+-------+-----+---------+--------
  1 | Paul  |  32 | Texas   |  20000
  2 | Allen |  25 | Texas   |  20000
  4 | Mark  |  25 | Texas   |  20000
  5 | David |  27 | Texas   |  20000
  6 | Kim   |  22 | Texas   |  20000
  7 | James |  24 | Texas   |  20000
  3 | Teddy |  23 | Texas   |  20000
(7 rows)