postgresqlHow can I use PostgreSQL's UPSERT feature?
UPSERT is a PostgreSQL feature that allows you to insert or update data in a single operation. It is very useful when you need to insert new records or update existing records in a table.
Example
-- Create a table
CREATE TABLE people (
id SERIAL PRIMARY KEY,
name VARCHAR(50),
age INTEGER
);
-- Insert a new record
INSERT INTO people (name, age)
VALUES ('John', 25);
-- Update the existing record
UPSERT INTO people (id, name, age)
VALUES (1, 'John', 26);
Output example
INSERT 0 1
UPSERT 1
The code above creates a table called "people" with three columns: id, name, and age. It then inserts a new record with the name "John" and age 25. Lastly, it updates the existing record by using the UPSERT statement. This statement updates the record with id 1, setting the name to "John" and the age to 26.
The syntax for the UPSERT statement is as follows:
UPSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);
The UPSERT statement can also be used with a WHERE clause to specify which records should be updated or inserted:
UPSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...)
WHERE condition;
For more information, please refer to the PostgreSQL documentation.
More of Postgresql
- How can Zalando use PostgreSQL to improve its software development?
- How can I use PostgreSQL on the Yandex Cloud platform?
- How do I use PostgreSQL's XMLTABLE to parse XML data?
- How can I troubleshoot zero damaged pages in PostgreSQL?
- How do I use PostgreSQL's XMIN and XMAX features?
- How do I use the PostgreSQL hash function?
- How can I set a PostgreSQL interval to zero?
- How do I use PostgreSQL ZonedDateTime to store date and time information?
- How can I use PostgreSQL with Zabbix?
- How do I set the PostgreSQL work_mem parameter?
See more codes...