PostgreSQL Table Delete

removes all records from a table. But this command will not destroy the table's structure
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.

--select the table what record do you want to delete
postgres=# select * from demo;                                                  
 id |   name   
----+----------
  2 | benz
  3 | benz
  1 | benz1
  4 | oracle
  5 | mysql
  6 | postgres
  7 | db2
(7 rows)
--You use the DELETE command to remove rows; the syntax is very similar to the UPDATE command
postgres=# delete from demo where id=1;                                         
DELETE 1

postgres=# select * from demo;         
 id |   name   
----+----------
  2 | benz
  3 | benz
  4 | oracle
  5 | mysql
  6 | postgres
  7 | db2
(6 rows)
--Deleting multiple rows in a table
postgres=# delete from demo where id in (2,5,7);
DELETE 3

postgres=# select * from demo;                  
 id |   name   
----+----------
  3 | benz
  4 | oracle
  6 | postgres
(3 rows)
--all rows in the table will be deleted but table structure(metadata) cannot be deleted see following example
postgres=# delete from demo;                    
DELETE 3




No comments:

Post a Comment