Yogesh Chauhan's Blog

UPDATE and DELETE Statements in Postgres

in Postgres on November 29, 2020

Postgres Update

Syntax


UPDATE table_name
SET column_1 = value_1,
    column_2 = value_2 ,...
WHERE
	condition;

DO NOT FORGET TO ADD WHERE CONDITION WHILE UPDATING OR IT WILL UPDATE ALL ROWS!

To update existing rows, use the UPDATE command. This requires three pieces of information:

=> The name of the table and column to update

=> The new value of the column

=> Which row(s) to update

SQL does not, in general, provide a unique identifier for rows. Therefore it is not always possible to directly specify which row to update. Instead, you specify which conditions a row must meet in order to be updated.

Only if you have a primary key in the table (independent of whether you declared it or not) can you reliably address individual rows by choosing a condition that matches the primary key.

Graphical database access tools rely on this fact to allow you to update rows individually.

Examples

I am going to keep using the table from INSERT Post

For example, this command updates all products that have a price of 5 to have a price of 10:


UPDATE products 
     SET price = 10 
     WHERE price = 5;

This might cause zero, one, or many rows to be updated. It is not an error to attempt an update that does not match any rows.

Let’s look at that command in detail.

=> First is the key word UPDATE followed by the table name. As usual, the table name can be schema-qualified, otherwise it is looked up in the path.

=> Next is the key word SET followed by the column name, an equal sign, and the new column value. The new column value can be any scalar expression, not just a constant.

For example, if you want to raise the price of all products by 10% you could use:


UPDATE products 
     SET price = price * 1.10;

As you see, the expression for the new value can refer to the existing value(s) in the row.

We also left out the WHERE clause. If it is omitted, it means that all rows in the table are updated.

If it is present, only those rows that match the WHERE condition are updated.

Note that the equals sign in the SET clause is an assignment while the one in the WHERE clause is a comparison, but this does not create any ambiguity.

Of course, the WHERE condition does not have to be an equality test. Many other operators are available. But the expression needs to evaluate to a Boolean result.

You can update more than one column in an UPDATE command by listing more than one assignment in the SET clause.

For example:


UPDATE mytable 
     SET a = 5, b = 3, c = 1 
     WHERE a > 0;

Update with returning clause


UPDATE mytable 
     SET a = 5, b = 3, c = 1 
     WHERE a > 0
     RETURNING a, b, c;

The above query will update the table and return the new values.

Postgres DELETE

Just as adding data is only possible in whole rows, you can only remove entire rows from a table.

In the previous section we explained that SQL does not provide a way to directly address individual rows.

Therefore, removing rows can only be done by specifying conditions that the rows to be removed have to match.

If you have a primary key in the table then you can specify the exact row. But you can also remove groups of rows matching a condition, or you can remove all rows in the table at once.

You use the DELETE command to remove rows; the syntax is very similar to the UPDATE command.

The DELETE statement returns the number of deleted rows. If no row is deleted, the DELETE statement returns zero.

Syntax


DELETE FROM table_name
     WHERE condition;

Example

To remove all rows from the products table that have a price of 10, use:


DELETE FROM products 
     WHERE price = 10;

If you simply write:


DELETE FROM products;

then all rows in the table will be deleted! Caveat programmer.

DELETE using subquery


DELETE FROM products
     WHERE price = (SELECT MAX(price) FROM products_2);

Sources


Most Read

#1 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
How to select an element using its ID without the high specificity of the ID selector?CSSHow to Check If a Variable is an Integer, a Float, a Number, NaN, an Infinite or a Numeric in PHP?PHPHow to compile and watch Sass using Gulp in WordPress?SCSSWhat are Web services?Miscellaneousadd_filter function in WordPressWordPressHow to link/add CSS file to HTML Document?CSS