PostgreSQL DELETE Query
The DELETE command allows to empty the table or remove some partial records from the table.You can use the WHERE clause with the DELETE query to delete the selected rows.
PostgreSQL DELETE Syntax:
The syntax is as follows:
DELETE FROM table_name [WHERE condition]
In this, records that satisfying the WHERE condition are to be deleted.If there is no WHERE condition then all records are deleted from the table.The delete statement returns the number of deleted records. 0 if no record is deleted.
DELETE Example :
Consider the STUDENT table is having the following records
ID | NAME | AGE | ADDRESS |
---|---|---|---|
1 | Rahul | 20 | Mysore |
2 | Kartik | 19 | Delhi |
3 | Nidhi | 21 | Udaipur |
4 | Prateek | 19 | Jaipur |
5 | Jugal | 20 | Mumbai |
6 | Abhi | 19 | Mumbai |
The following is an example, which would delete record for a student, whose ID is 2.
lfcdb=# DELETE FROM STUDENT WHERE ID = 2;
All the above statements would create the following records in STUDENT table
ID | NAME | AGE | ADDRESS |
---|---|---|---|
1 | Rahul | 20 | Mysore |
3 | Nidhi | 22 | Udaipur |
4 | Prateek | 19 | Jaipur |
5 | Jugal | 20 | Mumbai |
6 | Abhi | 19 | Mumbai |
The following is an example, if you want to DELETE all the records from STUDENT table.
lfcdb=# DELETE FROM STUDENT;There is no record in the student table because all records have been deleted by the DELETE statement.
Visit :
Discussion