Open In App

PostgreSQL – DELETE

Last Updated : 08 Oct, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The DELETE statement is a key command in PostgreSQL used to remove existing records from a table. By using DELETE, you can eliminate unwanted or outdated records, helping keep your database organized and up to date.

In this article, we will explore the DELETE statement, its syntax, and some practical examples to help you grasp how to use it. Knowing how to effectively use the DELETE statement ensures you maintain data integrity.

PostgreSQL DELETE

  • In PostgreSQL, the DELETE statement is used to remove existing records from a table.
  • This command can be utilized to delete one or more rows based on a specified condition.
  • Understanding how to effectively use the DELETE statement is essential for maintaining and managing our database.

Syntax

DELETE FROM table_name
WHERE condition;

The below rules need to be followed while using the DELETE statement:

  • table_name: The name of the table from which you want to delete records.
  • condition: A condition that specifies which records to delete. If this condition is omitted, all records in the table will be deleted.

Examples of PostgreSQL DELETE Statement

Let’s set up a sample database and table for the demonstration of the DELETE statement.

  CREATE DATABASE company;
CREATE TABLE employee (
    employee_id INT PRIMARY KEY,
    first_name VARCHAR (255) NOT NULL,
    last_name VARCHAR (255) NOT NULL,
    manager_id INT,
    FOREIGN KEY (manager_id) 
    REFERENCES employee (employee_id) 
    ON DELETE CASCADE
);
INSERT INTO employee (
    employee_id,
    first_name,
    last_name,
    manager_id
)
VALUES
    (1, 'Sandeep', 'Jain', NULL),
    (2, 'Abhishek ', 'Kelenia', 1),
    (3, 'Harsh', 'Aggarwal', 1),
    (4, 'Raju', 'Kumar', 2),
    (5, 'Nikhil', 'Aggarwal', 2),
    (6, 'Anshul', 'Aggarwal', 2),
    (7, 'Virat', 'Kohli', 3),
    (8, 'Rohit', 'Sharma', 3)

Create a database named “company” with the below command:

The value in themanager_id’ column represents the senior manager who the employee reports to. If it’s NULL, he/she doesn’t report to anyone. The overall hierarchy looks like the below image: The current database tables look like below: Output

Example 1: Deleting a Single Row

Here we will be deleting the employee data whose first name is “Raju“.

Query:

DELETE FROM employee
WHERE first_name = 'Raju';

Output:

Deleting a Single Row

Explanation: The row where ‘first_name' is “Raju” will be deleted from the employee table.

Example 2: Deleting Multiple Rows

Here we will delete multiple rows from the “employee” table. We will be deleting the data of the employee named “Abhishek Kelenia” and employees who work under him.

Query:

DELETE FROM employee
WHERE last_name = 'Kelenia';

Output:

Deleting Multiple Rows

Explanation: The row where ‘last_name' is “Kelenia” will be deleted, along with any rows dependent on this employee due to the ON DELETE CASCADE foreign key constraint.

Important Points About PostgreSQL DELETE Statement

  • The DELETE statement in PostgreSQL is essential for removing data from tables.
  • Use the WHERE clause to specify which rows to delete.
  • The ON DELETE CASCADE clause ensures that deleting a parent row also deletes dependent child rows.
  • Use the RETURNING clause to return the deleted rows, which is useful for verification.
  • Enable logging for delete operations to track changes and maintain an audit trail.

Conclusion

The DELETE statement in PostgreSQL is a crucial command for maintaining and managing your database. By allowing you to remove unwanted data, it plays a significant role in data integrity and organization.Understanding its syntax, the importance of the WHERE clause, and the implications of constraints like ON DELETE CASCADE will empower you to use this command effectively.

FAQs

What happens if I run a DELETE statement without a WHERE clause?

Running a DELETE statement without a WHERE clause will delete all records from the specified table. This action cannot be undone if committed, so it is crucial to use the WHERE clause to avoid unintentional data loss.

Can I use the DELETE statement in a transaction?

Yes, the DELETE statement can be used within a transaction. You can wrap it in BEGIN and COMMIT commands to ensure that all operations are completed successfully before finalizing the changes. If an error occurs, you can use ROLLBACK to revert any changes made during the transaction.


Next Article

Similar Reads

three90RightbarBannerImg