Usage of UPDATE statement in POSTGRESQL Database

The UPDATE statement is used to modify/update data in a database table.

SYNTAX:

UPDATE statement is used to modify one column or multiple columns at a time.

The syntax for updating one column is :

UPDATE “table_name” SET “column_1” = [new value] WHERE “condition”;

The syntax for updating multiple columns is:

UPDATE “table_name” SET “column_1 = [new value], column_2 = [new value],…. column_N = [new value] WHERE “condition”;

Examples:

We are using following table for our examples:

Table Employee

Example1 : Updating a single column in a database table called Employee

In this example we need to change the salary of Dave is actually 70000 instead of 80000 and that particular entry needs to be updated. To do so, we use the following SQL query:

UPDATE Employee SET salary = 70000 WHERE name = ‘Dave’ AND dept = ‘Accounting’;

The Resulting table would look like:

In this case only one row is modified because of WHERE clause. If you need to modify all rows then remove the WHERE clause and use the condition.

Example2 : Updating multiple columns in a database table called employee

We notice that salary of Clark is 80000 and dept is Managing. To do so we use the following SQL query:

UPDATE Employee SET salary = 80000, dept = ‘Managing’ WHERE name = ‘Clark’ ;

The Resulting table looks like :

Example3: Consider one more table and change multiple columns

UPDATE EMPLOYEE_DEMO1 SET first_name=‘Harshitha’ where ID=2

The output looks like:

Note : If you don’t supply where condition in update statement , all the rows in the table updated.

Leave a Comment

Your email address will not be published. Required fields are marked *