Skip to content

Modifying Data in Postgresql

Insert

  • The PostgreSQL INSERT statement allows you to insert a new row into a table.

  • Syntax

INSERT INTO table_name(column1, column2, …) VALUES (value1, value2, …);

  • The INSERT statement also has an optional RETURNING clause that returns the information of the inserted row.

INSERT INTO table_name(column1, column2, …) VALUES (value1, value2, …) RETURNING *;

  • Example 1:

insert into product(id, name, price, country) Values (7, 'SAMSUNG', 120000, 'KOREA');

  • Example 2:

insert into product(id, name, price, country) Values (8, 'ONEPLUS', 130000, 'CHINA') returning id;

Update

  • The PostgreSQL UPDATE statement allows you to modify data in a table.

  • Syntax:

UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;

  • Example:

``` Update product SET price = 12000 where id = 4;

```

Update product set price = 8000 where id=6 returning id;

Delete

  • The PostgreSQL DELETE statement allows you to delete one or more rows from a table.

  • Syntax:

DELETE FROM table_name WHERE condition RETURNING (select_list | *)

  • Example: delete from product where id=8;