DELETE

   

Like UPDATE , the DELETE command is simple. The general format of the DELETE command is

 DELETE FROM  table  [ WHERE  condition  ] 

The DELETE command removes all rows that satisfy the (optional) WHERE clause. Here is an example:

 movies=# SELECT * FROM tapes;  tape_id          title          duration ----------+----------------------+----------  AB-12345  The Godfather          AB-67472  The Godfather          MC-68873  Casablanca             OW-41221  Citizen Kane           AH-54706  Rear Window            OW-42200  Sly                   01:36  KJ-03335  American Citizen, An   OW-42201  Stone Cold            01:52 (8 rows) movies=# BEGIN WORK; BEGIN movies=# DELETE FROM tapes WHERE duration IS NULL; DELETE 6 movies=# SELECT * FROM tapes;  tape_id     title     duration ----------+------------+----------  OW-42200  Sly         01:36  OW-42201  Stone Cold  01:52 (2 rows) movies=# ROLLBACK; ROLLBACK 

Before we executed the DELETE command, there were eight rows in the tapes table, and six of these tapes had a NULL duration.

You can see that the DELETE statement returns the number of rows deleted (" DELETE 6 "). After the DELETE statement, only two tapes remain .

If you omit the WHERE clause in a DELETE command, PostgreSQL will delete all rows. Similarly, forgetting the WHERE clause for an UPDATE command updates all rows. Be careful!

   


PostgreSQL
PostgreSQL (2nd Edition)
ISBN: 0672327562
EAN: 2147483647
Year: 2005
Pages: 220
Authors: Korry Douglas

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net