PostgreSQL Update Command

PostgreSQL Update Command update rows of a table(objects). UPDATE changes the values of the specified columns in all rows that satisfy the condition. Only the columns to be modified need be mentioned in the SET clause; columns not explicitly modified retain their previous values. We must have update privilege on the table.

To update existing rows, use the UPDATE command. This requires three pieces of information:
1. The name of the table and column to update
2. The new value of the column
3. Which row(s) to update

Syntax:

[ WITH [ RECURSIVE ] with_query [, ...] ]
UPDATE [ ONLY ] table_name [ * ] [ [ AS ] alias ]
SET { column_name = { expression | DEFAULT } |
( column_name [, ...] ) = ( { expression | DEFAULT } [, ...] ) } [, ...]
[ FROM from_list ]
[ WHERE condition | WHERE CURRENT OF cursor_name ]
[ RETURNING * | output_expression [ [ AS ] output_name ] [, ...] ]

Example:

CREATE TABLE student (
student_name varchar(80),
student_id int,
class int,
dob date
);

insert into student values(‘Karunakar’,1001,1,’12-12-2015′);
insert into student values(‘varshini’,1002,2,’12-12-2014′);

PostgreSQL update command

Now, change student name to varshitha of student id 1002.

update student set student_name=’Varshitha’ where student_id=1002;

Please notify the below changes.

PostgreSQL update command

Powered by k2schools