PostgreSql DELETE Allows you to delete the contents of a table. The command can be used with conditions to target only certain lines to be deleted. The deletion being irreversible, this command must be used with caution.
What is PostgreSql DELETE ?
Order DELETE is used in PostgreSql for delete inputs from a table. The condition WHERE Allows you to select specific lines whose content must be deleted. If the clause WHERE is not specified, all the lines of the table will be irreversibly removed. This is why you should only use this command with the greatest caution.
Dedicated servers
Performance and innovation
Take advantage of your own server, with dedicated hardware, cloud integration, minute invoicing and Intel® Xeon® or AMD processor.
PostgreSql DELETE : syntax and operation
PostgreSql syntax DELETE To the following basic structure:
DELETE FROM nom_de_la_table
WHERE Condition;
postgreSql
DELETE FROM initiates deletion in the specified table. The condition WHERE Allows you to specify in which lines the content should be deleted. To apply several deletion criteria, use operators AND Or OR.
Note
Before deleting data, you should make sure you have a recent backup of the database or that you execute the deletion process in a transaction. In this way, you will avoid losing important data if the order is poorly executed inadvertently.
Delete all the contents of a table
The operation of PostgreSql DELETE is easier to explain with a practical example. For this, we use the command CREATE TABLE To create a new postgreSql table called « List_des_clients ». We then use INSERT INTO To fill it with different content. The table has three columns called « ID », « Name » and « City » and initially receives four entries. Here is what it looks like:
|ID|Nom|Ville|
|-|-|-|
|1|Berland|Paris|
|2|Wirth|Marseille|
|3|Madiot|Lille|
|4|Bourrat|Dijon|
postgreSql
To completely empty a table while retaining its structure, use DELETE unconditional WHERE. In our example, the command looks like this:
DELETE FROM liste_des_clients;
postgreSql
Delete a line with PostgreSql DELETE
Most often, you will want Delete a specific line Rather than all the content of the table. This is also done with PostgreSql DELETE. To do this, we use the command with a clause WHERE. In our example, we want to delete the customer « Wirth » with the ID « 2 ». The appropriate code is as follows:
DELETE FROM liste_des_clients
WHERE ID = 2;
postgreSql
Specify lines with several conditions
There may be duplicates and therefore entries that are not unique, especially in long tables. If you want to be sure you only delete the desired line, you can use PostgreSql DELETE with several conditions. In our example, we have two customers named « Madiot » and we only want to delete the second entry. We will therefore combine two conditions. The code for this is:
DELETE FROM liste_des_clients
WHERE Nom = 'Madiot'
AND ID >= 3;
postgreSql
Thus, only the lines where the name is « Madiot » and the ID is greater than or equal to 3 will be deleted. As the first input with the name has an ID number less than « 3 », this input remains in the database even after the deletion command.

