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!
Part I: General PostgreSQL Use
Introduction to PostgreSQL and SQL
Working with Data in PostgreSQL
PostgreSQL SQL Syntax and Use
Performance
Part II: Programming with PostgreSQL
Introduction to PostgreSQL Programming
Extending PostgreSQL
PL/pgSQL
The PostgreSQL C APIlibpq
A Simpler C APIlibpgeasy
The New PostgreSQL C++ APIlibpqxx
Embedding SQL Commands in C Programsecpg
Using PostgreSQL from an ODBC Client Application
Using PostgreSQL from a Java Client Application
Using PostgreSQL with Perl
Using PostgreSQL with PHP
Using PostgreSQL with Tcl and Tcl/Tk
Using PostgreSQL with Python
Npgsql: The .NET Data Provider
Other Useful Programming Tools
Part III: PostgreSQL Administration
Introduction to PostgreSQL Administration
PostgreSQL Administration
Internationalization and Localization
Security
Replicating PostgreSQL Data with Slony
Contributed Modules
Index