Update and Join Statements in PostgreSQL
This article guides you through using the UPDATE
and JOIN
statements to update a table.
Generate a shipment example in PostgreSQL
Consider a store that sells food. Every time a shipment arrives, the price of the product is updated to reflect the current market price.
In this example, there are two tables: Produce and Shipment.
Production table:
|id | price|
-----|-------
| 1 | 0|
| 2 | 0|
| 3 | 0|
| 4 | 0|
| 5 | 0|
| 6 | 0|
| 7 | 0|
| 8 | 0|
| 9 | 0|
| 10 | 0|
CREATE TABLE produce
(
id integer NOT NULL
GENERATED ALWAYS AS IDENTITY,
price integer NOT NULL,
CONSTRAINT pk_produce PRIMARY KEY (id)
)
Here is INSERT
the statement that populates the Produce table with a default cost of 0:
INSERT INTO produce(price)
SELECT 0
FROM generate_series(1,10) i
Shipping List:
|id | produce_id | produce_price|
-----|------------|---------------
| 1 | 1 | 193|
| 2 | 2 | 41|
| 3 | 3 | 184|
| 4 | 4 | 192|
| 5 | 5 | 174|
| 6 | 6 | 122|
| 7 | 7 | 70|
| 8 | 8 | 130|
| 9 | 9 | 105|
| 10 | 10 | 176|
CREATE TABLE shipment
(
id integer NOT NULL
GENERATED ALWAYS AS IDENTITY,
produce_id integer NOT NULL,
produce_price integer NOT NULL,
CONSTRAINT pk_shipment PRIMARY KEY (id),
CONSTRAINT fk_shipment_produce FOREIGN KEY (produce_id) REFERENCES produce (id)
)
Here is INSERT
the statement that populates the Shipment table with test data:
INSERT INTO shipment (produce_id, produce_price)
SELECT i, floor(random()*(200-20+1))+20
FROM generate_series(1,10) i
Update tables in PostgreSQL
using UPDATE
the and statementsJOIN
The price field in the Produce table produce_price
is updated from the shipment's field as follows:
UPDATE produce
SET price = s.produce_price
FROM shipment AS s
LEFT JOIN produce AS p ON s.produce_id = p.id
WHERE produce.id = p.id
UPDATE
Update a table in PostgreSQL
using only the
UPDATE
An optimized and more concise approach
can be achieved using only the statement, as follows:
UPDATE produce AS p
SET price = s.produce_price
FROM shipment AS s
WHERE p.id = s.produce_id
Output:
|id | price|
-----|-------
| 1 | 193|
| 2 | 41|
| 3 | 184|
| 4 | 192|
| 5 | 174|
| 6 | 122|
| 7 | 70|
| 8 | 130|
| 9 | 105|
| 10 | 176|
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Killing a process ID in PostgreSQL
Publish Date:2025/04/27 Views:191 Category:PostgreSQL
-
Today, we will learn how to kill or stop a running query in the background when working with PostgreSQL database. This may happen if the frontend stops working but a background process is still running. In this case, you may want to kill th
How to install and deploy PostgreSQL as a Docker container
Publish Date:2025/04/27 Views:50 Category:PostgreSQL
-
PostgreSQL , also known as Postgres, is a leading object-relational database system. It is popular because it is highly compliant with the SQL standard and includes additional features that simplify processing complex data sets at scale. Po
Selecting whether a string contains a substring match in PostgreSQL
Publish Date:2025/04/27 Views:186 Category:PostgreSQL
-
Today, we will learn how to find a value in a PostgreSQL string and select it if it matches certain conditions. Suppose you have a string carabc and want to see if it contains a value car . Therefore, you will try to use a function that tel
Add unique constraint after creating table in PostgreSQL
Publish Date:2025/04/27 Views:63 Category:PostgreSQL
-
Today we will learn how to add constraints after the rows in a table have been created UNIQUE . The UNIQUE constraint guarantees that the data in a row is unique in that column. So if the column ID exists, all rows will have unique values,
Creating a Schema in PostgreSQL
Publish Date:2025/04/27 Views:196 Category:PostgreSQL
-
This article will discuss creating schemas in PostgreSQL using SQL queries or psql. CREATE SCHEMA Use the statement to create a pattern in SQL query To create a new schema, execute the following command. CREATE SCHEMA test_schema To view al
Changing User Password in Postgres
Publish Date:2025/04/27 Views:108 Category:PostgreSQL
-
In this article, we will change the user password in Postgres. Changing User Passwords in Postgres Using Windows Open from the menu or search bar SQL Shell (psql) . Connect to the default database using the default port. If you set it up wi
Changing column types in Postgres
Publish Date:2025/04/27 Views:91 Category:PostgreSQL
-
This article shows how to change a column type to another data type in Postgres. ALTER TABLE To change the column type in Postgres, use the command ALTER TABLE table_name ALTER COLUMN column_name [ SET DATA ] TYPE new_type ; Use 表名 , , 列
Importing SQL files in PostgreSQL
Publish Date:2025/04/27 Views:129 Category:PostgreSQL
-
This article discusses how to import SQL files in PostgreSQL. psql Import SQL files in PostgreSQL using command To import the SQL file, run the following command: psql - U dbuser - h localhost databasename filename. sql If the file is locat
Create a table if it does not exist in PostgreSQL
Publish Date:2025/04/27 Views:197 Category:PostgreSQL
-
PostgreSQL is an object-relational database system, which means it can support more complex data types than its competitor MySQL. Apart from the above differences, when writing queries for PostgreSQL and MySQL or other database systems, the