9. Modifying Data
Insert
(guide you on how to insert a single row into a table.)
allows you to insert a new row into a table.
Its syntax is
Its return format is
OID is an object identifier. PostgreSQL used the OID internally as a primary key for its system tables. Typically, the INSERT statement returns OID with value 0
The count is the number of rows that the INSERT statement inserted successfully
Returning Clause
The INSERT statement also has an optional RETURNING clause that returns the information of the inserted row.
Example 1 (return the entire inserted row)
Example 2 (returns the id of the inserted row)
Example 3 (rename the returned value)
Examples
1. Inserting a single row into a table
It returns
2. Inserting character string that contains a single quote
you have to use an additional single quote (') to escape it.
3. Inserting a date value
To insert a date value into a column with the DATE type, you use the date in the format 'YYYY-MM-DD'.
Insert multiple rows
(show you how to insert multiple rows into a table.)
To insert multiple rows into a table using a single INSERT statement, you use the following syntax:
Example (Inserting multiple rows and returning inserted rows)
Update
(update existing data in a table.)
The PostgreSQL UPDATE statement allows you to modify data in a table.
Its syntax is
it returns the following command tag
where count is the number of rows updated including rows whose values did not change.
Returning updated rows
It also have optional RETURNING clause like INSERT statement that returns the updated rows
Example (updating a row and returning the updated row)
Update join
(update values in a table based on values in another table.)
Sometimes, you need to update data in a table based on values in another table. In this case, you can use the PostgreSQL UPDATE join syntax as follows:
Example
Calculate the net price of every product based on the discount of the product segment
Delete
(delete data in a table.)
It allows us to delete one or more rows from a table
Its syntax is
The DELETE statement returns the number of rows deleted.
To return the deleted row(s) to the client, you use the RETURNING clause as follows:
Example
Deletes the rows with ids 6 and 7 and returns the deleted row to the client
Upsert
(insert or update data if the new row already exists in the table.)
The idea is that when you insert a new row into the table, PostgreSQL will update the row if it already exists, otherwise, it will insert the new row. That is why we call the action is upsert (the combination of update or insert).
We can use the
INSERT ON CONFLICT
statement as follows
target
can be(column_name) – a column name.
ON CONSTRAINT constraint_name – where the constraint name could be the name of the UNIQUE constraint.
WHERE predicate – a WHERE clause with a predicate.
action
can beDO NOTHING – means do nothing if the row already exists in the table.
DO UPDATE SET column_1 = value_1, .. WHERE condition – update some fields in the table.
Example
Concatenate the new email with old email when inserting a customer that already exists
Last updated